Sunday, October 26, 2025
HomeData Modelling & AIMinimum steps required to reduce all the elements of the array to...

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

 

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


Javascript




<script>
// Javascript implementation of the approach
 
// Function to return the minimum steps
// required to reduce all the elements to 0
function 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>


Output: 

4

 

Time Complexity: O(n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS