Given an array arr[] of N positive integers, the task is to find the minimum number of operations required of the following types to obtain the array arr[] from an array of zeroes only.
- Select any index i and increment all the elements at the indices [i, N – 1] by 1.
- Select any index i and decrease all the elements at the indices [i, N – 1] by 1.
Examples:
Input: arr[]={1,1,2,2,1}
Output: 3
Explanation:
Initially arr[] = {0,0,0,0,0}
Step 1: arr[]={1,1,1,1,1}(Operation 1 on index 0)
Step 2: arr[]={1,1,2,2,2}(Operation 1 on index 2)
Step 3: arr[]={1,1,2,2,1}(Operation 2 on index 4)
Input: arr[]={1,2,3,4}
Output: 4
Naive Approach: The simplest approach is to convert each element of the array result array, brr[] to arr[] by performing one of the above operations over the indices [i, N – 1] and increase the count of each operation performed.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be optimized using Greedy Approach.
Follow the steps below to solve the problem:
- For the 0th index, convert the number 0 to arr[0]. Therefore, the number of steps required will always be a[0]. Hence, add arr[0] to the answer.
- For all the other indices, the common greedy observation is to use the increase or the decrease operation by abs(a[i]-a[i-1]) times.
- The intuition behind this approach is, if the number is less than a[i-1], then increase everything from ((i-1).. n-1) by a[i], and then decrease (a[i-1] – a[i]) to get a[i].
- If a[i] > a[i-1], the approach is to use the increase operation from ((i-1)..n-1) by a[i-1], and for the remaining value, we increase it by (a[i]-a[i-1]) operations from (i..(n-1)).
- Therefore, traverse the array and for every element after the first, add the absolute difference of consecutive pairs to the answer.
- Finally, print the answer.
Below is the implementation of the above approach:
C++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to calculate the minimum// steps to obtain the desired arrayint min_operation(int a[], int n){ // Initialize variable int ans = 0; // Iterate over the array arr[] for (int i = 0; i < n; i++) { // Check if i > 0 if (i > 0) // Update the answer ans += abs(a[i] - a[i - 1]); else ans += abs(a[i]); } // Return the result return ans;}// Driver Codeint main(){ int arr[] = { 1, 2, 3, 4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << min_operation(arr, n); return 0;} |
Java
// Java Program to implement// the above approachimport java.util.*;class GFG{// Function to calculate the minimum// steps to obtain the desired arraystatic int min_operation(int a[], int n){ // Initialize variable int ans = 0; // Iterate over the array arr[] for (int i = 0; i < n; i++) { // Check if i > 0 if (i > 0) // Update the answer ans += Math.abs(a[i] - a[i - 1]); else ans += Math.abs(a[i]); } // Return the result return ans;}// Driver Codepublic static void main(String[] args){ int arr[] = { 1, 2, 3, 4 }; int n = arr.length; System.out.print(min_operation(arr, n));}}// This code is contributed by gauravrajput1 |
Python3
# Python3 program to implement# the above approach# Function to calculate the minimum# steps to obtain the desired arraydef min_operation(a, n): # Initialize variable ans = 0 # Iterate over the array arr[] for i in range(n): # Check if i > 0 if (i > 0): # Update the answer ans += abs(a[i] - a[i - 1]) else: ans += abs(a[i]) # Return the result return ans# Driver Codeif __name__ == "__main__": arr = [ 1, 2, 3, 4 ] n = len(arr) print(min_operation(arr, n))# This code is contributed by chitranayal |
C#
// C# Program to implement// the above approachusing System;class GFG{// Function to calculate the minimum// steps to obtain the desired arraystatic int min_operation(int []a, int n){ // Initialize variable int ans = 0; // Iterate over the array []arr for (int i = 0; i < n; i++) { // Check if i > 0 if (i > 0) // Update the answer ans += Math.Abs(a[i] - a[i - 1]); else ans += Math.Abs(a[i]); } // Return the result return ans;}// Driver Codepublic static void Main(String[] args){ int []arr = { 1, 2, 3, 4 }; int n = arr.Length; Console.Write(min_operation(arr, n));}}// This code is contributed by Amit Katiyar |
Output:
4
Time Complexity: O(N)
Auxiliary Space: O(1)
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.


