Javatpoint Logo
Javatpoint Logo

Minimum Difference Among Group Size Two in Java

An array containing various numbers is given. The task is to create different groups, each containing only two elements, such that the difference between the group containing the largest sum and the group containing the lowest sum should be minimum. Note that any element can only be part of only one group. Also, it is not allowed to leave any element. In other words, an element has to be part of a group.

Example 1:

Input:

int arr[] = {5, 0, 9, 7, 15, 11, 19, 17}

Output: 3

Explanation: Let's form a group of two-element for the given array.

G1 = {5, 17}, G2 = {0, 19}, G3 = {9, 11}, G4 = {7, 15}. If we find the sum of elements present in each group we get,

G1 = 5 + 17 = 22, G2 = 0 + 19 = 19, G3 = 9 + 11 = 20, G4 = 7 + 15 = 22

Thus, we see that the minimum sum is 19, and the maximum sum is 22. Hence, the difference is 22 - 19 = 3.

Example 2:

Input:

int arr[] = {4, 11, 5, 3, 7, 2}

Output: 4

Explanation: Let's form a group of two-element for the given array.

G1 = {4, 5}, G2 = {11, 2}, G3 = {3, 7}. If we find the sum of elements present in each group we get,

G1 = 4 + 5 = 9, G2 = 11 + 2 = 13, G3 = 3 + 7 = 10.

Thus, we see that the minimum sum is 9, and the maximum sum is 13. Hence, the difference is 13 - 9 = 4.

Example 3:

Input:

int arr[] = {4, 9, 3, 8, 2, 0, 5}

Output: It is not possible to find the minimum difference.

Explanation: Let's form a group of two-element for the given array.

G1 = {4, 9}, G2 = {3, 8}, G3 = {2, 0}. Thus, we see that we have left element 5, and as per the condition, it is not allowed to leave any element. It does not matter how hard we try to form different groups, one of the elements has to skip. Hence, the solution is not possible.

Approach

The approach is simple. One has to keep the maximum to the lowest value and the minimum to the highest value so that the gap between the maximum and the minimum is the lowest. In order to do so, we have to combine the numbers of the array in such a way that the maximum element gets mapped with the lowest element, and the second maximum element gets mapped with the second largest element. In order to achieve the same, it is required to sort the array, then combine (make a group) the last element with the first element, the second last element with the second element, and so on. The illustration of the same is mentioned in the following program.

FileName: LeastDiff.java

Output:

For the input array: 
5 0 9 7 15 11 19 17 
The minimum difference is: 3


For the input array: 
4 11 5 3 7 2 
The minimum difference is: 4


For the input array: 
4 9 3 8 2 0 5 
It is not possible to find the minimum difference.

Complexity Analysis: Since the program is using sorting, the time complexity of the program is O(n * log(n)). Also, the program is using an array list for storing the sum. Thus, the space complexity of the program is O(n), where n is the total number of elements present in the input array.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA