Minimum increment operations to make the array in increasing order
Last Updated :
06 Sep, 2022
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++
// C++ program to find minimum moves required
// to make the array in increasing order
#include <bits/stdc++.h>
using namespace std;
// function to find minimum moves required
// to make the array in increasing order
int MinimumMoves(int a[], int n, int x)
{
// to store answer
int ans = 0;
// iterate over an array
for (int i = 1; i < n; i++) {
// non- increasing order
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
// add moves to answer
ans += p;
// increase the element
a[i] += p * x;
}
}
// return required answer
return ans;
}
// Driver code
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
// C program to find minimum moves required
// to make the array in increasing order
#include <stdio.h>
// function to find minimum moves required
// to make the array in increasing order
int MinimumMoves(int a[], int n, int x)
{
// to store answer
int ans = 0;
// iterate over an array
for (int i = 1; i < n; i++) {
// non- increasing order
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
// add moves to answer
ans += p;
// increase the element
a[i] += p * x;
}
}
// return required answer
return ans;
}
// Driver code
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;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to find minimum moves required
// to make the array in increasing order
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// function to find minimum moves required
// to make the array in increasing order
static int MinimumMoves(int a[], int n, int x)
{
// to store answer
int ans = 0;
// iterate over an array
for (int i = 1; i < n; i++) {
// non- increasing order
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
// add moves to answer
ans += p;
// increase the element
a[i] += p * x;
}
}
// return required answer
return ans;
}
// Driver code
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
# Python3 program to find minimum
# moves required to make the array
# in increasing order
# function to find minimum moves required
# to make the array in increasing order
def MinimumMoves(a, n, x) :
# to store answer
ans = 0
# iterate over an array
for i in range(1, n) :
# non- increasing order
if a[i] <= a[i - 1] :
p = (a[i - 1] - a[i]) // x + 1
# add moves to answer
ans += p
# increase the element
a[i] += p * x
# return required answer
return ans
# Driver code
if __name__ == "__main__" :
arr = [1, 3, 3, 2]
x = 2
n = len(arr)
print(MinimumMoves(arr, n, x))
# This code is contributed by ANKITRAI1
C#
// C# program to find minimum moves required
// to make the array in increasing order
using System;
class GFG {
// function to find minimum moves required
// to make the array in increasing order
static int MinimumMoves(int[] a, int n, int x)
{
// to store answer
int ans = 0;
// iterate over an array
for (int i = 1; i < n; i++) {
// non- increasing order
if (a[i] <= a[i - 1]) {
int p = (a[i - 1] - a[i]) / x + 1;
// add moves to answer
ans += p;
// increase the element
a[i] += p * x;
}
}
// return required answer
return ans;
}
// Driver code
public static void Main()
{
int[] arr = {1, 3, 3, 2};
int x = 2;
int n = arr.Length;
Console.Write(MinimumMoves(arr, n, x));
}
}
// This code is contributed by ChitraNayal
PHP
<?php
// PHP program to find minimum
// moves required to make the
// array in increasing order
// function to find minimum
// moves required to make the
// array in increasing order
function MinimumMoves(&$a, $n, $x)
{
// to store answer
$ans = 0;
// iterate over an array
for ($i = 1; $i < $n; $i++)
{
// non- increasing order
if ($a[$i] <= $a[$i - 1])
{
$p = ($a[$i - 1] - $a[$i]) / $x + 1;
// add moves to answer
$ans += $p;
// increase the element
$a[$i] += $p * $x;
}
}
// return required answer
return $ans;
}
// Driver code
$arr = array(1, 3, 3, 2 );
$x = 2;
$n = sizeof($arr);
echo ((int)MinimumMoves($arr, $n, $x));
// This code is contributed
// by Shivi_Aggarwal
?>
JavaScript
<script>
// Javascript program to find minimum
// moves required to make the array in
// increasing order
// Function to find minimum moves required
// to make the array in increasing order
function MinimumMoves(a, n, x)
{
// To store answer
var ans = 0;
// Tterate over an array
for(i = 1; i < n; i++)
{
// Non- increasing order
if (a[i] <= a[i - 1])
{
var p = parseInt((a[i - 1] -
a[i]) / x + 1);
// Add moves to answer
ans += p;
// Increase the element
a[i] += p * x;
}
}
// Return required answer
return ans;
}
// Driver code
var arr = [ 1, 3, 3, 2 ];
var x = 2;
var n = arr.length;
document.write(MinimumMoves(arr, n, x));
// This code is contributed by aashish1995
</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
Similar Reads
Minimum increments to make the array non-decreasing Given an array A[] of size N along with an integer M such that 0 <= A[i] < M, the task is to output minimum number of operations required to sort A[] to non-decreasing order using following operation, choose an element let say A[i] and update it as (A[i] + 1) mod M. Examples: Input: N = 4, M =
7 min read
Minimum increment or decrement operations required to make the array sorted Given an array arr[] of N integers, the task is to sort the array in non-decreasing order by performing the minimum number of operations. In a single operation, an element of the array can either be incremented or decremented by 1. Print the minimum number of operations required.Examples: Input: arr
15+ min read
Minimize increment operations to make Array non-decreasing Given an array arr[] of n integers. Modify the array such that every element is at least as large as the previous element. This can be done by increasing the value of any element by 1. The task is to find the minimum number of moves required to make the array non-decreasing. Examples: Input: n = 5,
5 min read
Minimum increment/decrement to make array non-Increasing Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in the minimum changes possible. Examples : Input : a[] = {3, 1, 2, 1}Output : 1Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of a
11 min read
Minimum pair merge operations required to make Array non-increasing Given an array arr[], the task is to find the minimum number of operations required in which two adjacent elements are removed from the array and replaced by their sum, such that the array is converted to a non-increasing array.Note: An array with a single element is considered non-increasing.Exampl
7 min read