# 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 12’0 clock and get back to 12 only 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Approach: Generate all possible combinations to solve the above problem. Hence generate the power set of N numbers. Check if any one’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 ` `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 combiantions ` `        ``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); ` ` `  `    ``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 combiantions  ` `            ``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 combiantions ` `        ``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 combiantions  ` `            ``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

 ` `

Output:

```YES
```

If we take a closer look, we can notice that this problem is basically a variation of Partition Problem. So we can optimize it using Dynamic Programming (Please refer method 2 of Partition Problem). My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.