We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

// java method which passes 6 test cases, 7th test case fails. what is //the problem?
// Complete the balancedSums function below.

static String balancedSums(List<Integer> arr) {
int n = arr.size();
int i = 0;
int j = n-1;
int left = 0;
int right = 0;
while ( i < n && j >= 0)
{
if ( left == right && i == j) {
return "YES";
}
if ( left > right) {
right += arr.get(j);
j--;
} else
{
left += arr.get(i);
i++;
}
}
return "NO";
}

Before returning "NO", just check if left and right are equal or not, if they are equal return "YES". Then it will pass all the test cases. By the way your code is really good.

## Sherlock and Array

You are viewing a single comment's thread. Return to all comments →

// java method which passes 6 test cases, 7th test case fails. what is //the problem? // Complete the balancedSums function below.

Before returning "NO", just check if left and right are equal or not, if they are equal return "YES". Then it will pass all the test cases. By the way your code is really good.

Hi,

Here is the video explanation of my solution with different approach in O(n) time-

https://youtu.be/xPHJH8RL3mo

any feedback or comment would be highly appreciated.