Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | Java program to check if we come back to zero or not in a clock ; Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; sum += a [ j ] ; if set then consider as ' + ' ; sum -= a [ j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver code
import java . lang . Math ; class GfG { static boolean checkCombinations ( int a [ ] , int n ) { int pow_set_size = ( int ) Math . pow ( 2 , n ) ; int counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { int sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } public static void main ( String [ ] args ) { int a [ ] = { 60 , 60 , 120 } ; int n = a . length ; if ( checkCombinations ( a , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }