Actually, you are using two nested loop which is having O(n^2) complexity but this can be easily done in O(n) time using one for loop.
Below are steps which will help you to do the same in O(n) time-
You can drive an equation by using a little bit of mathematics.
Assume we have an array which is the random array {3,7,5,10,2,7,4,2} so, in that, that element exists such that the sum of the left side of all the elements is equal to the sum of the right side all the elements.
I am assuming that element is represented by y and it exists in between somewhere. so some of the left side of the element is represented by x as I said the sum of all the elements towards the left side is equal to the sum of all the elements towards the right side of that element. so right side sum also can be represented by x.So by seeing this, we can easily say the sum of all elements presents inside the array should be equal to x + y + x.
x + y + x = sum of all the elements
2 x + y=sum of all the elements
2 x = sum of all the elements - y ---> eq 1
if we have x and y such that this equation holds true. It means, there is one element exist correct because in this equation we have to unknowns x and y. so we have to first get the value of x and y and then will place both the values inside the equation and see whether LHS is equals to RHS or not? LHS equals to RHS it means there is some element exist inside the array where the sum of all the elements towards the left side of the element is equal to the right side of the element. Let’s take one example array.
{3,7,5,10,2,7,4,2}
first, I will calculate the sum of all elements.
sum of all the elements= 3+7+5+10+2+7+4+2 sum of all the elements= 40
replace this in eq 1 then you will get below eq
2x =40 - y --> eq 2
as we are not aware of the y but y that's for sure y will belong to any one of the elements which are present inside the array. so will take one element at a time from the array and replace y with that element like that x also. we will take the x value based on y whatever it comes and replace in this question and see whether LHS equals to RHS or not. if we found any such pair of x and y where LHS equals to RHS. It means, we have that element inside the array which holds this criterion true and we will return YES.
for first iteration-
{3,7,5,10,2,7,4,2}
y=3, x=0
just replace both values in eq 2 and you can seed
0 is not equal to 37
now move the pointer ahead try this time
y=7, x=0+3=3
just replace both values in eq 2 and you can seed
6 is not equal to 33
....so do the same until you find that element y which satisfy this condition.
now skipping next iterating with y=5 and trying for y=10 know
if y=10 ,x=3+7+5=15
just replace both values in eq 2 and you can seed
30 is equal to 30. It means this the element(y) which we are looking for and where left sum is equal to the right sum.
Here is the code which passes 100% test case.
static String balancedSums(List<Integer> arr) {
int x = 0;
int sum = 0;
for (int a : arr) {
sum += a;
}
for (int y : arr) {
if (2 * x == sum - y) {
return "YES";
}
x = x + y;
}
return "NO";
}
Still having doubt you can check out the video tutorial here.