How to write an "all these numbers are different" condition in Java? - java
OK, I have this problem to solve but I can’t program it in Java correctly. See the picture below, you’ll see a 6 pointed star were every point and intersection of lines is a letter.
The assignment is to position the numbers 1 to 12 in such a way that the sum of all lines of four balls is 26 and the sum of all the 6 points of the star is 26 as well.
This comes down to:
(A+C+F+H==26)
(A+D+G+K==26)
(B+C+D+E==26)
(B+F+I+L==26)
(E+G+J+L==26)
(H+I+J+K==26)
(A+B+E+H+K+L==26)
So I started programming a program that would loop through all options brute forcing a solution. The loop is working, however, it now shows solutions where one number is used more than once, which is not allowed. How can I make it in the code that it also checks whether all variables are different or not?
if ((A!= B != C != D != E != F != G != H != I != J != K != L)
I tried the above, but it doesn't work, because it says:
incomparable types: boolean and int.
How can I make a check within 1 or a small statement for whether or not all the numbers are different?
(instead of making a nested 12*12 statement which checks every variable combination)
This is my code so far:
public class code {
public static void main(String[] args){
for(int A = 1; A < 13; A++){
for(int B = 1; B < 13; B++){
for(int C = 1; C < 13; C++){
for(int D = 1; D < 13; D++){
for(int E = 1; E < 13; E++){
for(int F = 1; F < 13; F++){
for(int G = 1; G < 13; G++){
for(int H = 1; H < 13; H++){
for(int I = 1; I < 13; I++){
for(int J = 1; J < 13; J++){
for(int K = 1; K < 13; K++){
for(int L = 1; L < 13; L++){
if ((A+C+F+H==26) && (A+D+G+K==26) && (B+C+D+E==26) && (B+F+I+L==26) && (E+G+J+L==26) && (H+I+J+K==26) && (A+B+E+H+K+L==26)){
if ((A= C != D != E != F != G != H != I != J != K != L)){
System.out.println("A: " + A);
System.out.println("B: " + B);
System.out.println("C: " + C);
System.out.println("D: " + D);
System.out.println("E: " + E);
System.out.println("F: " + F);
System.out.println("G: " + G);
System.out.println("H: " + H);
System.out.println("I: " + I);
System.out.println("J: " + J);
System.out.println("K: " + K);
System.out.println("L: " + L);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
If I get it correctly, you want to check if all A to L are unique. So just put them in a set and find the size of the set:
if ((new HashSet<Integer>(
Arrays.asList(A, B, C, D, E, F, G, H, I, J, K, L)))
.size() == 12) {
//do your stuff
}
I strongly advise using recursion instead, which would vastly simplify the code. Do something like this:
function generate(set used, array list):
if list.size() == 12:
if list matches criteria:
yield list as solution
else:
for next = 1; next < 13; next++:
if next not in used:
used.add(next)
generate(used, list + next)
used.remove(next)
However, to answer you question directly: You can throw all the values into a set and check that it's size is equal to the number of items you threw in. This works because a set will count duplicates as one.
Before looking for a good solution for you, I would like to help with the error you get.
if ((A= C != D != E != F != G != H != I != J != K != L)){
This line does not makes much sense. The first thing the compiler will check is:
if (A=C)
You probably wanted to code if (A!=C), but let's consider what you really type. A=C is an attribution, so A will receive C value.
Then, the compiler will go on. After attributing C's value to A, it will check the comparison:
if (A=C != D)
This will compare A's value to D, which will result in a boolean -- let's say that the result is false.
The next comparison would be:
if (false != E)
At this point, there is a comparison between a boolean and an int, hence the error incomparable types: boolean and int..
Well, as you need to check wheter your numbers are unique, a nice solution would be the one proposed by #abhin4v.
Your nested loops will execute 12^12 = 8.91610045E12 IF-Statements, many of them invalid because of wrong combinations of numbers. You need permutations of 1,2,3,..,12 as candidates of your bruteforcing approach. The number of permutations of 12 Elements is 12!= 479 001 600, so the bruteforcing will be much faster I guess. With only generating valid permutations you don't need any check for valid combinations.
Here is some sample code, the code in nextPerm() is copied and modified from Permutation Generator :
import java.util.Arrays;
public class Graph26 {
private static final int A = 0;
private static final int B = 1;
private static final int C = 2;
private static final int D = 3;
private static final int E = 4;
private static final int F = 5;
private static final int G = 6;
private static final int H = 7;
private static final int I = 8;
private static final int J = 9;
private static final int K = 10;
private static final int L = 11;
private final static boolean rule1(final int[] n) {
return n[A] + n[C] + n[F] + n[H] == 26;
}
private final static boolean rule2(final int[] n) {
return n[A] + n[D] + n[G] + n[K] == 26;
}
private final static boolean rule3(final int[] n) {
return n[H] + n[I] + n[J] + n[K] == 26;
}
private final static boolean rule4(final int[] n) {
return n[B] + n[C] + n[D] + n[E] == 26;
}
private final static boolean rule5(final int[] n) {
return n[B] + n[F] + n[I] + n[L] == 26;
}
private final static boolean rule6(final int[] n) {
return n[E] + n[G] + n[J] + n[L] == 26;
}
private final static boolean rule7(final int[] n) {
return n[A] + n[B] + n[E] + n[H] + n[K] + n[L] == 26;
}
private final static boolean isValid(final int[] nodes) {
return rule1(nodes) && rule2(nodes) && rule3(nodes) && rule4(nodes)
&& rule5(nodes) && rule6(nodes) && rule7(nodes);
}
class Permutation {
private final int[] o;
private boolean perms = true;
public boolean hasPerms() {
return perms;
}
Permutation(final int[] obj) {
o = obj.clone();
}
private int[] nextPerm() {
int temp;
int j = o.length - 2;
while (o[j] > o[j + 1]) {
j--;
if (j < 0) {
perms = false;
break;
}
}
if (perms) {
int k = o.length - 1;
while (o[j] > o[k]) {
k--;
}
temp = o[k];
o[k] = o[j];
o[j] = temp;
int r = o.length - 1;
int s = j + 1;
while (r > s) {
temp = o[s];
o[s] = o[r];
o[r] = temp;
r--;
s++;
}
}
return o.clone();
}
}
public static void main(final String[] args) {
int[] nodes = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
final Graph26 graph = new Graph26();
final Permutation p = graph.new Permutation(nodes);
int i = 0;
while (p.hasPerms()) {
if (isValid(nodes)) {
System.out.println(Arrays.toString(nodes));
}
i++;
nodes = p.nextPerm();
}
System.out.println(i);
}
}
Related
how to initialize result in method?
result isn't initialized, how to do it in this method?(to preserve the functionality of the code) static int maxNumbers(int r, int s) { int result; int[] rk = new int[r]; for (int i = 0; i < rk.length; i++) { if (s > 1) { rk[i] = (s - 1) + 1; s--; } else if (s == 1) { rk[i] = (s + 1) + 1; s = 0; } else { rk[i] = 1; } } for (int i = 0; i < rk.length; i++) { result = rk[0] * 2 * rk[i++]*2; } return result; }
For the integer primitive and regarding your example you can simply write int result = -1;. Although you set your result variable in the 2nd for loop, the compiler cannot guarantee that this for loop actually loops and therefore the variable will be set. So in the end you should check the result of the method.
Program to find the number of 'a' in a string that is repeated n characters?
I am writing a program to find the number of 'a' in a given string that is repeated. For example, the call findAmountA("aba", 7) means that it finds the number of 'a' in the string "aba" repeated for 7 characters. So "abaabaa" is the final string, so that call would return 5. Without actually making the string 7 characters (so calls for 1,000,000 characters would not take so long), how would I use mathematics to accomplish this task? I cannot get further than this, as I have been trying to troubleshoot this for a while. Keep in mind I am a beginner Java programmer (Student) and do not want to use any advanced/fancy syntax that I would not learn in high school. Thank you! public class AInString { public static void main(String[] args) { boolean a = findAmountA("aba", 10) == 7; boolean b = findAmountA("a", 100) == 100; boolean c = findAmountA("abca", 10) == 5; boolean d = findAmountA("", 10) == 0; boolean e = findAmountA("abcaa", 1000000) == 600000; boolean f = findAmountA("abc", 0) == 0; boolean g = findAmountA("bcd", 10) == 0; System.out.println(a && b && c && d && e && f && g); } public static int findAmountA(String word, int n) { String s = word; if(s.length() == 0 || aInWord(word) == 0) { return 0; }else { int a = (aInWord(s)); return a; } } public static int aInWord(String word) { String s = word; int aInWord = 0; for(int i = 0; i < word.length(); i++) { if(s.charAt(i) == 'a') { aInWord++; } } return aInWord; } }
Let's say your short string w has N copies of 'a' in it. Then the result string will consist of K copies of w followed by a possibility empty “tail” string. The value of K can be determined by integer-dividing the number of 'a's in the target string by N. Then the number t of 'a's in the “tail” would be equal to the remainder of the division. Now you can print K copies of w followed by the shortest prefix of 'w' containing t 'a's.
Divide the target length by the input length: for the example: 7 / 3 = 2 remainder 1 2 the number of "full copies" of the entire input string you will use. So, find the number of "a"s in the entire string, multiply by 2. You will take the first 1 character of the input to make up the remainder of the 7 characters. Count the number of "a"s in that substring. Simply add these two numbers together. int total = count(input, "a") * targetLength / input.length() + count(input.substring(0, targetLength % input.length()), "a"); where count(input, c) is some method to count the number of occurrences of c in input.
Now that you've counted the occurrences of a char a in a string word, you can count the occurrences of the char in the string extended n characters with: return n / word.length() * aInWord(word) + aInWord(word.substring(0, n % word.length())); n / word.length() gives the number of full repeats of the string that fit into n. Multiplying this by the count of aInWord(word) gives the count of a in repeats of word that fit cleanly into n. The rest is a matter of finding the number of repeats in the substring of word that doesn't fit cleanly into n using the % modulus operator to find the size of the partial substring (if any). Adding the two counts together produces the total number of occurrences in the extended string. Here is a clean version which avoids duplicate variables, extra conditionals and generalizes methods to maximize reusability: class Main { public static void main(String[] args) { assert findAmount("aba", 10, "a") == 7; assert findAmount("a", 100, "a") == 100; assert findAmount("abca", 10, "a") == 5; assert findAmount("", 10, "a") == 0; assert findAmount("abcaa", 1000000, "a") == 600000; assert findAmount("abc", 0, "a") == 0; assert findAmount("bcd", 10, "a") == 0; System.out.println("tests passed"); } public static int findAmount(String word, int n, String target) { if (word.length() == 0) { return 0; } return n / word.length() * count(target, word) + count(target, word.substring(0, n % word.length())); } public static int count(String target, String s) { return s.length() - s.replace(target, "").length(); } } Try it!
I made some changes in your code, take a look: public static void main(String[] args) { int a = findAmountA("aba", 10); // 7 int b = findAmountA("a", 100); // 100; int c = findAmountA("abca", 10); //5; int d = findAmountA("", 10); //0; int f = findAmountA("abc", 0); //0; int g = findAmountA("bcd", 10); //0; System.out.println(a + " " + b + " " + c + " " + d + " " + f + " " + g); } public static int findAmountA(String word, int n) { if (word.length() < n) { for (int i=0; i<word.length(); i++) { while (word.length() < n) { word = word + word.charAt(i); break; } } } else if (word.length() > n) { for (int i=0; i<word.length(); i++) { word = word.substring(0, n); } } else { return aInWord(word); } return aInWord(word); } public static int aInWord(String word) { String s = word; int aInWord = 0; for(int i = 0; i < word.length(); i++) { if(s.charAt(i) == 'a') { aInWord++; } }
Thank you all for your help, using substrings I found an answer: public class AInString { public static void main(String[] args) { boolean a = findAmountA("aba", 10) == 7; boolean b = findAmountA("a", 100) == 100; boolean c = findAmountA("abca", 10) == 5; boolean d = findAmountA("", 10) == 0; boolean e = findAmountA("abcaa", 1000000) == 600000; boolean f = findAmountA("abc", 0) == 0; boolean g = findAmountA("bcd", 10) == 0; System.out.println(a && b && c && d && e && f && g); } public static int findAmountA(String word, int n) { String s = word; if(s.length() == 0 || aInWord(s) == 0) { return 0; }else { int a = aInWord(s)*(n/s.length()); int b = n % s.length(); return a + aInWord(s.substring(0, b)); } } public static int aInWord(String word) { String s = word; int aInWord = 0; for(int i = 0; i < word.length(); i++) { if(s.charAt(i) == 'a') { aInWord++; } } return aInWord; } }
Printing integer from 2D array using nested FOR Loops
I'm having a bit of a problem with a piece of Java code, part of an AI project. The programm is supposed to take a 11x13 2d array representing a maze. The printed maze we are given uses characters for each cell, but for ease of use I've converted it to integers using a mnemonic code. My problem is when I try to print the 2d integer array to the screen, to check eveything is OK, I get zeros at every cell, even though I have a check function in place which parses the array cell-by-cell checking for incorrect values. The project is currently composed of 2 files. The main file - function (AISemesterProject.java) and a file that will implement the UCS algorithm in the future (UCS.java) AISemesterProject.java package aisemesterproject; public class AISemesterProject { public static void main(String[] args) { UCS a = new UCS(); a.checkArrayInt(); a.printInt(); } } UCS.java package aisemesterproject; import java.util.Arrays; public class UCS { int row = 11; int col = 13; int[][] array_int = new int[row][col]; public UCS() { // Lets assume // x = 0 // e = 1 // d = 2 // s = 8 // g = 9 int[][] array_int = new int[][] { {0,1,0,1,1,1,1,0,0,1,0,9,0}, {1,1,1,2,0,1,1,0,0,1,2,1,0}, {0,1,0,1,1,1,1,0,0,1,0,0,0}, {8,1,2,0,1,2,0,1,1,2,1,1,1}, {0,0,1,1,0,1,1,1,0,0,0,0,0}, {1,2,1,0,1,0,1,1,0,0,1,1,1}, {0,1,2,0,1,0,0,2,1,1,2,1,9}, {1,0,1,1,2,1,1,1,0,1,1,1,1}, {1,1,2,1,1,0,0,1,0,0,0,0,0}, {0,0,1,1,1,0,0,1,1,1,1,1,2}, {0,0,1,0,0,1,1,1,0,9,0,1,1} }; } public void checkArrayInt() { int i = 0, j = 0; boolean checker = false; for(i = 0; i < row; i++) { for(j = 0; j < col; j++) { if(!(array_int[i][i] == 0 || array_int[i][j] == 1 || array_int[i][j] == 2 || array_int[i][j] == 8 || array_int[i][j] == 9)) { checker = true; System.out.print("Error at Row:" + i + " Column:" + j + "\n"); } } } if(checker == false) { System.out.print("Array OK... \n"); } } public void printInt() { int i = 0, j = 0; //System.out.println(Arrays.deepToString(array_int)); for(i = 0; i < row; i++) { System.out.print("Row " + (i + 1) + ":"); for(j = 0; j < col; j++) { System.out.print(" " + String.valueOf(array_int[i][j])); //System.out.print(" " + Integer.toString(array_int[i][j])); //System.out.printf(" %d", array_int[i][j]); //System.out.print(" " + array_int[i][j]); } System.out.print("\n"); } } } Output As you can see the output is not what I expected and I have tried 4 different methods for the print (1 active, 3 commented) but the result is always the same. Anyone have an idea what am I missing or doing wrong? Thanks for your time.
It looks like you have a scope issue... int[][] array_int = new int[row][col]; public UCS() { // Lets assume // x = 0 // e = 1 // d = 2 // s = 8 // g = 9 array_int = new int[][] { {0,1,0,1,1,1,1,0,0,1,0,9,0}, {1,1,1,2,0,1,1,0,0,1,2,1,0}, {0,1,0,1,1,1,1,0,0,1,0,0,0}, {8,1,2,0,1,2,0,1,1,2,1,1,1}, {0,0,1,1,0,1,1,1,0,0,0,0,0}, {1,2,1,0,1,0,1,1,0,0,1,1,1}, {0,1,2,0,1,0,0,2,1,1,2,1,9}, {1,0,1,1,2,1,1,1,0,1,1,1,1}, {1,1,2,1,1,0,0,1,0,0,0,0,0}, {0,0,1,1,1,0,0,1,1,1,1,1,2}, {0,0,1,0,0,1,1,1,0,9,0,1,1} }; you already created the array as a class level variable
Your constructor is setting the local variable array_int. This local variable overshadows the field with the same name, and thus it never sees the array you're assigning to it. You should make sure that you're assigning to the field, which can most easily be done by removing the int[][] word from your constructor.
Thank you everyone. I moved the declatarion from the constructor to the variable at the beggining and it worked. package aisemesterproject; import java.util.Arrays; public class UCS { int row = 11; int col = 13; // Lets assume // x = 0 // e = 1 // d = 2 // s = 8 // g = 9 int[][] array_int = new int[][] { {0,1,0,1,1,1,1,0,0,1,0,9,0}, {1,1,1,2,0,1,1,0,0,1,2,1,0}, {0,1,0,1,1,1,1,0,0,1,0,0,0}, {8,1,2,0,1,2,0,1,1,2,1,1,1}, {0,0,1,1,0,1,1,1,0,0,0,0,0}, {1,2,1,0,1,0,1,1,0,0,1,1,1}, {0,1,2,0,1,0,0,2,1,1,2,1,9}, {1,0,1,1,2,1,1,1,0,1,1,1,1}, {1,1,2,1,1,0,0,1,0,0,0,0,0}, {0,0,1,1,1,0,0,1,1,1,1,1,2}, {0,0,1,0,0,1,1,1,0,9,0,1,1} }; public UCS() { // Lets assume // x = 0 // e = 1 // d = 2 // s = 8 // g = 9 // Array initialization outside the constructor scope } public void checkArrayInt() { int i = 0, j = 0; boolean checker = false; for(i = 0; i < row; i++) { for(j = 0; j < col; j++) { if(array_int[i][j] == 0) //Check for 0 = x { checker = false; } else if(array_int[i][j] == 1) //Check for 1 = e { checker = false; } else if(array_int[i][j] == 2) //Check for 2 = d { checker = false; } else if(array_int[i][j] == 8) //Check for 8 = s { checker = false; } else if(array_int[i][j] == 9) //Check for 9 = g { checker = false; } else //All other integers, which are false { checker = true; System.out.print("Error at Row:" + i + " Column:" + j + "\n"); } } } if(checker == false) { System.out.print("Array OK... \n"); } } public void printInt() { int i = 0, j = 0; //System.out.println(Arrays.deepToString(array_int)); for(i = 0; i < row; i++) { System.out.print("Row " + (i + 1) + ":"); for(j = 0; j < col; j++) { System.out.print(" " + String.valueOf(array_int[i][j])); //System.out.print(" " + Integer.toString(array_int[i][j])); //System.out.printf(" %d", array_int[i][j]); //System.out.print(" " + array_int[i][j]); } System.out.print("\n"); } } }
Can you quickly tell me if this pseudocode makes sense or not?
I believe my code is now foolproof. I will write up the pseudocode now. But I do have one question. Why does DRJava ask that I return something outside of my if statements? As you can see I wrote for ex: "return 1;" just because it asked. It will never return that value however. Can someone explain this to me? public class assignment1question2test { public static void main(String[] args) { int[] a = new int[50]; int l = 0; int r = a.length; for(int i=0; i<r; i++) { a[i] = 1; } a[0] = 10; for (int i=0; i<r; i++) { System.out.println(a[i]); } System.out.print(recursiveSearch(a,l,r)); } public static int recursiveSearch (int[] a, int l, int r) { int third1 = (r-l)/3 + l; int third2 = third1*2 - l + 1; System.out.println("i will be checking compare from " + l + " to " + third1 + " and " + (third1 + 1) + " to " + third2); int compareResult = compare(a,l,third1,third1 + 1, third2); if(r-l == 1) { if (compareResult == 1) { return l; } else { return r; } } if (compareResult == 0) { return recursiveSearch(a,third2 + 1, r); } if (compareResult == 1) { return recursiveSearch(a,l,third1); } if (compareResult == -1) { return recursiveSearch(a,third1 + 1, third2); } return 1; } public static int compare(int[] a, int i, int j, int k, int l) { int count1 = 0; int count2 = 0; for(int g=i; g<=j; g++) { count1 = count1 + a[g]; } for(int g=k; g<=l; g++) { count2 = count2 + a[g]; } if (count1 == count2) { return 0; } if (count1 > count2) { return 1; } if (count1 < count2) { return -1; } return 0; } } UPDATED FINAL PSEUDOCODE: Algorithm: recursiveSearch (a,l,r) Inputs: An array a, indices l and r which delimit the part of interest. Output: The index that has the lead coin. int third1 ← (r - l + 1)/3 int third2 ← third1*2 - l + 1 if (r-l = 0) then return l int compareResult ← compare(a,l,third1,third1 + 1,third2) if (r-l = 1) then if (compareResult = 1) then return l else return r if (compareResult = 0) then return recursiveSearch(a, third2 + 1, r) if (compareResult = "1") then return recursiveSearch(a,l,third1) if (compareResult = "-1") then return recursiveSearch(a,third1 + 1,third2)
You seem to be including mid in the following search regardless of which side is larger. The recursive calls should both exclude mid from their search space. Also, for the comparison to be meaningful, the two groups being compared need to be of equal size. That will require some extra odd/even logic.
divide bags of candies among three children evenly
I have n bags of candies such that no two bags have the same number of candies inside (i.e. it's a set A[] = {a0,a1,a2,...,ai,...,aj} where ai != aj). I know how many candies is in each bag and the total number M of candies I have. I need to divide the bags among three children so that the candies are distributed as fairly as possible (i.e. each child gets as close to M/3 as possible). Needless to say, I may not tear into the bags to even out the counts -- then the question would be trivial. Does anyone have any thoughts how to solve this -- preferably in Java? EDIT: the interviewer wanted me to use a 2-D array to solve the problem: the first kid gets x, the second kid y, the third gets the rest: S[x][y]. This after I tried following: 1] sort array n lg n 2] starting with largest remaining bag, give bag to kid with fewest candy. Here is my solution for partitioning to two children (it is the correct answer). Maybe it will help with getting the 3-way partition. int evenlyForTwo(int[] A, int M) { boolean[] S = new boolean[M+1]; S[0]=true;//empty set for(int i=0; i<A.length; i++) for(int x=M; x >= A[i]; x--) if(!S[x]) S[x]=S[x-A[i]]; int k = (int) M/2; while(!S[k]) k--; return k;//one kid gets k the other the rest. }//
The problem you describe is known as the 3-Partition problem and is known to be NP-hard. The problem is discussed a bit on MathOverflow. You might find some of the pointers there of some value.
Here is a little solution, crude but gives correct results. And you can even change the number of children, bags, etc. public class BagOfCandies { static public void main(String...args) { int repeat = 10; int childCount = 3; int bagsCount = childCount + (int) (Math.random() * 10); for (int k=0; k<repeat; k++) { int candyCount = 0, n=0; int[] bags = new int[bagsCount]; for (int i=0; i<bags.length; i++) { n += 1 + (int) (Math.random() * 2); bags[i] = n; candyCount += n; } shuffle(bags); // completely optional! It works regardless boolean[][] dist = divideBags(bags, childCount); System.out.println("Bags of candy : " + Arrays.toString(bags) + " = " + bags.length); System.out.println("Total calculated candies is " + candyCount); int childCandySum = 0; for (int c=0; c<childCount; c++) { int childCandies = countSumBags(bags, dist[c]); System.out.println("Child " + (c+1) + " = " + childCandies + " --> " + Arrays.toString(dist[c])); childCandySum += childCandies; } System.out.println("For a total of " + childCandySum + " candies"); System.out.println("----------------"); } } static private void shuffle(int[] bags) { for (int i=0, len=bags.length; i<len; i++) { int a = (int)Math.floor(Math.random()*len); int b = (int)Math.floor(Math.random()*len); int v = bags[a]; bags[a] = bags[b]; bags[b] = v; } } static private boolean[][] divideBags(int[] bags, int childCount) { int bagCount = bags.length; boolean[][] dist = new boolean[childCount][bagCount]; for (int c=0; c<childCount; c++) Arrays.fill(dist[c], false); for (int i=0; i<bagCount; i+=childCount) for (int j=i, c=0; c<childCount && j<bagCount; j++, c++) dist[c][j] = true; if (childCount == 1) return dist; // shortcut here int sumDiff = 1; int oldDiff = 0; while (sumDiff != oldDiff) { oldDiff = sumDiff; sumDiff = 0; // start comparing children in pair for (int child1=0; child1<childCount-1; child1++) { for (int child2=child1+1; child2<childCount; child2++) { int count1 = countSumBags(bags, dist[child1]); int count2 = countSumBags(bags, dist[child2]); int diff = Math.abs(count1 - count2); // a difference less than 2 is negligeable if (diff > 1) { // find some bags with can swap to even their difference int c1=-1, c2=-1, cdiff; boolean swap = false; for (int i=0; i<bagCount-1; i++) { for (int j=i; j<bagCount; j++) { if (dist[child1][i] && dist[child2][j]) { cdiff = Math.abs((count1 - bags[i] + bags[j]) - (count2 + bags[i] - bags[j])); if (cdiff < diff) { c1 = i; c2 = j; diff = cdiff; swap = true; } } if (dist[child1][j] && dist[child2][i]) { cdiff = Math.abs((count1 - bags[j] + bags[i]) - (count2 + bags[j] - bags[i])); if (cdiff < diff) { c1 = j; c2 = i; diff = cdiff; swap = true; } } } } if (swap) { //System.out.println("Swaping " + c1 + " with " + c2); dist[child1][c1] = false; dist[child1][c2] = true; dist[child2][c1] = true; dist[child2][c2] = false; } } //System.out.println("Diff between " + child1 + "(" + countSumBags(bags, dist[child1]) + ") and " + child2 + "(" + countSumBags(bags, dist[child2]) + ") is " + diff); sumDiff += diff; } } //System.out.println("oldDiff="+oldDiff+", sumDiff="+sumDiff); } return dist; } static private int countSumBags(int[] bags, boolean[] t) { int count = 0; for (int i=0; i<t.length; i++) { if (t[i]) { count+=bags[i]; } } return count; } } I don't know if this the result you were looking for, but it seems to be, from my understanding of the question.