import java.util.ArrayList;
import java.util.List;
public class ArrayPartition {
public static void main(String[] args) {
int[] array = new int[] {5,7,9,2,69,23,9,1,8,5,7,3,6,16};
//int[] array = new int[] {1,2,3,4,5,6,7,8,9,10,1,3,3,6};
//int[] array = new int[] {5,5,2,2,1,1,2};
//int[] array = new int[] {1,2,3,4,5,6,7,8,9,10};
//int[] array = new int[] {100,50,55,2,2,1};
//int[] array = new int[] {1 , 2 , 3 , 6, 12};
//int[] array = new int[] {1,2,3,4};
//int[] array = new int[] {1000, 5000, 3000, 1000};
//int[] array = new int[] {1,2,3,4,5,6,7};
//int[] array = new int[] {1,1,1,1,1,1,2,4,2,2,1,3};
//int[] array = new int[] {10,100,50,110};
//int[] array = new int[] {4, 1, 2, 3};
//int[] array = new int[] {500000, 1,332234,3565, 332234};
// Sort the given array
sortArray(array);
// Call partition method to split the array
partition(array);
}
public static void partition(int[] array) {
List<Integer> arr1 = new ArrayList<Integer>();
List<Integer> arr2 = new ArrayList<Integer>();
int sum1 = 0, sum2 = 0;
// Place last value (largest) value in array1
arr1.add(array[array.length-1]);
sum1 = array[array.length-1];
// Iterate remaining values in array
for (int i=0,j=array.length-2;i<=j;) {
if(sum2 <= sum1) {
arr2.add(array[i]);
sum2 += array[i];
i++;
}else {
arr1.add(array[i]);
sum1 += array[i];
i++;
}
if(i <= j) {
if(sum2 <= sum1) {
arr2.add(array[j]);
sum2 += array[j];
j--;
}else {
arr1.add(array[j]);
sum1 += array[j];
j--;
}
}
}
// Reorder array1 and array2 with equal sum or minimal sum
if(sum1 > sum2) {
int diff = (sum1 - sum2)/2;
if(arr1.contains(diff) && diff > 0) {
int sum[] = replaceArrayElement(arr1, diff, arr2, sum1, sum2);
sum1 = sum[0];
sum2 = sum[1];
}
while(sum1 > sum2 && diff > 0) {
diff--;
if(arr1.contains(diff) && diff > 0) {
int sum[] = replaceArrayElement(arr1, diff, arr2, sum1, sum2);
sum1 = sum[0];
sum2 = sum[1];
diff++;
}
}
}
// Reorder array1 and array2 with equal sum or minimal sum
if(sum2 > sum1) {
int diff = (sum2 - sum1)/2;
if(arr2.contains(diff) && diff > 0) {
int sum[] = replaceArrayElement(arr2, diff, arr1, sum2, sum1);
sum2 = sum[0];
sum1 = sum[1];
}
while(sum2 > sum1 && diff > 0) {
diff--;
if(arr2.contains(diff) && diff > 0) {
int sum[] = replaceArrayElement(arr2, diff, arr1, sum2, sum1);
sum2 = sum[0];
sum1 = sum[1];
diff++;
}
}
}
System.out.println("1st Array sum : "+sum1);
System.out.println("2nd Array sum : "+sum2);
System.out.print("\nARRAY-1 : ");
for (Integer integer : arr1) {
System.out.print(integer +", ");
}
System.out.print("\nARRAY-2 : ");
for (Integer integer : arr2) {
System.out.print(integer +", ");
}
}
private static int[] replaceArrayElement(List<Integer> arr1, int diff, List<Integer> arr2, int sum1, int sum2) {
int in = arr1.indexOf(diff);
arr1.remove(in);
sum1 = sum1 - diff;
arr2.add(diff);
sum2 = sum2 + diff;
return new int[] {sum1, sum2};
}
private static void sortArray(int[] array) {
for(int i=0;i<=array.length-1;i++) {
for(int j=i+1;j<=array.length-1;j++) {
if(array[i] > array[j]) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
}