Minimum increments or decrements by 1 required to make all array elements in AP
Last Updated :
07 Sep, 2021
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:
- Increment array element arr[0](= 19) by 1.
- Decrement array element arr[1](= 16) by 1.
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
int findMinimumMoves( int N, int arr[])
{
if (N <= 2) {
return 0;
}
int res = N + 1;
for ( int a = -1; a <= 1; a++) {
for ( int b = -1; b <= 1; b++) {
int changes = 0;
if (a != 0) {
changes++;
}
if (b != 0) {
changes++;
}
int orig = (arr[0] + a);
int diff = (arr[1] + b) - (arr[0] + a);
bool good = true ;
for ( int i = 2; i < N; i++) {
int actual = orig + i * diff;
if ( abs (actual - arr[i]) > 1) {
good = false ;
break ;
}
if (actual != arr[i])
changes++;
}
if (!good)
continue ;
res = min(res, changes);
}
}
if (res > N)
res = -1;
return res;
}
int main()
{
int arr[] = { 19, 16, 9, 5, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findMinimumMoves(N, arr);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int findMinimumMoves( int N, int arr[])
{
if (N <= 2 )
{
return 0 ;
}
int res = N + 1 ;
for ( int a = - 1 ; a <= 1 ; a++)
{
for ( int b = - 1 ; b <= 1 ; b++)
{
int changes = 0 ;
if (a != 0 )
{
changes++;
}
if (b != 0 )
{
changes++;
}
int orig = (arr[ 0 ] + a);
int diff = (arr[ 1 ] + b) - (arr[ 0 ] + a);
boolean good = true ;
for ( int i = 2 ; i < N; i++)
{
int actual = orig + i * diff;
if (Math.abs(actual - arr[i]) > 1 )
{
good = false ;
break ;
}
if (actual != arr[i])
changes++;
}
if (!good)
continue ;
res = Math.min(res, changes);
}
}
if (res > N)
res = - 1 ;
return res;
}
public static void main(String[] args)
{
int arr[] = { 19 , 16 , 9 , 5 , 0 };
int N = arr.length;
System.out.println(findMinimumMoves(N, arr));
}
}
|
Python3
def findMinimumMoves(N, arr):
if (N < = 2 ):
return 0
res = N + 1
for a in range ( - 1 , 2 , 1 ):
for b in range ( - 1 , 2 , 1 ):
changes = 0
if (a ! = 0 ):
changes + = 1
if (b ! = 0 ):
changes + = 1
orig = (arr[ 0 ] + a)
diff = (arr[ 1 ] + b) - (arr[ 0 ] + a)
good = True
for i in range ( 2 , N, 1 ):
actual = orig + i * diff
if ( abs (actual - arr[i]) > 1 ):
good = False
break
if (actual ! = arr[i]):
changes + = 1
if (good = = False ):
continue
res = min (res, changes)
if (res > N):
res = - 1
return res
if __name__ = = '__main__' :
arr = [ 19 , 16 , 9 , 5 , 0 ]
N = len (arr)
print (findMinimumMoves(N, arr))
|
C#
using System;
class GFG{
static int findMinimumMoves( int N, int [] arr)
{
if (N <= 2)
{
return 0;
}
int res = N + 1;
for ( int a = -1; a <= 1; a++)
{
for ( int b = -1; b <= 1; b++)
{
int changes = 0;
if (a != 0)
{
changes++;
}
if (b != 0)
{
changes++;
}
int orig = (arr[0] + a);
int diff = (arr[1] + b) - (arr[0] + a);
bool good = true ;
for ( int i = 2; i < N; i++)
{
int actual = orig + i * diff;
if (Math.Abs(actual - arr[i]) > 1)
{
good = false ;
break ;
}
if (actual != arr[i])
changes++;
}
if (!good)
continue ;
res = Math.Min(res, changes);
}
}
if (res > N)
res = -1;
return res;
}
static public void Main()
{
int [] arr = { 19, 16, 9, 5, 0 };
int N = arr.Length;
Console.WriteLine(findMinimumMoves(N, arr));
}
}
|
Javascript
<script>
function findMinimumMoves(N, arr)
{
if (N <= 2)
{
return 0;
}
let res = N + 1;
for (let a = -1; a <= 1; a++)
{
for (let b = -1; b <= 1; b++)
{
let changes = 0;
if (a != 0)
{
changes++;
}
if (b != 0)
{
changes++;
}
let orig = (arr[0] + a);
let diff = (arr[1] + b) - (arr[0] + a);
let good = true ;
for (let i = 2; i < N; i++)
{
let actual = orig + i * diff;
if (Math.abs(actual - arr[i]) > 1)
{
good = false ;
break ;
}
if (actual != arr[i])
changes++;
}
if (!good)
continue ;
res = Math.min(res, changes);
}
}
if (res > N)
res = -1;
return res;
}
let arr = [ 19, 16, 9, 5, 0 ];
let N = arr.length;
document.write(findMinimumMoves(N, arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...