Given an array of integers greater than zero, find it possible to split it in two subarrays such that the sum of the two subarrays is the same or with minimal difference. Print the two subarrays. |
Examples:
Input: {5,7,9,2,69,23,9,1,8,5,7,3,6,16}
Output:
1st Array sum : 85
2nd Array sum : 85
ARRAY-1 : 69, 8, 7, 1,
ARRAY-2 : 23, 2, 16, 3, 9, 5, 9, 5, 6, 7,
Input: {1,2,3,4,5,6,7,8,9,10}
Output:
1st Array sum : 27
2nd Array sum : 28
ARRAY-1 : 10, 8, 2, 6, 1,
ARRAY-2 : 9, 3, 7, 4, 5,
Lets see simple solution how to split the given array with equal sum or minimal difference.
Steps:
1. Sort the given array.
2. Iterate the array from both end and place the values in 2 different arraylist and also maintain the sum of both list.
3. On each iteration check for both sum difference and if they are not equal then swap difference value from 1st array to 2nd array or wise-verse to make it equal or minimal difference between both the array.
4. Finally print both the array sum and list of elements in both array.
Simple java code for the above solution:
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; } } } } }
OUTPUT:
1st Array sum : 85 2nd Array sum : 85 ARRAY-1 : 69, 8, 7, 1, ARRAY-2 : 23, 2, 16, 3, 9, 5, 9, 5, 6, 7,
No comments:
Write comments