VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-increments-or-decrements-by-1-required-to-make-all-array-elements-in-ap/

⇱ Minimum increments or decrements by 1 required to make all array elements in AP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum increments or decrements by 1 required to make all array elements in AP

Last Updated : 23 Jul, 2025

Given an array arr[] consisting of N integers, the task is to find the minimum number of increment/decrement by 1 required to be performed on array elements to make all the elements of the given array arr[] in AP. If it is not possible to make the array in AP, then print "-1".

Examples:

Input: arr[] = {19, 16, 9, 5, 0}
Output: 3
Explanation:
Following are the order of increment/decrements of array elements is made:

  1. Increment array element arr[0](= 19) by 1.
  2. Decrement array element arr[1](= 16) by 1.
  3. Increment array element arr[2](= 9) by 1.

After the above operations, the array arr[] modifies to {20, 15, 10, 5, 0}, which is in AP with first term 20 and common difference -5. Therefore, the total count of element is 3.

Input: arr[] = {1, 2, 3, 4, 10}
Output: -1

Approach: The given problem can be solved by finding the first term and the common difference from the first two elements and then check if all elements can be changed to the AP sequence with the given first term and common difference by simply iterating over the array. Follow the steps below to solve the problem: 

  • If N is less than equal to 2, then print 0, because each such sequence is an Arithmetic Progression.
  • Initialize a variable say, res as N + 1 to store the answer.
  • Iterate in the range [-1, 1] using the variable a and perform the following steps: 
    • Iterate in the range [-1, 1] using the variable b and perform the following steps:
      • Initialize a variable say changes as 0 to store the count of changed elements of the array arr[].
      • If a is not equal to 0, then increase the value of changes by 1.
      • If b is not equal to 0, then increase the value of changes by 1.
      • Initialize a variable say, orig as arr[0] + a to store the first element and diff as (arr[1] + b) - (arr[0] + a) to store the common difference of the arithmetic progression.
      • Initialize a variable say, good as true to store whether the arithmetic progression sequence with first term orig and common difference diff is possible or not.
      • Iterate in the range  [2, N-1] using the variable i and perform the following steps:
        • Initialize a variable actual as orig+i*diff to store the actual element of the arithmetic progression at index i.
        • If abs(actual - arr[i]) is greater than 1, then such arithmetic progression is unreachable. Then Set the value of good to false and break the loop.
        • Else, if actual is not equal to arr[i], increase the value of changes by 1.
      • After iterating through the inner for loop, update the value of res to min(changes, res).
  • After completing the above steps, if res is greater than N then assign -1 to res. Otherwise, print the value of res as the answer.

Below is the implementation of the above approach:


Output: 
3

 

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

Comment