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

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →