Given an array **arr[]** consisting of integers, the task is to split the given array into two sub-arrays such that difference between their maximum elements is minimum.

**Example:**

Input:arr[] = {7, 9, 5, 10}

Output:1

Explanation:

The subarrays are {5, 10} and {7, 9} with the difference between their maximums = 10 – 9 = 1.

Input:arr[] = {6, 6, 6}

Output:0

**Approach:**

We can observe that we need to split the array into two subarrays such that:

- If the maximum element occurs more than once in the array, it needs to be present in both the subarrays at least once.
- Otherwise, the largest and the second-largest elements should be present in different subarrays.

This ensures that the difference between the maximum elements of the two subarrays is maximized.

Hence, we need to sort the array, and then the difference between largest 2 elements, i.e. arr[n – 1] and arr[n – 2], is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ Program to split a given ` `// array such that the difference ` `// between their maximums is minimized. ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findMinDif(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Sort the array ` ` ` `sort(arr, arr + N); ` ` ` ` ` `// Return the difference ` ` ` `// between two highest ` ` ` `// elements ` ` ` `return` `(arr[N - 1] - arr[N - 2]); ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` ` ` `int` `arr[] = { 7, 9, 5, 10 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << findMinDif(arr, N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to split a given array ` `// such that the difference between ` `// their maximums is minimized. ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `int` `findMinDif(` `int` `arr[], ` `int` `N) ` `{ ` ` ` ` ` `// Sort the array ` ` ` `Arrays.sort(arr); ` ` ` ` ` `// Return the difference between ` ` ` `// two highest elements ` ` ` `return` `(arr[N - ` `1` `] - arr[N - ` `2` `]); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `7` `, ` `9` `, ` `5` `, ` `10` `}; ` ` ` `int` `N = arr.length; ` ` ` ` ` `System.out.println(findMinDif(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to split a given ` `# array such that the difference ` `# between their maximums is minimized. ` `def` `findMinDif(arr, N): ` ` ` ` ` `# Sort the array ` ` ` `arr.sort() ` ` ` ` ` `# Return the difference ` ` ` `# between two highest ` ` ` `# elements ` ` ` `return` `(arr[N ` `-` `1` `] ` `-` `arr[N ` `-` `2` `]) ` ` ` `# Driver Program ` `arr ` `=` `[ ` `7` `, ` `9` `, ` `5` `, ` `10` `] ` `N ` `=` `len` `(arr) ` `print` `(findMinDif(arr, N)) ` ` ` `# This code is contributed by yatinagg ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to split a given array ` `// such that the difference between ` `// their maximums is minimized. ` `using` `System; ` `class` `GFG{ ` ` ` `static` `int` `findMinDif(` `int` `[]arr, ` `int` `N) ` `{ ` ` ` ` ` `// Sort the array ` ` ` `Array.Sort(arr); ` ` ` ` ` `// Return the difference between ` ` ` `// two highest elements ` ` ` `return` `(arr[N - 1] - arr[N - 2]); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 7, 9, 5, 10 }; ` ` ` `int` `N = arr.Length; ` ` ` ` ` `Console.Write(findMinDif(arr, N)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time complexity: ** O(N*log(N)), N is the number of elements of the array.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Split array into two subarrays such that difference of their sum is minimum
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Split an array into two equal Sum subarrays
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Split array into K subsets to maximize their sum of maximums and minimums
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Check if an array can be split into subarrays with GCD exceeding K
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Break an array into maximum number of sub-arrays such that their averages are same
- Divide array into two sub-arrays such that their averages are equal
- Split array into minimum number of subsets having maximum pair sum at most K
- Split array into two subsequences having minimum count of pairs with sum equal to X
- Count all subarrays whose sum can be split as difference of squares of two Integers

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.