Below is my code. I m getting wrong answer . Can someone point out where i m getting it wrong?
#include <iostream>
using namespace std;
int main() {
int T;
cin>>T;
while(T--){
bool flag = false;
long long n,c,x;
cin>>n>>c;
for(int i=0;i<n;i++){
cin>>x;
if(x>c){ //if cant fullfil the need ,break and print No
flag = true;
break;
}
c -= x;
}
if(flag)
cout<<"No";
else
cout<<"Yes";
if(T)
cout<<"\n";
}
return 0;
}
int N = Integer.parseInt(r.readLine());
int C = Integer.parseInt(r.readLine());
The error is over here. You’re reading a line of String which contains multiple numbers and converting it into an integer.
For eg. if input it “10 15”, you’re doing “int N = 10 15”.
I hope that you get what I’m saying.
This is my solution, I hope it helps:
import java.util.;
import java.lang.;
import java.io.;
/ Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int j=0;j<T;j++)
{
// elecan stores number of elephants and number of candies
// candyHappy stores number of candies required by elephants to be happy
// both in string format which is then converted into a integer array
String elecan = br.readLine();
int arr[] = new int[2];
String[] strs = elecan.trim().split(“\s+”);
for (int i = 0; i < strs.length; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
//arr[0] is number of elephants and arr[1] is number of candies available
String candyHappy = br.readLine();
int can[] = new int[arr[0]];
String[] str = candyHappy.trim().split("\\s+");
int sum =0;
for (int i = 0; i < str.length; i++) {
can[i] = Integer.parseInt(str[i]);
sum += can[i];
if(sum>arr[1]){
System.out.println("No");
return;
}
}
System.out.println("Yes");
}
}
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int n=0,c=0;
boolean flag=true;
for(int i=0;i<t;i++){
n=sc.nextInt();
c=sc.nextInt();
for(int j=0;j<n;j++){
c=c-sc.nextInt();
if(c<0){
flag=false;
break;
}
}
if(flag==false){
System.out.println("No");
}else{
System.out.println("Yes");
}
flag=true;
}
}
}
T=int(input())
for i in range(T):
N,C=map(int,input().split())
A=[]
A=list(map(int,input().split(’ ')))
sum=C
flag=0
for j in range(N):
sum=sum-A[j]
if(sum<0):
flag=1
break;
if(flag==0):
print(‘Yes’)
else:
print(‘No’)
int main(void) {
// your code goes here
int satisfactionCandies;
int noOfElephants[10],test,noOfCandies;
int i;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
scanf("%d",&noOfElephants[i]);
while t:
total=0
n,c=map(int,input().split())
k=input().split()
for i in range(n):
total+=int(k[i])
if total<=c:
print('YES')
else:
print('NO')
t=t-1```
# cook your dish here
t=int(input())
while t:
total=0
n,c=map(int,input().split())
k=input().split()
for i in range(n):
total+=int(k[i])
if total<=c:
print('YES')
else:
print('NO')
t=t-1