Reduce given three Numbers by decrementing in Pairs
Last Updated :
10 May, 2021
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 become 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 operations 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 to find the maximum number of operations on the basis of the following condition:
Case 1: When A ? (B + C)
- Choose pairs 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) 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 the 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 generalized 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 operation 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 operations 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 generalized as:
- Since from the above steps X = Y, therefore a total number of possible causes 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++
#include <bits/stdc++.h>
using namespace std;
int solution( int A, int B, int C)
{
int arr[3];
arr[0] = A, arr[1] = B, arr[2] = C;
sort(arr, arr + 3);
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1]
+ arr[2])
/ 2);
else
return (arr[0] + arr[1]);
}
int main()
{
int A = 8, B = 1, C = 5;
cout << solution(A, B, C);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int solution( int A, int B, int C)
{
int arr[] = new int [ 3 ];
arr[ 0 ] = A; arr[ 1 ] = B; arr[ 2 ] = C;
Arrays.sort(arr);
if (arr[ 2 ] < arr[ 0 ] + arr[ 1 ])
return ((arr[ 0 ] + arr[ 1 ] + arr[ 2 ]) / 2 );
else
return (arr[ 0 ] + arr[ 1 ]);
}
public static void main(String[] args)
{
int A = 8 , B = 1 , C = 5 ;
System.out.println(solution(A, B, C));
}
}
|
Python
def solution(A, B, C):
arr = [ 0 ] * 3
arr[ 0 ] = A
arr[ 1 ] = B
arr[ 2 ] = C
arr = sorted (arr)
if (arr[ 2 ] < arr[ 0 ] + arr[ 1 ]):
return ((arr[ 0 ] + arr[ 1 ] + arr[ 2 ]) / / 2 )
else :
return (arr[ 0 ] + arr[ 1 ])
if __name__ = = '__main__' :
A = 8
B = 1
C = 5
print (solution(A, B, C))
|
C#
using System;
class GFG{
public static int solution( int A, int B, int C)
{
int []arr = new int [3];
arr[0] = A; arr[1] = B; arr[2] = C;
Array.Sort(arr);
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1] + arr[2]) / 2);
else
return (arr[0] + arr[1]);
}
public static void Main(String[] args)
{
int A = 8, B = 1, C = 5;
Console.WriteLine(solution(A, B, C));
}
}
|
Javascript
<script>
function solution(A, B, C)
{
let arr = Array.from({length: 3}, (_, i) => 0);
arr[0] = A; arr[1] = B; arr[2] = C;
arr.sort();
if (arr[2] < arr[0] + arr[1])
return ((arr[0] + arr[1] + arr[2]) / 2);
else
return (arr[0] + arr[1]);
}
let A = 8, B = 1, C = 5;
document.write(solution(A, B, C));
</script>
|
Time Complexity: O(1)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...