Split array into two subarrays such that difference of their maximum is minimum - GeeksforGeeks

07 May, 2021
Given an array arr[] consisting of integers, the task is to split the given array into two sub-arrays such that the 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 the 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++
Java
Python3
C#
Javascript
// 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 Programint main(){     int arr[] = { 7, 9, 5, 10 };    int N = sizeof(arr) / sizeof(arr[0]);     cout << findMinDif(arr, N);    return 0;}
// 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 codepublic 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
# 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 Programarr = [ 7, 9, 5, 10 ]N = len(arr)print(findMinDif(arr, N)) # This code is contributed by yatinagg
// 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 codepublic 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
<script>// javascript Program to split a given array// such that the difference between// their maximums is minimized.     function findMinDif(arr , N) {         // Sort the array        arr.sort((a,b)=>a-b);         // Return the difference between        // two highest elements        return (arr[N - 1] - arr[N - 2]);    }     // Driver code             var arr = [ 7, 9, 5, 10 ];        var N = arr.length;         document.write(findMinDif(arr, N)); // This code contributed by gauravrajput1</script>
1
 
Time complexity: O(N*log(N)), N is the number of elements of the array. 
yatinagg
offbeat
Code_Mech
GauravRajput1
subarray
Arrays
Sorting
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Top 50 Array Coding Problems for Interviews
Introduction to Arrays
Multidimensional Arrays in Java