OneCompiler

code to divide the even set of integers into two equal sets(n/2) and the difference between addition of these two sets is minimum.

227

import java.util.Scanner;

public class Data {
public static void main(String args[]) {
int n, size1, size2, a1 = 0, b1 = 0, c1, temp;
Scanner s = new Scanner(System.in);
System.out.println("eneter the even size of array");
n = s.nextInt();
size1 = n / 2;
size2 = n - size1;
int a[] = new int[n];
int b[] = new int[size1];
int c[] = new int[size2];
int diff[] = new int[size1];
System.out.println("enter " + n + " array elements");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
System.out.println(" New array1 after dividation");
for (int j = 0; j < size1; j++) {
b[j] = a[j];
System.out.println(b[j]);
}
System.out.println(" New array2 after dividation");
for (int k = 0; k < size1; k++) {

        c[k] = a[k + size1];
        System.out.println(c[k]);
    }
    for (int i = 0; i < size1; i++) {
        for (int j = 0; j < size1; j++) {
            a1 = a1 + b[j];

            b1 = b1 + c[j];

        }
        System.out.println("First array addition  =" + a1);
        System.out.println("Second array addition  =" + b1);
        c1 = a1 - b1;
        System.out.println("first difference is = " + c1);
        diff[i] = c1;

        a1 = 0;
        b1 = 0;
        temp = b[i];
        b[i] = c[i];
        c[i] = temp;
        System.out.println("*****************************");

    }

    for (int i = 0; i < size1; i++) {
        for (int j = 1; j < size1; j++) {
            if (Math.abs(diff[i]) > Math.abs(diff[j])) {
                temp = diff[i];
                diff[i] = diff[j];
                diff[j] = temp;
            }
        }
    }
    System.out.println("the smallest difference is " + Math.abs(diff[0]));
}

}