class Solution { public: int countValidSelections(vector& nums) { int n = nums.size(), sum = 0, currSum = 0; for (int i = 0; i < n; i++) { sum += nums[i]; } int ans = 0; for (int i = 0; i < n; i++) { if (currSum == (sum - currSum)) {ans += 2;} else if (abs(sum - (2 * currSum)) == 1) {ans++;} currSum += nums[i]; } return ans; } }; showing wrong ans for first example test case, output should be 2 but coming out to be 4, why?
Thank you! It was really helpful.
could you please improve the audio quality . It would be helpful
class Solution {
public:
int countValidSelections(vector& nums) {
int n = nums.size(), sum = 0, currSum = 0;
for (int i = 0; i < n; i++) {
sum += nums[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (currSum == (sum - currSum)) {ans += 2;}
else if (abs(sum - (2 * currSum)) == 1) {ans++;}
currSum += nums[i];
}
return ans;
}
};
showing wrong ans for first example test case, output should be 2 but coming out to be 4, why?
Can you provide link to your submissions codes please. Thank you