The program asks you to calculate if all the elephants would get the number of candies that they want.
EXPLANATION:
If there are C candies in all, and elephant i needs A[i] candies, then it is possible to serve all the elephants only if there are enough candies available, i.e. the following condition must be satisfied:
C >= A[0] + A[1] + … + A[N-1]
This means you can have a simple loop over the array A to count the sum of the required number of candies by the elephants and then finally comparing it with C to determine the answer. If the above condition is satisfied, answer will be Yes, else the answer will be No (they have to be case-sensitive to avoid WA).
int t,n,i,j;
int A[1000];
long long int c;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
scanf("%lli",&c);
for(j=0;j<n;j++){
scanf("%d",&A[j]);
c=c-A[j];
}
if(c<0){
printf(“No\n”);
}
else{
printf(“Yes\n”);
}
int i,T,ii;
scanf("%d",&T);
for(i=0;i<T;i++) //for test cases
{ int C,N;
scanf("%d %d",&N,&C);
int E[N];
for(ii=0;ii<N;ii++) //assigning candies to elephants
{
scanf("%d",&E[ii]);
if(C>=ii) //for assign only grater or equal index number candy to elephant
{
C=C-E[ii];
}
else {
C=-1;
}
}
if(C >=0)
printf("Yes");
else
printf("No");
}
return 0;
I’m getting NZEC and unable to understand why? Please help
import java.io.IOException;
import java.util.Scanner;
public class Main{
public static void main(String args[]){
lecandy();
}
public static void lecandy(){
//Scanner in = new Scanner(System.in);
try{
java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader (System.in));
int t = Integer.parseInt(r.readLine());
while(t>0){
int N = Integer.parseInt(r.readLine());
int C = Integer.parseInt(r.readLine());
// int Ak[] = new int[N];
int sum = 0;
for(int i=0;i<N;i++){
// Ak[i] = in.nextInt();
sum+=Integer.parseInt(r.readLine());
}
if(sum<=C){
System.out.println("Yes");
}else{
System.out.println( "No");
}
t--;
}
r.close();
}catch(IOException e){
e.printStackTrace();
}
// in.close();
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T > 0){
int N = sc.nextInt();
long C = sc.nextLong();
String result = "Yes";
while(N > 0){
long k = sc.nextLong();
C -= k;
if(C < 0){
result = "No";
break;
}
N--;
}
System.out.println(result);
T--;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of Candies = ");
int c=sc.nextInt();
System.out.print(c);
System.out.print("Enter the number of Elephant = ");
int n=sc.nextInt();
System.out.print(n);
int[] arr1= new int[n];
int l=arr1.length-1;
for(i=0;i<=arr1.length-1;i++)
{
arr1[i]=sc.nextInt();
}
if(c>=l )
System.out.println("true");
else
System.out.println("false");
// System.out.println("Enter the number of Elephant = "+n);
// for(i=0;i<=A[n-1];i++)
// {
// if(c>=A[n-1])
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
for (int i = 0; i < T; i++){
int N;
int C;
cin >> N >> C;
int sum = 0;
for (int k = 0; k < N; k++) {
int temp = 0;
cin >> temp;
sum += temp;
if (sum > C)
break;
}
if (sum > C)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
If, I don’t use break statement within for loop, I get correct result. But, I couldn’t understand why using a break statement will alter the result.
#include <iostream>
using namespace std;
int main()
{
int T;
cin >> T;
for (int i = 0; i < T; i++){
int N;
int C;
cin >> N >> C;
int sum = 0;
for (int k = 0; k < N; k++) {
int temp = 0;
cin >> temp;
sum += temp;
if (sum > C)
break;
}
if (sum > C)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
return 0;
}
If, I don’t use break statement within for loop, I get correct result. But, I couldn’t understand why using a break statement will alter the result.