# Reduce given three Numbers by decrementing in Pairs

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:
1. 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: 2. 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: • 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  using namespace std;     // Function to find the minimum number  // operations  int solution(int A, int B, int C)  {      int arr;         // Insert the three numbers in array      arr = A, arr = B, arr = C;         // Sort the array      sort(arr, arr + 3);         // Case 2      if (arr < arr + arr)          return ((arr + arr                   + arr)                  / 2);      // Case 1      else         return (arr + arr);  }     // Driver Code  int main()  {      // Given A, B, C      int A = 8, B = 1, C = 5;         // Function Call      cout << solution(A, B, C);      return 0;  }

Output:

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.