给定N秒。任务是检查是否有可能从12’0时钟开始并仅增加或减去给定的秒数回到12。我们需要只使用给定的所有秒数一次,我们可以添加元素也可以减去元素。
例子:
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
简单方法:生成所有可能的组合以解决上述问题。因此,生成N个数的幂集。检查任何人的总和%(24 * 60)是否等于零,如果等于零,则有可能不等于零。
下面是上述方法的实现:
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[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 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
输出:
YES
如果仔细研究,我们会发现该问题基本上是分区问题的一种变体。因此,我们可以使用动态编程对其进行优化(请参阅分区问题的方法2)。
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。