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 :



title-img


                            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

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →

Simple Text Editor

In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,

View Solution →

Poisonous Plants

There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan

View Solution →

AND xor OR

Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value

View Solution →