GFG App
Open App
Browser
Continue

# Minimize increment or decrement operations to make given Array elements consecutive

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++

 // C++ program for the above approach #include using namespace std;    // Function to output sum of all the // required changes in array int minChanges(int arr[], int N) {     sort(arr, arr + N);        // Stores median of array     int mid = arr[N / 2];        // Variable for position of median     int pos = N / 2;        // Smallest element of     // the required array     int ele = mid - pos;     int sum = 0;        // Loop to find  sum of changes     for (int i = 0; i < N; i++) {            // Adding difference of original         // and required element to answer         sum += abs(arr[i] - ele);         ele++;     }     return sum; }    // Driver code int main() {     int N = 5;     int arr[] = { 13, 6, 11, 18, 4 };        cout << minChanges(arr, N);     return 0; }

## Java

 // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG {      // Function to output sum of all the   // required changes in array   static int minChanges(int arr[], int N)   {     Arrays.sort(arr);        // Stores median of array     int mid = arr[N / 2];        // Variable for position of median     int pos = N / 2;        // Smallest element of     // the required array     int ele = mid - pos;     int sum = 0;        // Loop to find  sum of changes     for (int i = 0; i < N; i++)      {          // Adding difference of original       // and required element to answer       sum += Math.abs(arr[i] - ele);       ele++;     }     return sum;   }      // Driver code   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);   } }    // This code is contributed by hrithikgarg03188

## Python3

 # Python code for the above approach  import math as Math      # Function to output sum of all the # required changes in array def minChanges(arr, N):     arr.sort();        # Stores median of array     mid = arr[N // 2];        # Variable for position of median     pos = N // 2;        # Smallest element of     # the required array     ele = mid - pos;     sum = 0;        # Loop to find  sum of changes     for i in range(N):            # Adding difference of original         # and required element to answer         sum += Math.fabs(arr[i] - ele);         ele += 1     return int(sum);    # Driver code N = 5; arr = [13, 6, 11, 18, 4]; print(minChanges(arr, N));    # This code is contributed by Saurabh Jaiswal

## C#

 // C# program for above approach using System; using System.Collections.Generic;    public class GFG {        // Function to output sum of all the   // required changes in array   static int minChanges(int[ ] arr, int N)   {     Array.Sort(arr);        // Stores median of array     int mid = arr[N / 2];        // Variable for position of median     int pos = N / 2;        // Smallest element of     // the required array     int ele = mid - pos;     int sum = 0;        // Loop to find  sum of changes     for (int i = 0; i < N; i++)     {                // Adding difference of original       // and required element to answer       sum += Math.Abs(arr[i] - ele);       ele++;     }     return sum;   }      // Driver code   public static void Main(String[] args)   {     int N = 5;     int[ ] arr = { 13, 6, 11, 18, 4 };        Console.WriteLine(minChanges(arr, N));   } }    // This code is contributed by hrithikgarg03188

## Javascript



Output

15

Time Complexity: O(N * logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up