# Count of suffix increment/decrement operations to construct a given array

• Difficulty Level : Medium
• Last Updated : 09 Apr, 2021

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| operations. Once this is done, arr, arr, arr, … arr[n] = brr.
• To make Second element equal we have to make |brr – brr| operations. Once this is done, arr, arr, arr, … arr[n] = brr.

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| + |b – b| + |b – b| + … + |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 ``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);``    ``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

 ` 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

 ``

Output :

`3`

Time complexity = O(n).

My Personal Notes arrow_drop_up