Hi,

I am working on below challenge

http://codingbat.com/prob/p158767

I have not understood the description properly

please advise

I am working on below challenge

http://codingbat.com/prob/p158767

I have not understood the description properly

Given a non-empty array, return true if there is a place to split the array so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.

canBalance([1, 1, 1, 2, 1]) → true//i thought since 1+1 is not equal to 2+1 it should be false?

canBalance([2, 1, 1, 2, 1]) → false//i thought since 2+1 is equal to 2+1 it should be true??

canBalance([10, 10]) → true

please advise

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

front and middle and back

But, they want us to split the array into two pieces

front and back

[1, 1, 1, 2, 1] can balance 1+1+1 == 2 +1

[2, 1, 1, 2, 1] can't balance

[2, 2,1,5] can balance 2+2+1 == 5

My solution has nested for loops . Something like

for( whole array){

for( front){ add them}

for( back){add them}

if front equals back return true

}

break at what index or point? just randomly we can break?

They want us to split the array into two parts that balance.

[1, 1, 1, 2, 1] can balance 1+1+1 == 2 +1

[2, 2,1,5] can balance 2+2+1 == 5

We are search for the condition that front == back

So, we have to search for the balance point.

for( each split point in array){

for( each element from first to split ){ add them up}

for( each element from split to last){add them up}

if front equals back return true

}

For it to be possible the sum of the full array must be ?????

If it is possible then there is no need to search

Sum from the front of the array until FrontSum = ??????

mlmcc

```
public class CanBalance {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ar={1,1,1,2,1};
System.out.println("canBalance value is--->"+canBalance(ar));
}
public static boolean canBalance(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int leftTotal=0;
leftTotal=leftTotal+nums[i];
System.out.println("i is " + i + " leftTotal is " + leftTotal);
for (int j = nums.length-1; j >i; j--) {
int rightTotal=0;
rightTotal=rightTotal+nums[i];
System.out.println("j is " + j + " rightTotal is " + leftTotal);
if(leftTotal==rightTotal){
return true;
}
}
}
return false;
}
}
```

The output isi is 0 leftTotal is 1

j is 4 rightTotal is 1

canBalance value is--->true

Now we see what the machine is doing. It is just comparing the first element with the first element. Which is true for this particular input.

Now let's change input to {1, 2, 3, 1, 0, 1, 3}

The output now is

i is 0 leftTotal is 1

j is 6 rightTotal is 1

canBalance value is--->true

With the debug statements I have added, the code should print the totals in every iteration. Your code just stops after a single iteration. Another thing we can see from the second test, this statement

```
rightTotal=rightTotal+nums[i];
```

is wrong. rightTotal should have equal to 3(at that point). You have i where you should have j.
You are resetting the total to 0 each time through the loop.

The loops should calculate the total from that direction so the reset should be ????

mlmcc

do i need to define ....I don't know. I can't really understand your code(thinking). My thoughts :

in each iteration of outer for loop

add left

add right

if equal then break out

else reset

```
public class CanBalance {
public static void main(String[] args) {
int[] ar={4, 2, 3, 3};
System.out.println("canBalance value is--->"+canBalance(ar));
}
public static boolean canBalance(int[] nums) {
boolean result = false;
int front = 0;
int back = 0;
for(int i = 0; i < nums.length; i++){
for(int f = 0; f < i + 1 ; f++){
front += nums[f];
System.out.println("f is " + f + " front is " + front);
}
for(int b = i + 1; b < nums.length ; b++){
back += nums[b];
System.out.println("b is " + b + " back is " + back);
}
if(front == back){
result = true;
break;
}
else{
front = 0;
back = 0;
}
}
return result;
}
}
```

Output was f is 0 front is 4

b is 1 back is 2

b is 2 back is 5

b is 3 back is 8

f is 0 front is 4

f is 1 front is 6

b is 2 back is 3

b is 3 back is 6

canBalance value is--->true

I stubbornly held to my first thoughts. I wastefully added the front(left) over and over. Your way is better.

add left

start iteration of inner for loop

add right

if equal then return true

Loop inner

Loop outer

return false

mlmcc

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial