Open In App

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:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
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 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




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




<script>
      // JavaScript code for the above approach 
 
 
      // Function to output sum of all the
      // required changes in array
      function minChanges(arr, N) {
          arr.sort(function (a, b) { return a - b })
 
          // Stores median of array
          let mid = arr[(Math.floor(N / 2))];
 
          // Variable for position of median
          let pos = Math.floor(N / 2);
 
          // Smallest element of
          // the required array
          let ele = mid - pos;
          let sum = 0;
 
          // Loop to find  sum of changes
          for (let 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
 
      let N = 5;
      let arr = [13, 6, 11, 18, 4];
 
      document.write(minChanges(arr, N));
 
// This code is contributed by Potta Lokesh
  </script>

Output
15

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


Article Tags :