Split a given array into K subarrays minimizing the difference between their maximum and minimum
Last Updated :
21 Aug, 2021
Given a sorted array arr[] of N integers and an integer K, the task is to split the array into K subarrays such that the sum of the difference of maximum and minimum element of each subarray is minimized.
Examples:
Input: arr[] = {1, 3, 3, 7}, K = 4
Output: 0
Explanation:
The given array can be split into 4 subarrays as {1}, {3}, {3}, and {7}.
The difference between minimum and maximum of each subarray is:
1. {1}, difference = 1 - 1 = 0
2. {3}, difference = 3 - 3 = 0
3. {3}, difference = 3 - 3 = 0
4. {7}, difference = 7 - 7 = 0
Therefore, the sum all the difference is 0 which is minimized.
Input: arr[] = {4, 8, 15, 16, 23, 42}, K = 3
Output: 12
Explanation:
The given array can be split into 3 subarrays as {4, 8, 15, 16}, {23}, and {42}.
The difference between minimum and maximum of each subarray is:
1. {4, 8, 15, 16}, difference = 16 - 4 = 12
2. {23}, difference = 23 - 23 = 0
3. {42}, difference = 42 - 42 = 0
Therefore, the sum all the difference is 12 which is minimized.
Approach: To split the given array into K subarrays with the given conditions, the idea is to split at indexes(say i) where the difference between elements arr[i+1] and arr[i] is largest. Below are the steps to implement this approach:
- Store the difference between consecutive pairs of elements in the given array arr[] into another array(say temp[]).
- Sort the array temp[] in increasing order.
- Initialise the total difference(say diff) as the difference of the first and last element of the given array arr[].
- Add the first K - 1 values from the array temp[] to the above difference.
- The value stored in diff is the minimum sum of the difference of maximum and minimum element of the K subarray.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the subarray
int find(int a[], int n, int k)
{
vector<int> v;
// Add the difference to vectors
for (int i = 1; i < n; ++i) {
v.push_back(a[i - 1] - a[i]);
}
// Sort vector to find minimum k
sort(v.begin(), v.end());
// Initialize result
int res = a[n - 1] - a[0];
// Adding first k-1 values
for (int i = 0; i < k - 1; ++i) {
res += v[i];
}
// Return the minimized sum
return res;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 4, 8, 15, 16, 23, 42 };
int N = sizeof(arr) / sizeof(int);
// Given K
int K = 3;
// Function Call
cout << find(arr, N, K) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the subarray
static int find(int a[], int n, int k)
{
Vector<Integer> v = new Vector<Integer>();
// Add the difference to vectors
for(int i = 1; i < n; ++i)
{
v.add(a[i - 1] - a[i]);
}
// Sort vector to find minimum k
Collections.sort(v);
// Initialize result
int res = a[n - 1] - a[0];
// Adding first k-1 values
for(int i = 0; i < k - 1; ++i)
{
res += v.get(i);
}
// Return the minimized sum
return res;
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
int arr[] = { 4, 8, 15, 16, 23, 42 };
int N = arr.length;
// Given K
int K = 3;
// Function Call
System.out.print(find(arr, N, K) + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to find the subarray
def find(a, n, k):
v = []
# Add the difference to vectors
for i in range(1, n):
v.append(a[i - 1] - a[i])
# Sort vector to find minimum k
v.sort()
# Initialize result
res = a[n - 1] - a[0]
# Adding first k-1 values
for i in range(k - 1):
res += v[i]
# Return the minimized sum
return res
# Driver code
arr = [ 4, 8, 15, 16, 23, 42 ]
# Length of array
N = len(arr)
K = 3
# Function Call
print(find(arr, N, K))
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the subarray
static int find(int []a, int n, int k)
{
List<int> v = new List<int>();
// Add the difference to vectors
for(int i = 1; i < n; ++i)
{
v.Add(a[i - 1] - a[i]);
}
// Sort vector to find minimum k
v.Sort();
// Initialize result
int res = a[n - 1] - a[0];
// Adding first k-1 values
for(int i = 0; i < k - 1; ++i)
{
res += v[i];
}
// Return the minimized sum
return res;
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
int []arr = { 4, 8, 15, 16, 23, 42 };
int N = arr.Length;
// Given K
int K = 3;
// Function Call
Console.Write(find(arr, N, K) + "\n");
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// javascript program for the above approach
// Function to find the subarray
function find(a , n , k) {
var v = [];
// Add the difference to vectors
for (i = 1; i < n; ++i) {
v.push(a[i - 1] - a[i]);
}
// Sort vector to find minimum k
v.sort((a,b)=>a-b);
// Initialize result
var res = a[n - 1] - a[0];
// Adding first k-1 values
for (i = 0; i < k - 1; ++i) {
res += v[i];
}
// Return the minimized sum
return res;
}
// Driver Code
// Given array arr
var arr = [ 4, 8, 15, 16, 23, 42 ];
var N = arr.length;
// Given K
var K = 3;
// Function Call
document.write(find(arr, N, K) + "\n");
// This code contributed by aashish1995
</script>
Time Complexity: O(N), where N is the number of elements in the array.
Auxiliary Space: O(N), where N is the number of elements in the array.
Similar Reads
Split array into subarrays such that sum of difference between their maximums and minimums is maximum Given an array arr[] consisting of N integers, the task is to split the array into subarrays such that the sum of the difference between the maximum and minimum elements for all the subarrays is maximum. Examples : Input: arr[] = {8, 1, 7, 9, 2}Output: 14Explanation:Consider splitting the given arra
6 min read
Minimize difference between maximum and minimum Subarray sum by splitting Array into 4 parts Given an array arr[] of size N, the task is to find the minimum difference between the maximum and the minimum subarray sum when the given array is divided into 4 non-empty subarrays. Examples: Input: N = 5, arr[] = {3, 2, 4, 1, 2}Output: 2Explanation: Divide the array into four parts as {3}, {2}, {
14 min read
Split array into K Subarrays to minimize sum of difference between min and max Given a sorted array arr[] of size N and integer K, the task is to split the array into K non-empty subarrays such that the sum of the difference between the maximum element and the minimum element of each subarray is minimized. Note: Every element of the array must be included in one subarray and e
6 min read
Minimize difference between maximum and minimum array elements by removing a K-length subarray Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum difference between the maximum and minimum element present in the array after removing any subarray of size K. Examples: Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2Output: 4Explanation: Remove the subarray {
10 min read
Min difference between maximum and minimum element in all Y size subarrays Given an array arr[] of size N and integer Y, the task is to find a minimum of all the differences between the maximum and minimum elements in all the sub-arrays of size Y. Examples: Input: arr[] = { 3, 2, 4, 5, 6, 1, 9 } Y = 3Output: 2Explanation:All subarrays of length = 3 are:{3, 2, 4} where maxi
15+ min read
Minimum difference between maximum and minimum value of Array with given Operations Given an array arr[] and an integer K. The following operations can be performed on any array element: Multiply the array element with K.If the element is divisible by K, then divide it by K. The above two operations can be applied any number of times including zero on any array element. The task is
9 min read