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:3Explanation:

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(N^{2}) **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
**0**, convert the number^{th}index**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 array` `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 += ` `abs` `(a[i] - a[i - 1]);` ` ` `else` ` ` `ans += ` `abs` `(a[i]);` ` ` `}` ` ` `// Return the result` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 1, 2, 3, 4 };` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << min_operation(arr, n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to calculate the minimum` `// steps to obtain the desired array` `static` `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 Code` `public` `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` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to calculate the minimum` `# steps to obtain the desired array` `def` `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 Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `]` ` ` `n ` `=` `len` `(arr)` ` ` `print` `(min_operation(arr, n))` `# This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# Program to implement` `// the above approach` `using` `System;` `class` `GFG{` `// Function to calculate the minimum` `// steps to obtain the desired array` `static` `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 Code` `public` `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` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Count of decrement operations required to obtain K in N steps
- Minimum Steps to obtain N from 1 by the given operations
- Minimum count of elements required to obtain the given Array by repeated mirror operations
- Minimum number of operations required to obtain a given Binary String
- Minimize Steps required to obtain Sorted Order of an Array
- Minimize operations required to obtain N
- Minimum increments by index value required to obtain at least two equal Array elements
- Minimum count of digits required to obtain given Sum
- Minimum steps required to rearrange given array to a power sequence of 2
- Minimum number of given operations required to reduce the array to 0 element
- Minimum steps required to reduce all the elements of the array to zero
- Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings
- Minimum number of operations required to delete all elements of the array
- Find the minimum number of operations required to make all array elements equal
- Minimum steps to reduce N to 0 by given operations
- Minimum increment/decrement operations required on Array to satisfy given conditions
- Minimize the number of steps required to reach the end of the array | Set 2
- Count of Ways to obtain given Sum from the given Array elements
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K

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.