Count of suffix increment/decrement operations to construct a given array
Last Updated :
22 Sep, 2022
Given an array of non-negative integers. We need to construct given array from an array of all zeros. We are allowed to do following operation.
- Choose any index of say i and add 1 to all the elements or subtract 1 from all the elements from index i to last index. We basically increase/decrease a suffix by 1.
Examples :
Input : brr[] = {1, 2, 3, 4, 5}
Output : 5
Here, we can successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes.
Input : brr[] = {1, 2, 2, 1}
Output : 3
Here, we choose indices 1 and 2 and adds 1 to corresponding suffixes, then we choose index 4 and subtract 1.
Let brr[] be given array and arr[] be current array (which is initially 0).
The approach is simple:
- To make first element equal we have to make |brr[1]| operations. Once this is done, arr[2], arr[3], arr[4], ... arr[n] = brr[1].
- To make Second element equal we have to make |brr[2] - brr[1]| operations. Once this is done, arr[3], arr[4], arr[5], ... arr[n] = brr[2].
In general, to make arr[i] = brr[i] we need to make |brr[i] - b[i - 1]| operations. So in total we have to make |b[1]| + |b[2] - b[1]| + |b[3] - b[2]| + ... + |b[n] - b[n - 1]| operations.
Below is CPP and Java implementation of the above approach:
C++
// CPP program to find minimum number of steps
// to make the array equal to the given array.
#include <bits/stdc++.h>
using namespace std;
// function to calculate min_Steps
int minSteps(int arr[], int n)
{
int min_Steps = 0;
for (int i = 0; i < n; i++) {
if (i > 0)
min_Steps += abs(arr[i] - arr[i - 1]);
// first element of arr.
else
min_Steps += abs(arr[i]);
}
return min_Steps;
}
// driver function
int main()
{
int arr[] = { 1, 2, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << minSteps(arr, n) << endl;
}
Java
// Java program to find minimum number of steps
// to make the array equal to the given array.
import java.util.*;
import java.lang.*;
public class GfG {
// function to calculate min_Steps
public static int minSteps(int arr[], int n)
{
int min_Steps = 0;
for (int i = 0; i < n; i++) {
if (i > 0)
min_Steps +=
Math.abs(arr[i] - arr[i - 1]);
// first element of arr.
else
min_Steps += Math.abs(arr[i]);
}
return min_Steps;
}
// driver function
public static void main(String argc[])
{
int[] arr = new int[] { 1, 2, 2, 1 };
int n = 4;
System.out.println(minSteps(arr, n));
}
}
Python3
# Python 3 program to find minimum number
# of steps to make the array equal to the
# given array.
# function to calculate min_Steps
def minSteps(arr, n):
min_Steps = 0
for i in range(n):
if (i > 0):
min_Steps += abs(arr[i] -
arr[i - 1])
# first element of arr.
else:
min_Steps += abs(arr[i])
return min_Steps
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 2, 1 ]
n = len(arr)
print(minSteps(arr, n))
# This code is contributed
# by PrinciRaj19992
C#
// C# program to find minimum number of steps
// to make the array equal to the given array.
using System;
public class GfG {
// function to calculate min_Steps
public static int minSteps(int[] arr, int n)
{
int min_Steps = 0;
for (int i = 0; i < n; i++) {
if (i > 0)
min_Steps += Math.Abs(arr[i] - arr[i - 1]);
// first element of arr.
else
min_Steps += Math.Abs(arr[i]);
}
return min_Steps;
}
// driver function
public static void Main()
{
int[] arr = new int[] { 1, 2, 2, 1 };
int n = 4;
Console.WriteLine(minSteps(arr, n));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to find minimum
// number of steps to make the
// array equal to the given array.
// function to calculate min_Steps
function minSteps($arr, $n)
{
$min_Steps = 0;
for ($i = 0; $i < $n; $i++)
{
if ($i > 0)
$min_Steps += abs($arr[$i] -
$arr[$i - 1]);
// first element of arr.
else
$min_Steps += abs($arr[$i]);
}
return $min_Steps;
}
// Driver Code
$arr = array( 1, 2, 2, 1 );
$n = sizeof($arr) ;
echo minSteps($arr, $n),"\n";
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript program to find minimum number of steps
// to make the array equal to the given array.
// function to calculate min_Steps
function minSteps(arr, n)
{
let min_Steps = 0;
for (let i = 0; i < n; i++) {
if (i > 0)
min_Steps += Math.abs(arr[i] - arr[i - 1]);
// first element of arr.
else
min_Steps += Math.abs(arr[i]);
}
return min_Steps;
}
let arr = [ 1, 2, 2, 1 ];
let n = arr.length;
document.write(minSteps(arr, n));
// This code is contributed by divyeshrabadiya07.
</script>
Time complexity: O(n), where N is the number of elements in the given array.
Auxiliary space: O(1) because it is using constant space
Similar Reads
Count minimum decrement prefix or suffix or increment all operations to make Array equal to 0 Given an array arr[] of size N. The task is to make all the array elements equal to zero by applying the minimum number of operations. Following operations are allowed: Select an index i and decrease each element by 1 for the prefix up to that index.Select an index i and decrease each element by 1 f
7 min read
Count of elements in array A left after performing deletion/rotation operation based on given conditions Given two binary arrays, A[] and B[] of size N respectively, the task is to find the number of elements in array A[] that will be left after performing the following operation until no elements can be deleted: If the starting elements of array A[] and B[] are equal, then delete both the elements.Oth
9 min read
Count of indices in Array having all prefix elements less than all in suffix Given an array arr[], the task is to calculate the total number of indices where all elements in the left part is less than all elements in the right part of the array. Examples: Input: arr[] = {1, 5, 4, 2, 3, 8, 7, 9}Output: 3Explanation: Lets consider left part = [1], right part = [5, 4, 2, 3, 8,
12 min read
Count of non decreasing Arrays with ith element in range [A[i], B[i]] Given two arrays A[] and B[] both consisting of N integers, the task is to find the number of non-decreasing arrays of size N that can be formed such that each array element lies over the range [A[i], B[i]]. Examples: Input: A[] = {1, 1}, B[] = {2, 3}Output: 5Explanation:The total number of valid ar
9 min read
Counts 1s that can be obtained in an Array by performing given operations Given an array arr[] of size N consisting of only of 0s initially, the task is to count the number of 1s that can be obtained in the array by performing the following operation N times. In i th operation, flip all the array elements whose index ( 1-based indexing ) is a multiple of i. Examples: Inpu
9 min read