Minimum steps required to reduce all the elements of the array to zero - GeeksforGeeks

19 Mar, 2022
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++
Java
Python3
C#
Javascript
// 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 0int minSteps(int arr[], int n){     // Maximum element from the array    int maxVal = *max_element(arr, arr + n);    return maxVal;} // Driver codeint main(){    int arr[] = { 1, 2, 4 };    int n = sizeof(arr) / sizeof(int);     cout << minSteps(arr, n);     return 0;}
// Java implementation of the approachclass 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 implementation of the approach # Function to return the minimum steps# required to reduce all the elements to 0def minSteps(arr, n):     # Maximum element from the array    maxVal = max(arr)    return maxVal # Driver codearr = [1, 2, 4]n = len(arr) print(minSteps(arr, n)) # This code is contributed by Mohit Kumar
// C# implementation of the approachusing 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
<script>// Javascript implementation of the approach // Function to return the minimum steps// required to reduce all the elements to 0function minSteps(arr, n){     // Maximum element from the array    let maxVal = Math.max(...arr);    return maxVal;} // Driver code    let arr = [ 1, 2, 4 ];    let n = arr.length;     document.write(minSteps(arr, n)); </script>
4
 
Time Complexity: O(n)
Auxiliary Space: O(1)
mohit kumar 29
ankthon
andrew1234
subhammahato348
subham348
Arrays
Greedy
Mathematical
Sorting
Arrays
Greedy
Mathematical
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Multidimensional Arrays in Java
Introduction to Arrays
Linear Search
Python | Using 2D arrays/lists the right way
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string