Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | C ++ code for the above approach ; If total candies are not multiple of 4 then its not possible to be left with 0 candies ; If minimum candies of three bags are less than number of operations required then the task is not possible ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; bool can_empty ( ll a , ll b , ll c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { int m = min ( a , min ( b , c ) ) ; if ( m < ( ( a + b + c ) / 4 ) ) return false ; } return true ; } int main ( ) { ll a = 4 , b = 2 , c = 2 ; cout << ( can_empty ( a , b , c ) ? " true " : " false " ) << endl ; a = 3 , b = 4 , c = 2 ; cout << ( can_empty ( a , b , c ) ? " true " : " false " ) << endl ; return 0 ; }