ATM Machine Codechef Solution|Problem Code: ATM2

ATM Machine Codechef Solution: Hello coders, today we are going to solve ATM Machine Codechef Solution.

Codechef solutions

Problem

There is an ATM machine. Initially, it contains a total of K units of money. N people (numbered 11 through NN) want to withdraw money; for each valid ii, the ii-th person wants to withdraw Ai units of money.

The people come in and try to withdraw money one by one, in the increasing order of their indices. Whenever someone tries to withdraw money, if the machine has at least the required amount of money, it will give out the required amount. Otherwise, it will throw an error and not give out anything; in that case, this person will return home directly without trying to do anything else.

For each person, determine whether they will get the required amount of money or not.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains two space-separated integers N and K.
  • The second line contains NN space-separated integersA1,A2,…,AN.

Output

For each test case, print a single line containing a string with length N. For each valid ii, the ii-th character of this string should be ‘1’ if the ii-th person will successfully withdraw their money or ‘0’ otherwise.

Constraints

  • 1≤T≤1001≤T≤100
  • 1≤N≤1001≤N≤100
  • 1≤Ai≤1,000,0001≤Ai≤1,000,000 for each valid ii
  • 1≤K≤1,000,000

Example

Sample Input 1 

2
5 10
3 5 3 2 1
4 6
10 8 6 4

Sample Output 1 

11010
0010

ATM Machine- CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int tc = 0; tc < T; tc++) {
			int N = sc.nextInt();
			int K = sc.nextInt();
			int[] A = new int[N];
			for (int i = 0; i < A.length; i++) {
				A[i] = sc.nextInt();
			}
			System.out.println(solve(A, K));
		}
		sc.close();
	}
	static String solve(int[] A, int K) {
		StringBuilder result = new StringBuilder();
		for (int element : A) {
			if (element <= K) {
				result.append('1');
				K -= element;
			} else {
				result.append('0');
			}
		}
		return result.toString();
	}
}

ATM Machine- CodeChef Solution in CPP

#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {int n,k;
    cin>>n>>k;
    int a[n];
    for(int i=0;i<n;i++)
    {cin>>a[i];
    }
    for(int i=0;i<n;i++)
    {if(k>=a[i])
    {k=k-a[i];
    cout<<'1';
    }
    else
    {cout<<'0';
    }
    }
    cout<<endl;
    }
}

ATM Machine- CodeChef Solution in Python

for t in range(int(input())):
    n,k=map(int,input().split())
    n=list(map(int,input().split()))
    ans=''
    for i in n:
        if k-i>=0:
            ans+='1'
            k-=i
        else:
            ans+='0'
    print(ans)

Disclaimer: The above Problem (ATM Machine) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment