Partition a set into two subsets such that difference between max of one and min of other is minimized

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 arrayint 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 Codeint 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;} |
Java
// java program for the above approachimport java.util.*; class GFG{Â
// Function to split the arraystatic 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 Codepublic 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 |
Python3
# Python3 program for the above approachÂ
# Function to split the arraydef 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 Codeif __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 |
C#
// C# program for the above approachusing System;class GFG{Â
// Function to split the arraystatic 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 Codepublic 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 |
Javascript
<script>Â
    // Javascript program for the above approach         // Function to split the array    function splitArray(arr, N)    {        // Sort the array in increasing order        arr.sort();Â
        let result = Number.MAX_VALUE;Â
        // Calculating the max difference        // between consecutive elements        for (let i = 1; i < N; i++) {            result = Math.min(result,                         arr[i] - arr[i - 1]);        }Â
        // Return the final minimum difference        return result;    }         // Given array arr[]    let arr = [ 3, 1, 2, 6, 4 ];       // Size of array    let N = arr.length;       // Function Call    document.write(splitArray(arr, N));     </script> |
1
Â
Time Complexity: O(N*log N)
 Space Complexity : O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



