Check if it is possible to get back to 12’0 clock only by adding or subtracting given seconds
Given N seconds. The task is to check if it is possible to start from the 12’0 clock and get back to 12 only by adding or subtracting the given seconds. We need to use all given seconds exactly once, we can either add an element or subtract it.
Examples:
Input: a[] = {60, 60, 120} Output: YES Add the first two seconds and subtract the last one to get back to 0. Input : a[] = {10, 20, 60, 180} Output : NO
Simple Approach: Generate all possible combinations to solve the above problem. Hence generate the power set of N numbers. Check if anyone’s sum%(24*60) is equal to zero or not, if it is then it is possible else not.
Below is the implementation of the above approach:
C++
// C++ program to check if we come back to // zero or not in a clock #include <bits/stdc++.h> using namespace std; // Function to check all combinations bool checkCombinations( int a[], int n) { // Generate all power sets int pow_set_size = pow (2, n); int counter, j; // Check for every combination for (counter = 0; counter < pow_set_size; counter++) { // Store sum for all combinations int sum = 0; for (j = 0; j < n; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if (counter & (1 << j)) sum += a[j]; // if set then consider as '+' else sum -= a[j]; // else consider as '-' } // If we can get back to 0 if (sum % (24 * 60) == 0) return true ; } return false ; } // Driver Code int main() { int a[] = { 60, 60, 120 }; int n = sizeof (a) / sizeof (a[0]); if (checkCombinations(a, n)) cout << "YES" ; else cout << "NO" ; return 0; } |
Java
// Java program to check if we come // back to zero or not in a clock import java.lang.Math; class GfG { // Function to check all combinations static boolean checkCombinations( int a[], int n) { // Generate all power sets int pow_set_size = ( int )Math.pow( 2 , n); int counter, j; // Check for every combination for (counter = 0 ; counter < pow_set_size; counter++) { // Store sum for all combinations int sum = 0 ; for (j = 0 ; j < n; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if ((counter & ( 1 << j)) != 0 ) sum += a[j]; // if set then consider as '+' else sum -= a[j]; // else consider as '-' } // If we can get back to 0 if (sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } // Driver code 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" ); } } // This code is contributed by Rituraj Jain |
Python 3
# Python 3 program to check if we come # back to zero or not in a clock # Function to check all combinations def checkCombinations(a, n): # Generate all power sets pow_set_size = pow ( 2 , n) # Check for every combination for counter in range (pow_set_size): # Store sum for all combinations sum = 0 for j in range (n) : # Check if jth bit in the counter is set # If set then print jth element from set if (counter & ( 1 << j)): sum + = a[j] # if set then consider as '+' else : sum - = a[j] # else consider as '-' # If we can get back to 0 if ( sum % ( 24 * 60 ) = = 0 ): return True return False # Driver Code if __name__ = = "__main__" : a = [ 60 , 60 , 120 ] n = len (a) if (checkCombinations(a, n)): print ( "YES" ) else : print ( "NO" ) # This code is contributed by ita_c |
C#
// C# program to check if we come // back to zero or not in a clock using System; class GfG { // Function to check all combinations static bool checkCombinations( int [] a, int n) { // Generate all power sets int pow_set_size = ( int )Math.Pow(2, n); int counter, j; // Check for every combination for (counter = 0; counter < pow_set_size; counter++) { // Store sum for all combinations int sum = 0; for (j = 0; j < n; j++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if ((counter & (1 << j)) != 0) sum += a[j]; // if set then consider as '+' else sum -= a[j]; // else consider as '-' } // If we can get back to 0 if (sum % (24 * 60) == 0) return true ; } return false ; } // Driver code public static void Main() { int [] a = { 60, 60, 120 }; int n = a.Length; if (checkCombinations(a, n)) Console.WriteLine( "YES" ); else Console.WriteLine( "NO" ); } } // This code is contributed by ihritik |
PHP
<?php // PHP program to check if we come back to // zero or not in a clock // Function to check all combinations function checkCombinations( $a , $n ) { // Generate all power sets $pow_set_size = pow(2, $n ); // Check for every combination for ( $counter = 0; $counter < $pow_set_size ; $counter ++) { // Store sum for all combinations $sum = 0; for ( $j = 0; $j < $n ; $j ++) { /* Check if jth bit in the counter is set If set then print jth element from set */ if ( $counter & (1 << $j )) $sum += $a [ $j ]; // if set then consider as '+' else $sum -= $a [ $j ]; // else consider as '-' } // If we can get back to 0 if ( $sum % (24 * 60) == 0) return true; } return false; } // Driver Code $a = array ( 60, 60, 120 ); $n = sizeof( $a ); if (checkCombinations( $a , $n )) echo "YES" ; else echo "NO" ; // This code is contributed by Ryuga ?> |
Javascript
<script> // JavaScript program to check if we come // back to zero or not in a clock // Function to check all combinations function checkCombinations(a , n) { // Generate all power sets var pow_set_size = parseInt( Math.pow(2, n)); var counter, j; // Check for every combination for (counter = 0; counter < pow_set_size; counter++) { // Store sum for all combinations var sum = 0; for (j = 0; j < n; j++) { /* * Check if jth bit in the counter is set If set then print jth element from set */ if ((counter & (1 << j)) != 0) sum += a[j]; // if set then consider as '+' else sum -= a[j]; // else consider as '-' } // If we can get back to 0 if (sum % (24 * 60) == 0) return true ; } return false ; } // Driver code var a = [ 60, 60, 120 ]; var n = a.length; if (checkCombinations(a, n)) document.write( "YES" ); else document.write( "NO" ); // This code contributed by Rajput-Ji </script> |
YES
Time Complexity: O(N*2N), as we are using a nested loop to traverse 2N*N times. Where N is the number of elements in the array.
Auxiliary Space: O(1), as we are not using any extra space.
If we take a closer look, we can notice that this problem is basically a variation of the Partition Problem. So we can optimize it using Dynamic Programming (Please refer to method 2 of Partition Problem).
Contact Us