#include <iostream>
int sum(int array[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += array[i];
}
return sum;
}
bool candiesLeft(int array[], int size, int candies) {
for (int i = 0; i < size; ++i) {
candies -= array[i];
}
if (candies > 0) {
return true;
} else {
return false;
}
}
int main() {
//Read in test cases
int tc = 0;
std::cin >> tc;
//Decrement test cases
while(tc--) {
//Read in number of elephants
int n = 0;
std::cin >> n;
//Read in number of candies
int c = 0;
std::cin >> c;
//n elephants
int a[n];
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
int candySum = sum(a, n);
if (candySum > c) {
std::cout << "No" << std::endl;
break;
}
bool result = candiesLeft(a, n, c);
if (result) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
}
return 0;
}
Link: LECANDY Problem - CodeChef
If the sum of the candies the elephants want is greater than the candies we have, the output should already be no, right?
I had a function called candiesLeft that returns a boolean value. If it is true, then we can give all elephants their candies, hence output Yes. If it returns false, then we output No.
1 Like
Nvm, I got it correct