Given an array of size N and X. Find minimum moves required to make the array in increasing order. In each move one can add X to any element in the array.
Examples:
Input : a = { 1, 3, 3, 2 }, X = 2
Output : 3
Explanation : Modified array is { 1, 3, 5, 6 }
Input : a = { 3, 5, 6 }, X = 5
Output : 0
Observation:
Let’s take two numbers a and b. a >= b and convert this into a < b by adding some number X.
so, a < b + k*X
( a – b ) / x < k
so, the minimum possible value of k is ( a – b ) / x + 1.
Approach :
Iterate over the given array and take two numbers when a[i] >= a[i-1]
and apply above observation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinimumMoves( int a[], int n, int x)
{
int ans = 0;
for ( int i = 1; i < n; i++) {
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
ans += p;
a[i] += p * x;
}
}
return ans;
}
int main()
{
int arr[] = { 1, 3, 3, 2 };
int x = 2;
int n = sizeof (arr) / sizeof (arr[0]);
cout << MinimumMoves(arr, n, x);
return 0;
}
|
C
#include <stdio.h>
int MinimumMoves( int a[], int n, int x)
{
int ans = 0;
for ( int i = 1; i < n; i++) {
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
ans += p;
a[i] += p * x;
}
}
return ans;
}
int main()
{
int arr[] = { 1, 3, 3, 2 };
int x = 2;
int n = sizeof (arr) / sizeof (arr[0]);
printf ( "%d" ,MinimumMoves(arr, n, x));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static int MinimumMoves( int a[], int n, int x)
{
int ans = 0 ;
for ( int i = 1 ; i < n; i++) {
if (a[i] <= a[i - 1 ]) {
int p = (a[i - 1 ] - a[i]) / x + 1 ;
ans += p;
a[i] += p * x;
}
}
return ans;
}
public static void main(String args[])
{
int arr[] = { 1 , 3 , 3 , 2 };
int x = 2 ;
int n = arr.length;
System.out.println(MinimumMoves(arr, n, x));
}
}
|
Python3
def MinimumMoves(a, n, x) :
ans = 0
for i in range ( 1 , n) :
if a[i] < = a[i - 1 ] :
p = (a[i - 1 ] - a[i]) / / x + 1
ans + = p
a[i] + = p * x
return ans
if __name__ = = "__main__" :
arr = [ 1 , 3 , 3 , 2 ]
x = 2
n = len (arr)
print (MinimumMoves(arr, n, x))
|
C#
using System;
class GFG {
static int MinimumMoves( int [] a, int n, int x)
{
int ans = 0;
for ( int i = 1; i < n; i++) {
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
ans += p;
a[i] += p * x;
}
}
return ans;
}
public static void Main()
{
int [] arr = {1, 3, 3, 2};
int x = 2;
int n = arr.Length;
Console.Write(MinimumMoves(arr, n, x));
}
}
|
PHP
<?php
function MinimumMoves(& $a , $n , $x )
{
$ans = 0;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] <= $a [ $i - 1])
{
$p = ( $a [ $i - 1] - $a [ $i ]) / $x + 1;
$ans += $p ;
$a [ $i ] += $p * $x ;
}
}
return $ans ;
}
$arr = array (1, 3, 3, 2 );
$x = 2;
$n = sizeof( $arr );
echo ((int)MinimumMoves( $arr , $n , $x ));
?>
|
Javascript
<script>
function MinimumMoves(a, n, x)
{
var ans = 0;
for (i = 1; i < n; i++)
{
if (a[i] <= a[i - 1])
{
var p = parseInt((a[i - 1] -
a[i]) / x + 1);
ans += p;
a[i] += p * x;
}
}
return ans;
}
var arr = [ 1, 3, 3, 2 ];
var x = 2;
var n = arr.length;
document.write(MinimumMoves(arr, n, x));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), to iterate over the array where n is the size of the array
- Auxiliary Space: O(1), as no extra space is required