Minimum decrements to make an Array at most 0 such that all array elements are cyclically decremented after a number is reduced to 0
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[1] twice. Hence, the value of arr[2] will be decremented by cost[1] and the value of arr[0] will be decremented by cost[2]. Therefore, the final array will be arr[] = {4, 0, 0}.
- Now, decrement the value of arr[0], 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++
#include <bits/stdc++.h>
using namespace std;
int minDecrements( int arr[], int powr[], int N)
{
int ans = 0;
int mn = INT_MAX;
for ( int i = 0; i < N; i++) {
int idx = (i + 1) % N;
int val = min(arr[idx], powr[i]);
ans += arr[idx] - val;
mn = min(mn, val);
}
ans += mn;
return ans;
}
int main()
{
int arr[] = { 6, 7, 7, 10, 8, 2 };
int powr[] = { 5, 10, 1, 4, 7, 7 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minDecrements(arr, powr, N);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int minDecrements( int []arr, int []powr, int N)
{
int ans = 0 ;
int mn = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
int idx = (i + 1 ) % N;
int val = Math.min(arr[idx], powr[i]);
ans += arr[idx] - val;
mn = Math.min(mn, val);
}
ans += mn;
return ans;
}
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));
}
}
|
Python3
def minDecrements(arr, powr, N):
ans = 0
mn = 99999999
for i in range (N):
idx = (i + 1 ) % N
val = min (arr[idx], powr[i])
ans + = arr[idx] - val
mn = min (mn, val)
ans + = mn
return ans
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))
|
C#
using System;
using System.Collections;
class GFG
{
static int minDecrements( int []arr, int []powr, int N)
{
int ans = 0;
int mn = Int32.MaxValue;
for ( int i = 0; i < N; i++) {
int idx = (i + 1) % N;
int val = Math.Min(arr[idx], powr[i]);
ans += arr[idx] - val;
mn = Math.Min(mn, val);
}
ans += mn;
return ans;
}
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));
}
}
|
Javascript
<script>
function minDecrements(arr, powr, N)
{
let ans = 0;
let mn = Number.MAX_SAFE_INTEGER
for (let i = 0; i < N; i++) {
let idx = (i + 1) % N;
let val = Math.min(arr[idx], powr[i]);
ans += arr[idx] - val;
mn = Math.min(mn, val);
}
ans += mn;
return ans;
}
let arr = [ 6, 7, 7, 10, 8, 2 ];
let powr = [ 5, 10, 1, 4, 7, 7 ];
let N = arr.length;
document.write(minDecrements(arr, powr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...