 Open in App
Not now

# Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0

• Difficulty Level : Easy
• Last Updated : 18 Nov, 2021

Given a circular array arr[] of N integers and an array cost[], the task is to calculate the minimum number of operations required to make all elements of the array equal to 0, where at each operation decrement the value of an index i by 1. If the value of an index becomes 0, decrement the value of arr[i+1] by cost[i], decrement the value of arr[i + 2] by cost[i + 1] and so on.

Note: If an element becomes less than 0, it is considered to be 0.

Example:

Input: arr[] = {7, 2, 5}, cost[] = {8, 9, 3}
Output: 6
Explanation: Decrements can be made in the following way:

• Decrement the value of arr twice. Hence, the value of arr will be decremented by cost and the value of arr will be decremented by cost. Therefore, the final array will be arr[] = {4, 0, 0}.
• Now, decrement the value of arr, 4 times to make it 0. Therefore, the array becomes arr[] = {0, 0, 0}.

Hence, the number of required operations to make all the elements of the array equal to zero is 6 which is the minimum possible.

Input: arr[] = {6, 7, 7, 10, 8, 2}, cost[] = {5, 10, 1, 4, 7, 7}
Output: 16

Approach: The given problem can be solved using a Greedy Approach by the following observations:

• The last remaining non-zero element of the array arr[], suppose x will require x decrement operations.
• Suppose after the decrement operations, the value of arr[i] becomes 0, then for arr[i], the required number of decrements are arr[i], for arr[i+1], the required number of decrements will be arr[i+1] – max(0, arr[i+1] – cost[i]) and so on.

Below are the steps to follow:

• Traverse the given array arr[] in the range [0, N) using a variable i.
• The number of operations required the make the ith index of the array equal to zero are arr[i] – min(arr[i], cost[i-1]). Therefore, maintain the sum of this value over all the indices in a variable ans.
• Increment the value of ans by the minimum value of arr[i] or cost[i] over all the indices in the range [0, N).

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find minimum decrements``// required to make all array elements 0``int` `minDecrements(``int` `arr[], ``int` `powr[], ``int` `N)``{``    ``// Variable to store the answer``    ``int` `ans = 0;``    ``int` `mn = INT_MAX;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``int` `idx = (i + 1) % N;``        ``int` `val = min(arr[idx], powr[i]);` `        ``ans += arr[idx] - val;` `        ``// Store the minimum one``        ``mn = min(mn, val);``    ``}``    ``ans += mn;` `    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 6, 7, 7, 10, 8, 2 };``    ``int` `powr[] = { 5, 10, 1, 4, 7, 7 };` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);` `    ``cout << minDecrements(arr, powr, N);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `public` `class` `GFG``{``// Function to find minimum decrements``// required to make all array elements 0``static` `int` `minDecrements(``int` `[]arr, ``int` `[]powr, ``int` `N)``{``    ``// Variable to store the answer``    ``int` `ans = ``0``;``    ``int` `mn = Integer.MAX_VALUE;` `    ``// Traverse the array``    ``for` `(``int` `i = ``0``; i < N; i++) {``        ``int` `idx = (i + ``1``) % N;``        ``int` `val = Math.min(arr[idx], powr[i]);` `        ``ans += arr[idx] - val;` `        ``// Store the minimum one``        ``mn = Math.min(mn, val);``    ``}``    ``ans += mn;` `    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `main(String args[])``{``    ``int` `[]arr = { ``6``, ``7``, ``7``, ``10``, ``8``, ``2` `};``    ``int` `[]powr = { ``5``, ``10``, ``1``, ``4``, ``7``, ``7` `};``    ``int` `N = arr.length;` `    ``System.out.println(minDecrements(arr, powr, N));``}``}``// This code is contributed by Samim Hossain Mondal.`

## Python3

 `# Python program for the above approach` `# Function to find minimum decrements``# required to make all array elements 0``def` `minDecrements(arr, powr, N):` `    ``# Variable to store the answer``    ``ans ``=` `0``    ``mn ``=` `99999999` `    ``# Traverse the array``    ``for` `i ``in` `range``(N):``        ``idx ``=` `(i ``+` `1``) ``%` `N``        ``val ``=` `min``(arr[idx], powr[i])` `        ``ans ``+``=` `arr[idx] ``-` `val` `       ``# Store the minimum one``        ``mn ``=` `min``(mn, val)``    ``ans ``+``=` `mn` `    ``# Return the ans``    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``6``, ``7``, ``7``, ``10``, ``8``, ``2``]``    ``powr ``=` `[``5``, ``10``, ``1``, ``4``, ``7``, ``7``]``    ``N ``=` `len``(arr)``    ``print``(minDecrements(arr, powr, N))` `# This code is contributed by Potta Lokesh`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections;` `class` `GFG``{``// Function to find minimum decrements``// required to make all array elements 0``static` `int` `minDecrements(``int` `[]arr, ``int` `[]powr, ``int` `N)``{``    ``// Variable to store the answer``    ``int` `ans = 0;``    ``int` `mn = Int32.MaxValue;` `    ``// Traverse the array``    ``for` `(``int` `i = 0; i < N; i++) {``        ``int` `idx = (i + 1) % N;``        ``int` `val = Math.Min(arr[idx], powr[i]);` `        ``ans += arr[idx] - val;` `        ``// Store the minimum one``        ``mn = Math.Min(mn, val);``    ``}``    ``ans += mn;` `    ``// Return the ans``    ``return` `ans;``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `[]arr = { 6, 7, 7, 10, 8, 2 };``    ``int` `[]powr = { 5, 10, 1, 4, 7, 7 };``    ``int` `N = arr.Length;` `    ``Console.Write(minDecrements(arr, powr, N));``}``}``// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`16`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up