Given an array **arr[]** of **N** integers, the task is to split the array into two subsets such that the absolute difference between the maximum of first subset and minimum of second subset is minimum.

**Examples:**

Input:arr[] = {3, 1, 2, 6, 4}

Output:1

Explanation:

Splitting the given array in two subsets, A = [1, 2, 4], B = [3, 6]. Difference of maximum of first set is 4 and minimum of second set is 3 and their difference is 1.

Input:arr[] = {2, 1, 3, 2, 4, 3}

Output:0

Explanation:

Splitting the given array in two subsets, A = [1, 2, 2, 3], B = [3, 4]. Difference of maximum of first set is 3 and minimum of second set is 3 and their difference is 0.

**Approach:** To solve the above problem we have to find the two integers such that **m** and **n** such that max of first set is **m** and the min of second set is **n**. The idea is to sort the given array ascending order and after sorting the array, the minimum difference between the consecutive element is the required minimum difference after partitioning the array elements into subsets.

Below is the implementation of above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to split the array ` `int` `splitArray(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Sort the array in increasing order ` ` ` `sort(arr, arr + N); ` ` ` ` ` `int` `result = INT_MAX; ` ` ` ` ` `// Calculating the max difference ` ` ` `// between consecutive elements ` ` ` `for` `(` `int` `i = 1; i < N; i++) { ` ` ` `result = min(result, ` ` ` `arr[i] - arr[i - 1]); ` ` ` `} ` ` ` ` ` `// Return the final minimum difference ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given array arr[] ` ` ` `int` `arr[] = { 3, 1, 2, 6, 4 }; ` ` ` ` ` `// Size of array ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function Call ` ` ` `cout << splitArray(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java program for the above approach ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to split the array ` `static` `int` `splitArray(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Sort the array in increasing order ` ` ` `Arrays.sort(arr); ` ` ` ` ` `int` `result = Integer.MAX_VALUE; ` ` ` ` ` `// Calculating the max difference ` ` ` `// between consecutive elements ` ` ` `for` `(` `int` `i = ` `1` `; i < N; i++) ` ` ` `{ ` ` ` `result = Math.min(result, ` ` ` `arr[i] - arr[i - ` `1` `]); ` ` ` `} ` ` ` ` ` `// Return the final minimum difference ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Given array arr[] ` ` ` `int` `arr[] = { ` `3` `, ` `1` `, ` `2` `, ` `6` `, ` `4` `}; ` ` ` ` ` `// Size of array ` ` ` `int` `N = arr.length; ` ` ` ` ` `// Function Call ` ` ` `System.out.print(splitArray(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to split the array ` `def` `splitArray(arr, N): ` ` ` ` ` `# Sort the array in increasing ` ` ` `# order ` ` ` `arr ` `=` `sorted` `(arr) ` ` ` ` ` `result ` `=` `10` `*` `*` `9` ` ` ` ` `# Calculating the max difference ` ` ` `# between consecutive elements ` ` ` `for` `i ` `in` `range` `(` `1` `, N): ` ` ` `result ` `=` `min` `(result, arr[i] ` `-` `arr[i ` `-` `1` `]) ` ` ` ` ` `# Return the final minimum difference ` ` ` `return` `result ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Given array arr[] ` ` ` `arr ` `=` `[ ` `3` `, ` `1` `, ` `2` `, ` `6` `, ` `4` `] ` ` ` ` ` `# Size of array ` ` ` `N ` `=` `len` `(arr) ` ` ` ` ` `# Function Call ` ` ` `print` `(splitArray(arr, N)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to split the array ` `static` `int` `splitArray(` `int` `[]arr, ` `int` `N) ` `{ ` ` ` `// Sort the array in increasing order ` ` ` `Array.Sort(arr); ` ` ` ` ` `int` `result = Int32.MaxValue; ` ` ` ` ` `// Calculating the max difference ` ` ` `// between consecutive elements ` ` ` `for` `(` `int` `i = 1; i < N; i++) ` ` ` `{ ` ` ` `result = Math.Min(result, ` ` ` `arr[i] - arr[i - 1]); ` ` ` `} ` ` ` ` ` `// Return the final minimum difference ` ` ` `return` `result; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `// Given array arr[] ` ` ` `int` `[]arr = { 3, 1, 2, 6, 4 }; ` ` ` ` ` `// Size of array ` ` ` `int` `N = arr.Length; ` ` ` ` ` `// Function Call ` ` ` `Console.Write(splitArray(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** *O(N*log N)*

## Recommended Posts:

- Partition the array into two odd length groups with minimized absolute difference between their median
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Partition of a set into K subsets with equal sum using BitMask and DP
- Choose k array elements such that difference of maximum and minimum is minimized
- Divide a sorted array in K parts with sum of difference of max and min minimized in each part
- Partition N into M parts such that difference between Max and Min part is smallest
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Minimum difference between max and min of all K-size subsets
- Maximum possible difference of two subsets of an array
- k size subsets with maximum difference d between max and min
- Maximum difference between two subsets of m elements
- Maximize the difference between two subsets of a set with negatives
- Minimize the absolute difference of sum of two subsets
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split Array into min number of subsets with difference between each pair greater than 1
- Subsets of size K with product equal to difference of two perfect squares
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Find a point such that sum of the Manhattan distances is minimized
- Select K elements from an array whose maximum value is minimized
- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.