Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | 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
function checkCombinations ( a , n ) { var pow_set_size = parseInt ( Math . pow ( 2 , n ) ) ; var counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { var sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } var a = [ 60 , 60 , 120 ] ; var n = a . length ; if ( checkCombinations ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;