Given three integers A, B and C. In one operation, choose any two of the three integers, subject to the condition that both of them should be greater than 0, and reduce them by 1. The task is to find the maximum number of operations that can be performed until at least two of them becomes 0.
Examples:
Input: A = 1, B = 3, C = 1
Output: 2
Explanation:
Operation 1: Choose A and B, reduce both by 1. Current values: A = 0, B = 2, C = 1
Operation 2: Choose B and C, reduce both by 1. Current Values: A = 0, B = 1, C = 0
No more opeartions are possible as any pair chosen will have at least one 0 in it.Input: A = 8, B = 1, C = 4
Output: 5
Approach: The idea is to arrange the given numbers in decreasing order find the maximum number of operations on the basis of the following condition:
Case 1: When A ≥ (B + C)
- Choose pair A and B for B times. As a result, after B operations the current status will be A = (A – B) and B = 0.
- As A ≥ (B + C) which implies (A – B) ≥ C. So the pair A and C can be chosen for C operations, and the current status will be A = (A – B – C), B = 0 and C = 0.
- Total operations performed = (B + C)
Case 2: When A < (B + C)
- Try to make A, B, C equal after performing some operations.
- First make A and B equal. For this choose A and C for performing (A – B) operations. Let the updated values be named A1, B1 and C1. The values A1, B1 and C1 will be:
A1 = A – (A – B)
B1 = B
C1 = C – (A – B) - The number of operations performed = (A – B).
- A1 and B1 are equal. So, choose the pair A1 and B1 for (A1 – C1) operations.
- Let A2, B2 and C2 be the updated values of A, B and C after the above operation. The values of A2, B2 and C2 will be same and that will be:
A2 = A1 – (A1 – C1) = C1 = (C – A + B)
B2 = C – A + B
C2 = C – A + B - Let the total number of operations performed as of now be Z. So the value of Z will be:
Z = (A – B) + (A1 – C1) = (A – B) + (B – C + A – B)= 2A – B – C
- As A2 = B2 = C2, then there arises two cases:
- A2, B2, C2 are even: For every set of 3 operations on the pairs (A2, B2), (B2, C2), and (C2, A2) the count of the A2, B2 and C2 decreases by 2.
Let A2 = B2 = C2 = 4. Let the operations that can be performed be X. So, X = (4 + 4 + 4) / 2 = 6. Thus the value of X can be generalised as:
- A2, B2, C2 are odd: For every set of 3 operations on the pairs (A2, B2), (B2, C2), and (C2, A2) the count of the A2, B2 and C2 decreases by 2, finally the values A2, B2 and C2 reach 1, 1 and 1 respectively. Here one additional opeartion can be performed.
Let A2 = B2 = C2 = 5. After performing 6 operations, A2 = B2 = C2 = 1. Here one more operation can be performed. Therefore total opeartions that can be performed are 7 (6+1). Let the operations that can be performed be Y. So, Y = floor((5 + 5 + 5) / 2) = 7. Thus the value of Y can be generalised as:
- A2, B2, C2 are even: For every set of 3 operations on the pairs (A2, B2), (B2, C2), and (C2, A2) the count of the A2, B2 and C2 decreases by 2.
- Since from the above steps X = Y, therefore total number of possible cases can be given by:
Total number of possible cases = (Z + X) = (Z + Y) = (A + B + C) / 2.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum number // operations int solution(int A, int B, int C) { int arr[3]; // Insert the three numbers in array arr[0] = A, arr[1] = B, arr[2] = C; // Sort the array sort(arr, arr + 3); // Case 2 if (arr[2] < arr[0] + arr[1]) return ((arr[0] + arr[1] + arr[2]) / 2); // Case 1 else return (arr[0] + arr[1]); } // Driver Code int main() { // Given A, B, C int A = 8, B = 1, C = 5; // Function Call cout << solution(A, B, C); return 0; } |
6
Time Complexity: O(1)
Space Complexity: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Making zero array by decrementing pairs of adjacent
- Check if all elements of the given array can be made 0 by decrementing value in pairs
- Maximize distinct elements by incrementing/decrementing an element or keeping it same
- No of pairs (a[j] >= a[i]) with k numbers in range (a[i], a[j]) that are divisible by x
- Print k numbers where all pairs are divisible by m
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Count of pairs with sum N from first N natural numbers
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count of decreasing pairs formed from numbers 1 to N
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count pairs of natural numbers with GCD equal to given number
- Find the maximum cost of an array of pairs choosing at most K pairs
- Ways to form n/2 pairs such that difference of pairs is minimum
- Find maximum operations to reduce N to 1
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Reduce a number to 1 by performing given operations | Set 2
- Reduce N to 1 with minimum number of given operations
- Reduce the fraction to its lowest form
- Min number of operations to reduce N to 0 by subtracting any digits from N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

