Check if it is possible to get back to 12’0 clock only by adding or subtracting given seconds
Last Updated :
16 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
bool checkCombinations( int a[], int n)
{
int pow_set_size = 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))
sum += a[j];
else
sum -= a[j];
}
if (sum % (24 * 60) == 0)
return true ;
}
return false ;
}
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
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 )
sum += a[j];
else
sum -= a[j];
}
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" );
}
}
|
Python 3
def checkCombinations(a, n):
pow_set_size = pow ( 2 , n)
for counter in range (pow_set_size):
sum = 0
for j in range (n) :
if (counter & ( 1 << j)):
sum + = a[j]
else :
sum - = a[j]
if ( sum % ( 24 * 60 ) = = 0 ):
return True
return False
if __name__ = = "__main__" :
a = [ 60 , 60 , 120 ]
n = len (a)
if (checkCombinations(a, n)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GfG
{
static bool 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)
sum += a[j];
else
sum -= a[j];
}
if (sum % (24 * 60) == 0)
return true ;
}
return false ;
}
public static void Main()
{
int [] a = { 60, 60, 120 };
int n = a.Length;
if (checkCombinations(a, n))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
PHP
<?php
function checkCombinations( $a , $n )
{
$pow_set_size = pow(2, $n );
for ( $counter = 0;
$counter < $pow_set_size ; $counter ++)
{
$sum = 0;
for ( $j = 0; $j < $n ; $j ++)
{
if ( $counter & (1 << $j ))
$sum += $a [ $j ];
else
$sum -= $a [ $j ];
}
if ( $sum % (24 * 60) == 0)
return true;
}
return false;
}
$a = array ( 60, 60, 120 );
$n = sizeof( $a );
if (checkCombinations( $a , $n ))
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
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)
sum += a[j];
else
sum -= a[j];
}
if (sum % (24 * 60) == 0)
return true ;
}
return false ;
}
var a = [ 60, 60, 120 ];
var n = a.length;
if (checkCombinations(a, n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
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).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...