Split array into two subarrays such that difference of their maximum is minimum
Last Updated :
20 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int findMinDif( int arr[], int N)
{
sort(arr, arr + N);
return (arr[N - 1] - arr[N - 2]);
}
int main()
{
int arr[] = { 7, 9, 5, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinDif(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findMinDif( int arr[], int N)
{
Arrays.sort(arr);
return (arr[N - 1 ] - arr[N - 2 ]);
}
public static void main(String[] args)
{
int arr[] = { 7 , 9 , 5 , 10 };
int N = arr.length;
System.out.println(findMinDif(arr, N));
}
}
|
Python3
def findMinDif(arr, N):
arr.sort()
return (arr[N - 1 ] - arr[N - 2 ])
arr = [ 7 , 9 , 5 , 10 ]
N = len (arr)
print (findMinDif(arr, N))
|
C#
using System;
class GFG{
static int findMinDif( int []arr, int N)
{
Array.Sort(arr);
return (arr[N - 1] - arr[N - 2]);
}
public static void Main()
{
int []arr = { 7, 9, 5, 10 };
int N = arr.Length;
Console.Write(findMinDif(arr, N));
}
}
|
Javascript
<script>
function findMinDif(arr , N) {
arr.sort((a,b)=>a-b);
return (arr[N - 1] - arr[N - 2]);
}
var arr = [ 7, 9, 5, 10 ];
var N = arr.length;
document.write(findMinDif(arr, N));
</script>
|
Time complexity: O(N*log(N)), N is the number of elements of the array.
Auxiliary Space: O(1)
Another Approach: We can optimize the above code by removing the sort function used above. As the answer is basically the difference between the two greatest elements of the array, so we can traverse the array and can find two greatest elements in O(n) time.
Below is the code for the given approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinDif( int arr[], int n)
{
int first_max = INT_MIN;
int second_max = INT_MIN;
for ( int i = 0; i < n ; i ++)
{
if (arr[i] > first_max)
{
second_max = first_max;
first_max = arr[i];
}
else if (arr[i] > second_max)
second_max = arr[i];
}
return first_max-second_max;
}
int main()
{
int arr[] = { 7, 9, 5, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findMinDif(arr, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static int findMinDif( int [] arr, int n)
{
int first_max = Integer.MIN_VALUE;
int second_max = Integer.MIN_VALUE;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > first_max) {
second_max = first_max;
first_max = arr[i];
}
else if (arr[i] > second_max)
second_max = arr[i];
}
return first_max - second_max;
}
public static void main(String[] args)
{
int [] arr = { 7 , 9 , 5 , 10 };
int n = arr.length;
System.out.println(findMinDif(arr, n));
}
}
|
Python3
def findMinDif(arr, n):
first_max = - 2147483647
second_max = - 2147483647
for i in range ( 0 ,n):
if (arr[i] > first_max):
second_max = first_max
first_max = arr[i]
elif (arr[i] > second_max):
second_max = arr[i]
return first_max - second_max
arr = [ 7 , 9 , 5 , 10 ]
n = len (arr)
print (findMinDif(arr, n))
|
C#
using System;
public class GFG {
public static int findMinDif( int [] arr, int n)
{
int first_max = Int32.MinValue;
int second_max = Int32.MinValue;
for ( int i = 0; i < n; i++)
{
if (arr[i] > first_max) {
second_max = first_max;
first_max = arr[i];
}
else if (arr[i] > second_max)
second_max = arr[i];
}
return first_max - second_max;
}
static public void Main()
{
int [] arr = { 7, 9, 5, 10 };
int n = arr.Length;
Console.WriteLine(findMinDif(arr, n));
}
}
|
Javascript
<script>
function findMinDif(arr,n)
{
let first_max = Number.MIN_VALUE;
let second_max = Number.MIN_VALUE;
for (let i = 0; i < n ; i ++)
{
if (arr[i] > first_max)
{
second_max = first_max;
first_max = arr[i];
}
else if (arr[i] > second_max)
second_max = arr[i];
}
return first_max-second_max;
}
let arr =[ 7, 9, 5, 10 ];
let n = arr.length;
console.log(findMinDif(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...