Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | Javascript 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
function can_empty ( a , b , c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { let m = Math . min ( a , Math . min ( b , c ) ) ; if ( m < Math . floor ( ( a + b + c ) / 4 ) ) return false ; } return true ; } let a = 4 , b = 2 , c = 2 ; document . write ( can_empty ( a , b , c ) ? " " : " " ) ; document . write ( " " ) ; ( a = 3 ) , ( b = 4 ) , ( c = 2 ) ; document . write ( can_empty ( a , b , c ) ? " " : " " ) ;