The Indian Job
Problem Statement :
It is the Indian version of the famous heist “The Italian Job”. N robbers have already broken into the National Museum and are just about to get inside the main vault which is full of jewels. They were lucky that just as they broke into the museum, the guard was leaving the museum for exactly G minutes. But there are other problems too. The main vault has heat sensors that if at any moment of time there are more than two people present in the vault, the alarm goes off. To collect the jewels, the ith robber needs to be inside the vault for exactly A[i] minutes, 0 <= i < N, in one continuous stretch. As guard will return after G minutes, they have to finish their tasks within G minutes. The robbers want to know if there exists any arrangement such that demands of each robber is satisfied and also they are not caught? Gotchas If a robber goes inside the vault at a time "X" and at the same time another robber comes out, it's equivalent to saying they were never in the vault at the same time. Similarly, when the guard gets inside vault at time G and a robber comes out exactly at time G, the guard will not be able see the robber. Input Format The first line contains an integer T denoting the number of testcases. T testcases follow. Each testcase consists of two lines. First line contains two space separated integers denoting N and G denoting the number of thieves and duration for which guard leaves the museum. The next line contains N space separated numbers where the ith integer, A[i] represents the time the ith robber needs to be in the vault. Constraints 1 <= T <= 20 1 <= N <= 100 0 <= G <= 1000000 (106) 0 <= A[i] <= 100 Output Format For each testcase print YES if there exists such an arrangement or NO otherwise in a newline.
Solution :
Solution in C :
In C++ :
#include <stdio.h>
#include <string.h>
#define NMAX 101
#define VMAX 10001
int A[NMAX];
char ok[VMAX];
int N, G, i, j, sum;
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &N, &G);
for (sum = 0, i = 1; i <= N; i++) {
scanf("%d", &(A[i]));
sum += A[i];
}
memset(ok, 0, sizeof(ok));
ok[0] = 1;
for (i = 1; i <= N; i++)
for (j = VMAX - 1 - A[i]; j >= 0; j--)
if (ok[j]) ok[j + A[i]] = 1;
for (i = 0; i < VMAX; i++)
if (ok[i] && i <= G && (sum - i) <= G) {
printf("YES\n");
break;
}
if (i == VMAX)
printf("NO\n");
}
return 0;
}
In Java :
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Solution {
void solve() throws IOException {
int t=nextInt();
for(int testCase=0;testCase<t;testCase++){
int n=nextInt();
int g=nextInt();
int[] a=new int[n];
int sum=0;
for(int i=0;i<n;i++){
a[i]=nextInt();
sum+=a[i];
}
if(g>=sum){
out.println("YES");
continue;
}
if(sum>2*g){
out.println("NO");
continue;
}
boolean[][] dp=new boolean[n+1][g+1];
dp[0][0]=true;
for(int i=0;i<n;i++){
for(int j=0;j<g;j++)
if(dp[i][j]){
dp[i+1][j]=true;
if(j+a[i]<=g)dp[i+1][j+a[i]]=true;
}
}
int max=0;
for(int i=g;i>=0;i--)
if(dp[n][i]){
max=i;
break;
}
if(sum-max<=g){
out.println("YES");
}
else
out.println("NO");
}
}
public static void main(String[] args) throws IOException {
new Solution().run();
}
void run() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
// reader = new BufferedReader(new FileReader("input.txt"));
tokenizer = null;
out = new PrintWriter(new OutputStreamWriter(System.out));
// out = new PrintWriter(new FileWriter("output.txt"));
solve();
reader.close();
out.flush();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter out;
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
In C :
#include <stdio.h>
#include <string.h>
int dp[101][5001];
int main()
{
int T, N;
int G;
int A[100];
scanf("%d", &T);
for(int i=0; i<T; i++)
{
int sum = 0;
int s;
memset(A, 0, sizeof(A));
memset(dp, 0, sizeof(dp));
scanf("%d%d", &N, &G);
for(int j=1; j<=N; j++)
{
scanf("%d", &A[j]);
sum += A[j];
}
dp[0][0] = 1;
/*for(int k1=1; k1<=N; k1++)
for(int k2=k1; k2>=1; k2--)
for(s=1; s<=(sum>>1); s++)
{
if(s >= A[k1] && dp[k2-1][s-A[k1]])
dp[k2][s] = 1;
}
for(int k=2; k<=N; k++)
for(s=1; s<=sum/2; s++)
if(dp[k-1][s])
dp[k][s] = 1; */
for(int k=1; k<=N; k++)
for(s=0; s<=sum/2; s++)
{
if(s >= A[k])
dp[k][s] = dp[k-1][s-A[k]] || dp[k-1][s];
else
dp[k][s] = dp[k-1][s];
}
for(s=(sum>>1); s>=1 && !dp[N][s]; s--) ;
if(sum/2 > s)
s = sum - s;
if(s > G)
printf("NO\n");
else
printf("YES\n");
}
}
In Python3 :
import itertools
def nzerolast(l):
for i in range(len(l)-1, -1, -1):
if l[i] > 0:
return i
return 0
T = int(input())
for _ in range(T):
N, G = map(int, input().split())
L = list(itertools.repeat(0, G + 1))
nl = list(map(int, input().split()))
L[0] = 1
for num in nl:
for g in range(G, num - 1, -1):
L[g] += L[g - num]
can = nzerolast(L)
print("YES" if sum(nl) - can <= G else "NO")
View More Similar Problems
Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →