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 ?> |
Output :
3
Time complexity = O(n).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of distinct substrings of a string using Suffix Array
- kasai’s Algorithm for Construction of LCP array from Suffix Array
- Count of distinct substrings of a string using Suffix Trie
- Count of possible arrays from prefix-sum and suffix-sum arrays
- Construct an array from GCDs of consecutive elements in given array
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Index with Minimum sum of prefix and suffix sums in an Array
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Construct a distinct elements array with given size, sum and element upper bound
- Construct sum-array with sum of elements in given range
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Construct the smallest possible Array with given Sum and XOR
- Construct the Array using given bitwise AND, OR and XOR
- Construct an array from XOR of all elements of array except element at same index
- Longest common suffix of two linked lists
- Queries for number of distinct integers in Suffix
- Count the number of ways to construct the target string
- Maximum count of equal numbers in an array after performing given operations
- Count of operations required to update the array such that it satisfies the given conditions
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

