Check if all 3 Candy bags can be emptied by removing 2 candies from any one bag and 1 from the other two repeatedly | Java 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
import java . util . * ; class GFG { static boolean can_empty ( int a , int b , int c ) { if ( ( a + b + c ) % 4 != 0 ) return false ; else { int m = Math . min ( a , Math . min ( b , c ) ) ; if ( m < ( ( a + b + c ) / 4 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 4 , b = 2 , c = 2 ; System . out . println ( can_empty ( a , b , c ) ? " true " : " false " ) ; a = 3 ; b = 4 ; c = 2 ; System . out . println ( can_empty ( a , b , c ) ? " true " : " false " ) ; } }