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.

Technically, if you've coded things to the problem statement, you don't even need to consider this special case, although it could technically shortcut the rest of your logic.

Exactly. Not only that, but a general solution would handle negatives and 0, too (which isn't specified). A single element isn't a special case, in my thinking, but rather than case where the sum of the left and the sum of the right of the single element are equal (0). shrug

But look at the sample input
2
3
1 2 3
4
1 2 3 3
the result is
NO
YES
if the single element should be YES, the the result should be
YES
YES
NO
YES
YES
right?

Oops i think i misunderstood the test description.
For input
2 -> how many test case will coming
3 -> how many element in the first test case
1 2 3 -> the actual first test case
4 -> how many element in the second test case
1 2 3 4 -> the actual second test case

I thought the 2, 3 and 4 are also test cases :(
But now it's clear to me , thanks #warrior_of_light

// 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 →

Technically, if you've coded things to the problem statement, you don't even need to consider this special case, although it could technically shortcut the rest of your logic.

Exactly. Not only that, but a general solution would handle negatives and 0, too (which isn't specified). A single element isn't a special case, in my thinking, but rather than case where the sum of the left and the sum of the right of the single element are equal (0).

shrugBut look at the sample input

2

3

1 2 3

4

1 2 3 3

the result is

NO

YES

if the single element should be YES, the the result should be

YES

YES

NO

YES

YES

right?

If the given input n (here 3 and 4) is equal to 1, then only we have to print yes.

Oops i think i misunderstood the test description.

For input

2 -> how many test case will coming

3 -> how many element in the first test case

1 2 3 -> the actual first test case

4 -> how many element in the second test case

1 2 3 4 -> the actual second test case

I thought the 2, 3 and 4 are also test cases :(

But now it's clear to me , thanks #warrior_of_light

If n == 1, then definitely answer is YES, for n > 1, you have to check.

// 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.