Maximum number of pair reductions possible on a given triplet
Last Updated :
31 Oct, 2023
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
void maxOps(int a, int b, int c)
{
// Convert them into an array
int arr[] = { a, b, c };
// Stores count of operations
int count = 0;
while (1) {
// Sort the array
sort(arr, arr + 3);
// If the first two array
// elements reduce to 0
if (!arr[0] && !arr[1])
break;
// Apply the operations
arr[1] -= 1;
arr[2] -= 1;
// Increment count
count += 1;
}
// Print the maximum count
cout << count;
}
// Driver Code
int main()
{
// Given triplet
int a = 4, b = 3, c = 2;
maxOps(a, b, c);
return 0;
}
// This code is contributed by subhammahato348.
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
static void maxOps(int a, int b, int c)
{
// Convert them into an array
int arr[] = { a, b, c };
// Stores count of operations
int count = 0;
while (1 != 0)
{
// Sort the array
Arrays.sort(arr);
// If the first two array
// elements reduce to 0
if (arr[0] == 0 && arr[1] == 0)
break;
// Apply the operations
arr[1] -= 1;
arr[2] -= 1;
// Increment count
count += 1;
}
// Print the maximum count
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
// Given triplet
int a = 4, b = 3, c = 2;
maxOps(a, b, c);
}
}
// This code is contributed by code_hunt.
Python3
# Python3 program for the above approach
# Function to count the maximum
# number of pair reductions
# possible on a given triplet
def maxOps(a, b, c):
# Convert them into an array
arr = [a, b, c]
# Stores count of operations
count = 0
while True:
# Sort the array
arr.sort()
# If the first two array
# elements reduce to 0
if not arr[0] and not arr[1]:
break
# Apply the operations
arr[1] -= 1
arr[2] -= 1
# Increment count
count += 1
# Print the maximum count
print(count)
# Given triplet
a, b, c = 4, 3, 2
maxOps(a, b, c)
C#
// C# program for the above approach
using System;
class GFG
{
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
static void maxOps(int a, int b, int c)
{
// Convert them into an array
int[] arr = { a, b, c };
// Stores count of operations
int count = 0;
while (1 != 0)
{
// Sort the array
Array.Sort(arr);
// If the first two array
// elements reduce to 0
if (arr[0] == 0 && arr[1] == 0)
break;
// Apply the operations
arr[1] -= 1;
arr[2] -= 1;
// Increment count
count += 1;
}
// Print the maximum count
Console.WriteLine(count);
}
// Driver Code
public static void Main(String[] args)
{
// Given triplet
int a = 4, b = 3, c = 2;
maxOps(a, b, c);
}
}
// This code is contributed by susmitakundugoaldanga.
JavaScript
<script>
// JavaScript program for the above approach
// Function to count the maximum
// number of pair reductions
// possible on a given triplet
function maxOps(a, b, c)
{
// Convert them into an array
let arr = [ a, b, c ];
// Stores count of operations
let count = 0;
while (1) {
// Sort the array
arr.sort();
// If the first two array
// elements reduce to 0
if (!arr[0] && !arr[1])
break;
// Apply the operations
arr[1] -= 1;
arr[2] -= 1;
// Increment count
count += 1;
}
// Print the maximum count
document.write(count);
}
// Driver Code
// Given triplet
let a = 4, b = 3, c = 2;
maxOps(a, b, c);
// This code is contributed by Surbhi Tyagi.
</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; // Output: 4
return 0;
}
Java
import java.util.Arrays;
public class MaxPairReductions {
// Function to calculate the maximum pair reductions
static int maxPairReductions(int A, int B, int C) {
int[] triplet = { A, B, C }; // Create an array to hold the values A, B, and C
Arrays.sort(triplet); // Sort the array in ascending order
int count = 0; // Initialize a counter for reductions
// Continue reducing pairs until both A and B are greater than 0
while (triplet[2] > 0 && triplet[1] > 0) {
int minVal = Math.min(triplet[2], triplet[1]); // Find the minimum value between triplet[2] and triplet[1]
triplet[2] -= minVal; // Reduce triplet[2] by minVal
triplet[1] -= minVal; // Reduce triplet[1] by minVal
count += minVal; // Increment the reduction count
Arrays.sort(triplet); // Sort the array again to maintain ascending order
}
return count + triplet[0]; // Return the total count plus the remaining value in triplet[0]
}
public static void main(String[] args) {
int A = 4;
int B = 3;
int C = 2;
System.out.println(maxPairReductions(A, B, C)); // Output: 4
}
}
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)) # 4
C#
using System;
using System.Linq;
class Program
{
// Function to find the maximum number of reductions in a triplet
static int MaxPairReductions(int A, int B, int C)
{
// Create an array to store the triplet values
int[] triplet = { A, B, C };
// Sort the triplet in descending order
Array.Sort(triplet, (x, y) => y.CompareTo(x));
// Initialize a variable to count the number of reductions
int count = 0;
// Continue reducing pairs while the two largest elements are positive
while (triplet[0] > 0 && triplet[1] > 0)
{
// Find the minimum value between the two largest elements
int minVal = Math.Min(triplet[0], triplet[1]);
// Subtract the minimum value from both elements
triplet[0] -= minVal;
triplet[1] -= minVal;
// Increase the reduction count
count += minVal;
// Re-sort the triplet to maintain the descending order
Array.Sort(triplet, (x, y) => y.CompareTo(x));
}
// Add the remaining value to the count
return count + triplet[2];
}
static void Main()
{
// Input values
int A = 4;
int B = 3;
int C = 2;
// Call the function and print the result
Console.WriteLine(MaxPairReductions(A, B, C)); // Output: 4
}
}
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)); // Output: 4
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.
Similar Reads
Maximum possible GCD for a pair of integers with product N
Given an integer N, the task is to find the maximum possible GCD among all pair of integers with product N.Examples: Input: N=12 Output: 2 Explanation: All possible pairs with product 12 are {1, 12}, {2, 6}, {3, 4} GCD(1, 12) = 1 GCD(2, 6) = 2 GCD(3, 4) = 1 Therefore, the maximum possible GCD = maxi
9 min read
Number of special pairs possible from the given two numbers
Given two numbers A, B. The task is to find the numbers of special pairs of A, B. A special pair of two numbers A, B is a pair of numbers X, Y which satisfies both of the given conditions - A = X | Y, B = X & Y. Examples: Input: A = 3, B = 0 Output: 2 (0, 3), (1, 2) will satisfy the conditions I
5 min read
Possible cuts of a number such that maximum parts are divisible by 3
Given a Large number N ( number of digits in N can be up to 105). The task is to find the cuts required of a number such that maximum parts are divisible by 3.Examples: Input: N = 1269 Output: 3 Cut the number as 12|6|9. So, 12, 6, 9 are the three numbers which are divisible by 3. Input: N = 71 Outp
12 min read
Minimum product modulo N possible for any pair from a given range
Given three integers L, R, and N, the task is to find the minimum possible value of (i * j) % N, where L ? i < j ? R. Examples: Input: L = 2020, R = 2040, N = 2019Output: 2Explanation: (2020 * 2021) % 2019 = 2 Input: L = 15, R = 30, N = 15Output: 0Explanation: If one of the elements of the pair i
5 min read
Maximum possible GCD for a pair of integers with sum N
Given an integer N, the task is to find the maximum possible GCD of a pair of integers such that their sum is N. Examples : Input: N = 30 Output: 15 Explanation: GCD of (15, 15) is 15, which is the maximum possible GCD Input: N = 33 Output: 11 Explanation: GCD of (11, 22) is 11, which is the maximum
4 min read
Find the Number of Maximum Product Quadruples
Given an array of N positive elements, find the number of quadruples, (i, j, k, m) such that i < j < k < m such that the product aiajakam is the maximum possible. Examples: Input : N = 7, arr = {1, 2, 3, 3, 3, 3, 5} Output : 4 Explanation The maximum quadruple product possible is 135, which
9 min read
Maximum number of groups of size 3 containing two type of items
Given n instance of item A and m instance of item B. Find the maximum number of groups of size 3 that can be formed using these items such that all groups contain items of both types, i.e., a group should not have either all items of type A or all items of type B. The total number of items of type A
10 min read
Number of pairs with maximum sum
Given an array arr[], count number of pairs arr[i], arr[j] such that arr[i] + arr[j] is maximum and i < j. Example : Input : arr[] = {1, 1, 1, 2, 2, 2} Output : 3 Explanation: The maximum possible pair sum where i<j is 4, which is given by 3 pairs, so the answer is 3 the pairs are (2, 2), (2,
11 min read
Maximum product of a triplet (subsequence of size 3) in array
Given an integer array, find a maximum product of a triplet in the array.Examples: Input: arr[ ] = [10, 3, 5, 6, 20]Output: 1200Explanation: Multiplication of 10, 6 and 20Input: arr[ ] = [-10, -3, -5, -6, -20]Output: -90Input: arr[ ] = [1, -4, 3, -6, 7, 0]Output: 168[Naive Approach] By Using three n
12 min read
Count number of triplets with product equal to given number | Set 2
Given an array of distinct integers(considering only positive numbers) and a number âmâ, find the number of triplets with the product equal to âmâ. Examples: Input: arr[] = { 1, 4, 6, 2, 3, 8} m = 24 Output: 3 Input: arr[] = { 0, 4, 6, 2, 3, 8} m = 18 Output: 0 An approach with O(n) extra space has
7 min read