Minimum steps required to reduce all the elements of the array to zero
Given an array arr[] of positive integers, the task is to find the minimum steps to reduce all the elements to 0. In a single step, -1 can be added to all the non-zero elements of the array at the same time.
Examples:
Input: arr[] = {1, 5, 6}
Output: 6
Operation 1: arr[] = {0, 4, 5}
Operation 2: arr[] = {0, 3, 4}
Operation 3: arr[] = {0, 2, 3}
Operation 4: arr[] = {0, 1, 2}
Operation 5: arr[] = {0, 0, 1}
Operation 6: arr[] = {0, 0, 0}
Input: arr[] = {1, 1}
Output: 1
Naive approach: A simple approach is to first sort the array then starting from the minimum element, count the number of steps required to reduce it to 0. This count will then be reduced from the next array element as all the elements will be updated at the same time.
Efficient approach: It can be observed that the minimum number of steps will always be equal to the maximum element from the array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum steps // required to reduce all the elements to 0 int minSteps(int arr[], int n) { // Maximum element from the array int maxVal = *max_element(arr, arr + n); return maxVal; } // Driver code int main() { int arr[] = { 1, 2, 4 }; int n = sizeof(arr) / sizeof(int); cout << minSteps(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // method to get maximum number from array elements static int getMax(int inputArray []) { int maxValue = inputArray[0]; for(int i = 1; i < inputArray.length; i++) { if(inputArray[i] > maxValue) { maxValue = inputArray[i]; } } return maxValue; } // Function to return the minimum steps // required to reduce all the elements to 0 static int minSteps(int arr[], int n) { // Maximum element from the array int maxVal = getMax(arr); return maxVal; } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 4 }; int n = arr.length; System.out.println(minSteps(arr, n)); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the minimum steps # required to reduce all the elements to 0 def minSteps(arr, n): # Maximum element from the array maxVal = max(arr) return maxVal # Driver code arr = [1, 2, 4] n = len(arr) print(minSteps(arr, n)) # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { // method to get maximum number from array elements static int getMax(int []inputArray) { int maxValue = inputArray[0]; for(int i = 1; i < inputArray.Length; i++) { if(inputArray[i] > maxValue) { maxValue = inputArray[i]; } } return maxValue; } // Function to return the minimum steps // required to reduce all the elements to 0 static int minSteps(int []arr, int n) { // Maximum element from the array int maxVal = getMax(arr); return maxVal; } // Driver code public static void Main(String []args) { int []arr = { 1, 2, 4 }; int n = arr.Length; Console.WriteLine(minSteps(arr, n)); } } // This code is contributed by Arnab Kundu |
4
Recommended Posts:
- Minimum number of given operations required to reduce the array to 0 element
- Minimum steps to make all the elements of the array divisible by 4
- Make array elements equal in Minimum Steps
- Minimum number of operations required to reduce N to 1
- Find minimum changes required in an array for it to contain k distinct elements
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of operations required to delete all elements of the array
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all the array elements equal
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Find the minimum number of operations required to make all array elements equal
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
- Generate array with minimum sum which can be deleted in P steps
- Count minimum steps to get the given desired array
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.



