Given an array, arr[] of size N., The task is to perform minimum increment or decrement operations on the elements of the array, to make all the elements consecutive, Output the minimum sum of all the possible changes(addition and subtractions) required to do the same.
Examples:
Input: N = 5, arr[] = {13, 6, 11, 18, 4}
Output: 15
Explanation: Convert 4 to 9, 8 to 10, 13 to12 and 18to13, the new array becomes {9, 10, 11, 12, 13}.
So the sum of changes are abs(9-4) + abs(10-8) + abs(12-13) + abs(13-18) = 15.
Input: N = 2, arr[] = {3, 8}
Output: 4
Approach: The task can be solved using observations. The median of elements of the array should remain unchanged and the rest elements should be changed accordingly such that all elements become consecutive.
Follow the below steps to solve the problem:
- Sort the array
- Take a variable mid which stores the median of the array and a variable pos to store its position.
- Also, take a variable ele and initialize it with the smallest value of the result array i.e. (mid – pos) and a variable sum = 0 to store the sum of all possible changes in the elements of the array.
- Iterate over the array and in each ith iteration:
- Increment sum with abs(arr[i]-ele)(adding the difference of original and required element)
- Increment ele with 1
- Output the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minChanges( int arr[], int N)
{
sort(arr, arr + N);
int mid = arr[N / 2];
int pos = N / 2;
int ele = mid - pos;
int sum = 0;
for ( int i = 0; i < N; i++) {
sum += abs (arr[i] - ele);
ele++;
}
return sum;
}
int main()
{
int N = 5;
int arr[] = { 13, 6, 11, 18, 4 };
cout << minChanges(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int minChanges( int arr[], int N)
{
Arrays.sort(arr);
int mid = arr[N / 2 ];
int pos = N / 2 ;
int ele = mid - pos;
int sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += Math.abs(arr[i] - ele);
ele++;
}
return sum;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 13 , 6 , 11 , 18 , 4 };
int ans = minChanges(arr, N);
System.out.println(ans);
}
}
|
Python3
import math as Math
def minChanges(arr, N):
arr.sort();
mid = arr[N / / 2 ];
pos = N / / 2 ;
ele = mid - pos;
sum = 0 ;
for i in range (N):
sum + = Math.fabs(arr[i] - ele);
ele + = 1
return int ( sum );
N = 5 ;
arr = [ 13 , 6 , 11 , 18 , 4 ];
print (minChanges(arr, N));
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int minChanges( int [ ] arr, int N)
{
Array.Sort(arr);
int mid = arr[N / 2];
int pos = N / 2;
int ele = mid - pos;
int sum = 0;
for ( int i = 0; i < N; i++)
{
sum += Math.Abs(arr[i] - ele);
ele++;
}
return sum;
}
public static void Main(String[] args)
{
int N = 5;
int [ ] arr = { 13, 6, 11, 18, 4 };
Console.WriteLine(minChanges(arr, N));
}
}
|
Javascript
<script>
function minChanges(arr, N) {
arr.sort( function (a, b) { return a - b })
let mid = arr[(Math.floor(N / 2))];
let pos = Math.floor(N / 2);
let ele = mid - pos;
let sum = 0;
for (let i = 0; i < N; i++) {
sum += Math.abs(arr[i] - ele);
ele++;
}
return sum;
}
let N = 5;
let arr = [13, 6, 11, 18, 4];
document.write(minChanges(arr, N));
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Mar, 2023
Like Article
Save Article