Split array into K subarrays with minimum sum of absolute difference between adjacent elements
Last Updated :
06 Apr, 2023
Given an array, arr[] of size N and an integer K, the task is to split the array into K subarrays minimizing the sum of absolute difference between adjacent elements of each subarray.
Examples:
Input: arr[] = {1, 3, -2, 5, -1}, K = 2
Output: 13
Explanation: Split the array into following 2 subarrays: {1, 3, -2} and {5, -1}.
Input: arr[] = {2, 14, 26, 10, 5, 12}, K = 3
Output: 24
Explanation: Splitting array into following 3 subarrays: {2, 14}, {26}, {10, 5, 12}.
Approach: The given problem can be solved based on the following observations:
The idea is to slice the array arr[] at ith index which gives maximum absolute difference of adjacent elements. Subtract it from the result. Slicing at K – 1 places will give K subarrays with minimum sum of absolute difference of adjacent elements.
Follow the steps below to solve the problem:
- Initialize an array, say new_Arr[], and an integer variable, say ans, to store total absolute difference sum.
- Traverse the array .
- Store absolute difference of adjacent elements, say arr[i+1] and arr[i] in new_Arr[] array.
- Increment ans by absolute difference of arr[i] and arr[i + 1]
- Sort the new_Arr[] array in descending order.
- Traverse the array from i = 0 to i = K-1
- Decrement ans by new_Arr[i].
- Finally, print ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void absoluteDifference( int arr[], int N, int K)
{
int new_Arr[N - 1];
int ans = 0;
for ( int i = 0; i < N - 1; i++) {
new_Arr[i] = abs (arr[i] - arr[i + 1]);
ans += new_Arr[i];
}
sort(new_Arr, new_Arr + N - 1,
greater< int >());
for ( int i = 0; i < K - 1; i++) {
ans -= new_Arr[i];
}
cout << ans << endl;
}
int main()
{
int arr[] = { 1, 3, -2, 5, -1 };
int K = 2;
int N = sizeof (arr) / sizeof (arr[0]);
absoluteDifference(arr, N, K);
return 0;
}
|
Java
import java.util.*;
import java.util.Arrays;
class GFG
{
public static void reverse( int [] array)
{
int n = array.length;
for ( int i = 0 ; i < n / 2 ; i++)
{
int temp = array[i];
array[i] = array[n - i - 1 ];
array[n - i - 1 ] = temp;
}
}
public static void absoluteDifference( int arr[],
int N, int K)
{
int new_Arr[] = new int [N - 1 ];
int ans = 0 ;
for ( int i = 0 ; i < N - 1 ; i++)
{
new_Arr[i] = Math.abs(arr[i] - arr[i + 1 ]);
ans += new_Arr[i];
}
Arrays.sort(new_Arr);
reverse(new_Arr);
for ( int i = 0 ; i < K - 1 ; i++)
{
ans -= new_Arr[i];
}
System.out.println(ans);
}
public static void main (String[] args)
{
int arr[] = { 1 , 3 , - 2 , 5 , - 1 };
int K = 2 ;
int N = arr.length;
absoluteDifference(arr, N, K);
}
}
|
Python3
def absoluteDifference(arr, N, K):
new_Arr = [ 0 for i in range (N - 1 )]
ans = 0
for i in range (N - 1 ):
new_Arr[i] = abs (arr[i] - arr[i + 1 ])
ans + = new_Arr[i]
new_Arr = sorted (new_Arr)[:: - 1 ]
for i in range (K - 1 ):
ans - = new_Arr[i]
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 3 , - 2 , 5 , - 1 ]
K = 2
N = len (arr)
absoluteDifference(arr, N, K)
|
C#
using System;
class GFG{
public static void reverse( int [] array)
{
int n = array.Length;
for ( int i = 0; i < n / 2; i++)
{
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static void absoluteDifference( int [] arr,
int N, int K)
{
int [] new_Arr = new int [N - 1];
int ans = 0;
for ( int i = 0; i < N - 1; i++)
{
new_Arr[i] = Math.Abs(arr[i] -
arr[i + 1]);
ans += new_Arr[i];
}
Array.Sort(new_Arr);
reverse(new_Arr);
for ( int i = 0; i < K - 1; i++)
{
ans -= new_Arr[i];
}
Console.WriteLine(ans);
}
public static void Main ()
{
int [] arr = { 1, 3, -2, 5, -1 };
int K = 2;
int N = arr.Length;
absoluteDifference(arr, N, K);
}
}
|
Javascript
<script>
function reverse(array)
{
let n = array.length;
for (let i = 0; i < n / 2; i++)
{
let temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
function absoluteDifference(arr, N, K)
{
let new_Arr = new Array(N - 1).fill(0);
let ans = 0;
for (let i = 0; i < N - 1; i++)
{
new_Arr[i] = Math.abs(arr[i] - arr[i + 1]);
ans += new_Arr[i];
}
new_Arr.sort();
reverse(new_Arr);
for (let i = 0; i < K - 1; i++)
{
ans -= new_Arr[i];
}
document.write(ans);
}
let arr = [ 1, 3, -2, 5, -1 ];
let K = 2;
let N = arr.length;
absoluteDifference(arr, N, K);
</script>
|
Time Complexity: O(N * Log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...