Two for-loops are designed to weigh two parts of an array in order to find an array array array.
Think of it this way:
You have an empty balance scale, in the first iteration of the outer loop, I am zero.
It comes to the first for the loop, here j is 0 and I is 0 i < j false, so it does not enter the first for loop and does not go into the second for loop and does not subtract all numbers from the sum.
Starting from the second iteration of the outer for loop, it begins to introduce the first for loop and begins to add the elements of the array one by one to the sum.
In paintings, it looks like an empty balance scale, adding all the elements to the second scale and moving one element to the first scale, for example:

In the end, if the sum is zero, then the array can be balanced, so return true. If the sum is not 0, it is unbalanced.
The values ββin are balanced by such loops:
Iterate external to the loop when i equals 0
Loop 2 β i (0) j (0) Subtract 1, the sum is -1 Loop 2 β i (0) j (1) Subtract 3, the sum is -4 Loop 2 β i (0) j (2) Subtraction 2, amount -6
Loop 2 β i (0) j (3) Subtract 6, the sum is -12
Iterate external to the loop when i equals 1
Loop 1 β i (1) j (0) Add 1, sum 1 Loop 2 β i (1) j (1) Subtract 3, the sum is -2
Loop 2 β i (1) j (2) Subtract 2, the sum is -4
Loop 2 β i (1) j (3) Subtract 6, the sum is -10
Iterate external to the loop when I equals 2
Loop 1 β i (2) j (0) Add 1, sum 1 Loop 1 β i (2) j (1) Add 3, sum 4
Loop 2 β i (2) j (2) Subtract 2, the sum is 2 Loop 2 β i (2) j (3) Subtract 6, the sum is -4
Iterate external to the loop when I equals 3
Loop 1 β i (3) j (0) Add 1, the sum is 1
Loop 1 β i (3) j (1) Add 3, sum 4
Loop 1 β i (3) j (2) Add 2, the sum is 6
Loop 2 β i (3) j (3) Subtract 6, the sum is 0
The end result is correct, so the array can be balanced
the code:
public class Test { public static void main(String[] args) { int[] test = { 1, 3, 2, 6 }; System.out.println("\nFinal result is "+canBalance(test)); } public static boolean canBalance(int[] nums) { for (int i = 0; i < nums.length; i++) { System.out.println("\nIteration of outer for loop when i is " + i); int sum = 0; for (int j = 0; j < i; j++){ sum += nums[j]; System.out.println("Loop 1 -> i(" +i + ") j("+j + ") Add "+nums[j] + ", sum is "+sum+" "); } for (int j = i; j < nums.length; j++){ sum -= nums[j]; System.out.println("Loop 2 -> i(" +i + ") j("+j + ") Subtract "+nums[j] + ", sum is "+sum+" "); } if (sum == 0) return true; } return false; } }
If you want to allow shuffling between array elements, you can use recursion as follows (comments are self-explanatory)
public class Test { public static void main(String[] args) { int[] original = { 10, 2, 24, 32 }; System.out.println(canDivideArray(original)); } private static boolean canDivideArray(int[] originalArray) { int total = 0; for (int number : originalArray) { total += number; } // check if sum == 2x for any value of x if (total % 2 != 0) { return false; } else { // sum of each half array should be x total /= 2; } return isTotal(originalArray, originalArray.length, total); } private static boolean isTotal(int array[], int n, int total) { // successful termination condition if (total == 0) { return true; } // unsuccessful termination when elements have finished but total is not reached if (n == 0 && total != 0){ return false; } // When last element is greater than total if (array[n - 1] > total) return isTotal(array, n - 1, total); //check if total can be obtained excluding the last element or including the last element return isTotal(array, n - 1, total - array[n - 1]) || isTotal(array, n - 1, total); } }