Maximize the cost of reducing array elements
Last Updated :
25 Feb, 2022
Given an array arr[] of N positive integers. We can choose any one index(say K) of the array and reduce all the elements of the array from index 0 to K – 1 by 1. The cost of this operation is K. If at any index(say idx) element is reduced to 0 then we can’t perform this operation in the range [idx, N]. The task is to maximize the cost of reduced operations until we are not able to perform the operation.
Examples:
Input: arr[] = {3, 2, 1, 4, 5}
Output: 8
Explanation:
Firstly, choose k=5 then remove each element from 0 to 4, and array becomes [2, 1, 0, 3, 4].
Secondly, choose k=2 then remove each element from 0 to 1, and array becomes [1, 0, 0, 3, 4].
Finally, choose k=1, then remove each element from 0 to 0, and array becomes [0, 0, 0, 3, 4].
Hence, the answer is 5+2+1=8
Input: arr[] = {4, 3, 2, 1}
Output: 10
Explanation:
Choose k=4 then 3 then 2 then 1, hence answer becomes 4+3+2+1=10
Naive Approach:
- Choose the minimum element from the array in the range [0, N].
- Let the minimum element be at index idx.
- Reduce all the elements by 1 idx number of times and count this operation.
- Now element at index idx is 0. Perform the above operations again by choosing the minimum element in the range [0, idx].
- Repeat the above steps until the first element of the array is non-zero.
- The count of operation in the above steps is the maximu17m cost of operation.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To maximize the cost of the operation always choose the maximum index from the array. While choosing the maximum index(say idx) and performing operations, if at any index the element reduces to zero then reduces the choosing index to idx. Below are the steps:
- Traverse the given array.
- While traversing the array find the minimum element found so far till every index.
- The sum of the minimum element for each index is the total cost of operations required.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxCost( int arr[], int N)
{
int maxi = INT_MAX;
int ans = 0;
for ( int i = 0; i < N; i++)
{
maxi = min(maxi, arr[i]);
ans = ans + maxi;
}
return ans;
}
int main()
{
int N = 4;
int arr[] = { 4, 3, 2, 1 };
int answer = maxCost(arr, N);
cout << (answer);
}
|
Java
class GFG{
public static int maxCost( int arr[], int N)
{
int maxi = Integer.MAX_VALUE;
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
maxi = Math.min(maxi, arr[i]);
ans = ans + maxi;
}
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
int arr[] = { 4 , 3 , 2 , 1 };
int answer = maxCost(arr, N);
System.out.println(answer);
}
}
|
Python3
def maxCost(arr, N):
maxi = float ( "inf" )
ans = 0
for i in range (N):
maxi = min (maxi, arr[i])
ans = ans + maxi
return ans
if __name__ = = '__main__' :
N = 4
arr = [ 4 , 3 , 2 , 1 ]
answer = maxCost(arr, N)
print (answer)
|
C#
using System;
class GFG{
public static int maxCost( int []arr, int N)
{
int maxi = int .MaxValue;
int ans = 0;
for ( int i = 0; i < N; i++)
{
maxi = Math.Min(maxi, arr[i]);
ans = ans + maxi;
}
return ans;
}
public static void Main(String[] args)
{
int N = 4;
int []arr = { 4, 3, 2, 1 };
int answer = maxCost(arr, N);
Console.WriteLine(answer);
}
}
|
Javascript
<script>
function maxCost(arr, N)
{
let maxi = Number.MAX_VALUE;
let ans = 0;
for (let i = 0; i < N; i++)
{
maxi = Math.min(maxi, arr[i]);
ans = ans + maxi;
}
return ans;
}
let N = 4;
let arr = [ 4, 3, 2, 1 ];
let answer = maxCost(arr, N);
document.write(answer);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...