My issue
help me in understanding the logic
My code
# cook your dish here
Problem Link: ATM2 Problem - CodeChef
help me in understanding the logic
# cook your dish here
Problem Link: ATM2 Problem - CodeChef
In the given problem, we need to find if N people can withdraw money. The logic I used here was like this;
Initially ATM has k amount of money. For i-th person to be able to withdraw, the amount in the ATM should not go below zero, (k - a[i] >=0).
If money can be successfully withdrawn, we subtract the amount withdrawn from the available money in the ATM,k. The process is repeated N times.
We print 1 if its possible, otherwise we print 0.
#include <stdio.h>
int main(void)
{
int t,n,k,a[10000],i;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(k-a[i]>=0)
{
k=k-a[i];
printf("1");
}
else
{
printf("0");
}
}
printf("\n");
}
return 0;
}
include <stdio.h>
int main(void) {
// your code goes here
int t;
scanf(“%d”,&t);
for(int i=0;i<t;i++){
int n,k;
scanf(“%d %d”,&n,&k);
int arr[n];
for(int j=0;j<n;j++){
scanf(“%d”,&arr[j]);
}
char ch[n];
for(int j=0;j<n;j++){
if(k>=arr[j]){
k=k-arr[j];
ch[j]=‘1’;
}
else ch[j]=‘0’;
}
printf(“%s\n”,ch);
}
return 0;
}
first take input of the array then initilize an string, after that check the condition if money in the ATM is greater than or equal to requirement or not. if it is then give it and subtract from the total money left and take 1 input in the string, otherwise 0 in the string.