Below is a code to determine whether or not can an array of numbers can be divided into two arrays, with each array holding the same sum of numbers. for example: {1, 3 ,2, 6} can be divided into {6} and {1,2,3}, therefore return true while {1,5,7} can't be divided into two, balanced array, therefore return false
public boolean canBalance(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = 0; j < i; j++) sum += nums[j];
for (int j = i; j < nums.length; j++) sum -= nums[j];
if (sum == 0) return true;
}
return false;
}
it's an accepted answer for an exercise in codingbat, I don't understand this piece in particular:
for (int j = 0; j < i; j++) sum += nums[j];
for (int j = i; j < nums.length; j++) sum -= nums[j];
doesn't for iteration usually starts with { and ends with } ? and how come if sum == 0 means it can be balanced? I have tried noting it down on piece of paper with array of {1,3,2,6} and had sum of 26, which returns false, where it's obvious that {1,3,2,6} should return true.
I think I misread the code, I don't know which, though. Or maybe the algorithm is false, but it was accepted in codingbat
This is a recursive solution to the problem, one non recursive solution could use a helper method to get the sum of indexes 0 to a current index in a for loop and another one could get the sum of all the elements from the same current index to the end, which works. Now if you wanted to get the elements into an array and compare the sum, first find the point (index) which marks the spilt where both side's sum are equal, then get a list and add the values before that index and another list to go after that index.
Here's mine (recursion), which only determines 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. Worry about indexOutOfBounds, which can easily happen in recursion, a slight mistake could prove fatal and yield a lot of exceptions and errors.
//non recursively
I wrote a standalone method to sum parts of an array and used this in my solution to get the simplest method I can see possible. If anyone has any comments, please join in. I appreciate the comments.
When there aren't any curly braces around around code after a for statement in Java, the very next line is the only line processed as part of the for statement. In this case, the for is like a function that calls the next line of code, which is a for statement that then calls the next line of code. So the first for calls the second for which evaluates to see if the two sides are the same, and if they aren't then it kicks back up to the second for which continues incrementing itself until it finishes and then it returns to the first for, which increments, and calls the second for... and so on. That code seems like it's partially broken, though, since it would have to have all numbers in numeric order, and it doesn't check anything in the middle.
For instance:
{1, 2, 3, 1} //evaluates to true because 1-1=0, although it should be false
{6, 2, 2, 3} //evaluates to true because 6-3-2=0, although it should be false
{2, 3, 4, 6} //evaluates to true because 2+3-6=0, although it should be false
I guess 'dividing' in the original question does not allow re-ordering of the array. So, you simply break the array at a particular position, and we will have the left side and the right side of the array. Numbers in each side should have the same sum.
The index (i) of outer loop is the breaking position.
The first inner loop sums the left side of the array.
The second inner loop subtracts elements of the right side from the sum of the left array.
If the final result is zero, it means the sum of left side and right side is the same. Otherwise, continue the outer loop and examine other breaking position until we find the right breaking position.
Approaching this problem with DP solution.
//Create a 2D array and fill in bottom up manner //DP[totalsum / 2 + 1] [ length of array + ]
The two for-loops are for weighing the two parts of the array, to find the array balancing point of an array.
Think of it like this:
You have a empty balance scale, in the first iteration of the outer for loop, i is zero.
It comes to first for loop, here j is 0 and i is 0
i < j
is false, so it doesn't enter the first for-loop and it goes into the second for-loop and subtracts all the numbers from sum.From second iteration onwards of the outer for-loop, it starts entering the first for-loop and starts adding the elements of the array one-by-one to the sum.
In pictures, it is like starting with an empty balance scale, adding all the elements into the second scale, and moving one by one element to the first scale, like this:
In the end, if the sum is zero, then the array can be balanced, so return true. If the sum isn't 0, it's unbalanced.
The values in the are balanced by the loops like this:
Iteration of outer for loop when i is 0
Loop 2 -> i(0) j(0) Subtract 1, sum is -1
Loop 2 -> i(0) j(1) Subtract 3, sum is -4
Loop 2 -> i(0) j(2) Subtract 2, sum is -6
Loop 2 -> i(0) j(3) Subtract 6, sum is -12
Iteration of outer for loop when i is 1
Loop 1 -> i(1) j(0) Add 1, sum is 1
Loop 2 -> i(1) j(1) Subtract 3, sum is -2
Loop 2 -> i(1) j(2) Subtract 2, sum is -4
Loop 2 -> i(1) j(3) Subtract 6, sum is -10
Iteration of outer for loop when i is 2
Loop 1 -> i(2) j(0) Add 1, sum is 1
Loop 1 -> i(2) j(1) Add 3, sum is 4
Loop 2 -> i(2) j(2) Subtract 2, sum is 2
Loop 2 -> i(2) j(3) Subtract 6, sum is -4
Iteration of outer for loop when i is 3
Loop 1 -> i(3) j(0) Add 1, sum is 1
Loop 1 -> i(3) j(1) Add 3, sum is 4
Loop 1 -> i(3) j(2) Add 2, sum is 6
Loop 2 -> i(3) j(3) Subtract 6, sum is 0
Final result is true, therefore the array can be balanced
Code:
If you want to allow shuffling between the elements of the array, you can use recursion as follows (comments are self-explanatory)