Maximum number of pair reductions possible on a given triplet
Given a triplet of integers (A, B, C), the task is to count the maximum number of decrements by 1 that can be performed on positive pairs of the given triplet.
Examples:
Input: A = 4, B = 3, C = 2
Output: 4
Explanation:
Operation 1: Reduce the pair (4, 3). Therefore, the triplet reduces to {3, 2, 2}.
Operation 2: Reduce the pair (3, 2). Therefore, the triplet reduces to {2, 1, 2}.
Operation 3: Reduce the pair (2, 2). Therefore, the triplet reduces to {1, 1, 1}.
Operation 3: Reduce the pair (1, 1). Therefore, the triplet reduces to {0, 0, 1}.
No further operations are possible.
Input: A = 7, B = 9, C = 6
Output: 11
Approach: The idea is to use Greedy Approach to solve the problem. Follow the steps below to solve the problem:
- Store the triplet in an array.
- Initialize a variable, say count, to store the maximum possible reductions that can be performed on the triplet.
- Iterate a loop until the first two array elements are reduced to 0 and perform the following operations:
- Print the value of count as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxOps( int a, int b, int c)
{
int arr[] = { a, b, c };
int count = 0;
while (1) {
sort(arr, arr + 3);
if (!arr[0] && !arr[1])
break ;
arr[1] -= 1;
arr[2] -= 1;
count += 1;
}
cout << count;
}
int main()
{
int a = 4, b = 3, c = 2;
maxOps(a, b, c);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void maxOps( int a, int b, int c)
{
int arr[] = { a, b, c };
int count = 0 ;
while ( 1 != 0 )
{
Arrays.sort(arr);
if (arr[ 0 ] == 0 && arr[ 1 ] == 0 )
break ;
arr[ 1 ] -= 1 ;
arr[ 2 ] -= 1 ;
count += 1 ;
}
System.out.print(count);
}
public static void main(String[] args)
{
int a = 4 , b = 3 , c = 2 ;
maxOps(a, b, c);
}
}
|
Python3
def maxOps(a, b, c):
arr = [a, b, c]
count = 0
while True :
arr.sort()
if not arr[ 0 ] and not arr[ 1 ]:
break
arr[ 1 ] - = 1
arr[ 2 ] - = 1
count + = 1
print (count)
a, b, c = 4 , 3 , 2
maxOps(a, b, c)
|
C#
using System;
class GFG
{
static void maxOps( int a, int b, int c)
{
int [] arr = { a, b, c };
int count = 0;
while (1 != 0)
{
Array.Sort(arr);
if (arr[0] == 0 && arr[1] == 0)
break ;
arr[1] -= 1;
arr[2] -= 1;
count += 1;
}
Console.WriteLine(count);
}
public static void Main(String[] args)
{
int a = 4, b = 3, c = 2;
maxOps(a, b, c);
}
}
|
Javascript
<script>
function maxOps(a, b, c)
{
let arr = [ a, b, c ];
let count = 0;
while (1) {
arr.sort();
if (!arr[0] && !arr[1])
break ;
arr[1] -= 1;
arr[2] -= 1;
count += 1;
}
document.write(count);
}
let a = 4, b = 3, c = 2;
maxOps(a, b, c);
</script>
|
Time Complexity: O(log n), where n is the maximum value among a, b, and c, as the number of pair reductions required to reduce the two largest numbers to 0 is proportional to log n.
Auxiliary Space: O(1)
METHOD: Max_ pair_ reductions
APPROACH:
This algorithm sorts the input triplet in descending order and then repeatedly subtracts the smaller of the first two values from both of them until one of them becomes zero. The number of reductions performed in each step is added to a counter. Once one of the values becomes zero, the algorithm adds the remaining value to the counter and returns it as the maximum number of pair reductions possible
ALGORITHM:
1.Create a list containing the values of A, B, and C.
2.Sort the list in decreasing order.
3.Initialize a count variable to 0.
4.While the first two elements of the list are positive, perform the following operations:
a. Find the minimum of the first two elements.
b. Subtract the minimum from both elements.
c. Increment the count variable by the minimum.
d. Sort the list in decreasing order.
5.Return the count variable plus the value of the remaining element of the list.
C++
#include <algorithm>
#include <iostream>
using namespace std;
int max_pair_reductions( int A, int B, int C)
{
int triplet[] = { A, B, C };
sort(triplet, triplet + 3, greater< int >());
int count = 0;
while (triplet[0] > 0 && triplet[1] > 0) {
int min_val = min(triplet[0], triplet[1]);
triplet[0] -= min_val;
triplet[1] -= min_val;
count += min_val;
sort(triplet, triplet + 3, greater< int >());
}
return count + triplet[2];
}
int main()
{
int A = 4;
int B = 3;
int C = 2;
cout << max_pair_reductions(A, B, C)
<< endl;
return 0;
}
|
Java
import java.util.Arrays;
public class MaxPairReductions {
static int maxPairReductions( int A, int B, int C) {
int [] triplet = { A, B, C };
Arrays.sort(triplet);
int count = 0 ;
while (triplet[ 2 ] > 0 && triplet[ 1 ] > 0 ) {
int minVal = Math.min(triplet[ 2 ], triplet[ 1 ]);
triplet[ 2 ] -= minVal;
triplet[ 1 ] -= minVal;
count += minVal;
Arrays.sort(triplet);
}
return count + triplet[ 0 ];
}
public static void main(String[] args) {
int A = 4 ;
int B = 3 ;
int C = 2 ;
System.out.println(maxPairReductions(A, B, C));
}
}
|
Python3
def max_pair_reductions(A, B, C):
triplet = [A, B, C]
triplet.sort(reverse = True )
count = 0
while triplet[ 0 ] > 0 and triplet[ 1 ] > 0 :
min_val = min (triplet[ 0 ], triplet[ 1 ])
triplet[ 0 ] - = min_val
triplet[ 1 ] - = min_val
count + = min_val
triplet.sort(reverse = True )
return count + triplet[ 2 ]
A = 4
B = 3
C = 2
print (max_pair_reductions(A, B, C))
|
C#
using System;
using System.Linq;
class Program
{
static int MaxPairReductions( int A, int B, int C)
{
int [] triplet = { A, B, C };
Array.Sort(triplet, (x, y) => y.CompareTo(x));
int count = 0;
while (triplet[0] > 0 && triplet[1] > 0)
{
int minVal = Math.Min(triplet[0], triplet[1]);
triplet[0] -= minVal;
triplet[1] -= minVal;
count += minVal;
Array.Sort(triplet, (x, y) => y.CompareTo(x));
}
return count + triplet[2];
}
static void Main()
{
int A = 4;
int B = 3;
int C = 2;
Console.WriteLine(MaxPairReductions(A, B, C));
}
}
|
Javascript
function max_pair_reductions(A, B, C)
{
let triplet = [ A, B, C ];
triplet.sort();
triplet.reverse();
let count = 0;
while (triplet[0] > 0 && triplet[1] > 0) {
let min_val = Math.min(triplet[0], triplet[1]);
triplet[0] -= min_val;
triplet[1] -= min_val;
count += min_val;
triplet.sort();
triplet.reverse();
}
return count + triplet[2];
}
let A = 4;
let B = 3;
let C = 2;
document.write(max_pair_reductions(A, B, C));
|
The time complexity of this algorithm is O(log n), where n is the maximum value of A, B, and C. The space complexity is O(1), as the algorithm only uses a fixed amount of memory for the input triplet, counter, and some temporary variables.
Last Updated :
31 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...