Open In App

Maximize the cost of reducing array elements

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: 

  1. Choose the minimum element from the array in the range [0, N].
  2. Let the minimum element be at index idx.
  3. Reduce all the elements by 1 idx number of times and count this operation.
  4. Now element at index idx is 0. Perform the above operations again by choosing the minimum element in the range [0, idx].
  5. Repeat the above steps until the first element of the array is non-zero.
  6. 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:

  1. Traverse the given array.
  2. While traversing the array find the minimum element found so far till every index.
  3. 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++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
    
// Function that finds the maximum cost
// of all the operations
int maxCost(int arr[], int N)
{
     
    // Initialise maxi with positive
    // integer value
    int maxi = INT_MAX;
     
    // Initialise the answer variable
    int ans = 0;
     
    // Iterate linearly in the array
    for(int i = 0; i < N; i++)
    {
         
        // Find minimum at each step
        maxi = min(maxi, arr[i]);
         
        // Add maximum to ans
        ans = ans + maxi;
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
int main()
{
     
    // Length of the array
    int N = 4;
     
    // Given array arr[]
    int arr[] = { 4, 3, 2, 1 };
     
    // Function call
    int answer = maxCost(arr, N);
     
    // Print the result
    cout << (answer);
}
 
// This code is contributed by princiraj1992




// Java program for the above approach
class GFG{
    
// Function that finds the maximum cost
// of all the operations
public static int maxCost(int arr[], int N)
{
     
    // Initialise maxi with positive
    // integer value
    int maxi = Integer.MAX_VALUE;
     
    // Initialise the answer variable
    int ans = 0;
     
    // Iterate linearly in the array
    for(int i = 0; i < N; i++)
    {
         
        // Find minimum at each step
        maxi = Math.min(maxi, arr[i]);
         
        // Add maximum to ans
        ans = ans + maxi;
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Length of the array
    int N = 4;
     
    // Given array arr[]
    int arr[] = { 4, 3, 2, 1 };
     
    // Function call
    int answer = maxCost(arr, N);
     
    // Print the result
    System.out.println(answer);
}
}
 
// This code is contributed by stutipathak31jan




# Python3 program for the above approach
 
# Function that finds the maximum cost
# of all the operations
def maxCost(arr, N):
 
    # Initialize maxi with positive
    # infinity value
    maxi = float("inf")
 
    # Initialise the answer variable
    ans = 0
 
    # Iterate linearly in the array
    for i in range(N):
 
        # Find minimum at each step
        maxi = min(maxi, arr[i])
 
        # Add maximum to ans
        ans = ans + maxi
 
 
# Return the answer
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Length of the array
    N = 4
 
    # Given array arr[]
    arr = [4, 3, 2, 1]
 
    # Function call
    answer = maxCost(arr, N)
 
    # Print the result
    print(answer)




// C# program for the above approach
using System;
 
class GFG{
     
// Function that finds the maximum cost
// of all the operations
public static int maxCost(int []arr, int N)
{
     
    // Initialise maxi with positive
    // integer value
    int maxi = int.MaxValue;
     
    // Initialise the answer variable
    int ans = 0;
     
    // Iterate linearly in the array
    for(int i = 0; i < N; i++)
    {
         
        // Find minimum at each step
        maxi = Math.Min(maxi, arr[i]);
         
        // Add maximum to ans
        ans = ans + maxi;
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Length of the array
    int N = 4;
     
    // Given array []arr
    int []arr = { 4, 3, 2, 1 };
     
    // Function call
    int answer = maxCost(arr, N);
     
    // Print the result
    Console.WriteLine(answer);
}
}
 
// This code is contributed by Rajput-Ji




<script>
 
// JavaScript program for the above approach
 
 
// Function that finds the maximum cost
// of all the operations
function maxCost(arr, N)
{
     
    // Initialise maxi with positive
    // integer value
    let maxi = Number.MAX_VALUE;
     
    // Initialise the answer variable
    let ans = 0;
     
    // Iterate linearly in the array
    for(let i = 0; i < N; i++)
    {
         
        // Find minimum at each step
        maxi = Math.min(maxi, arr[i]);
         
        // Add maximum to ans
        ans = ans + maxi;
    }
     
    // Return the answer
    return ans;
}
 
// Driver code
 
// Length of the array
let N = 4;
     
// Given array arr[]
let arr = [ 4, 3, 2, 1 ];
     
// Function call
let answer = maxCost(arr, N);
     
// Print the result
document.write(answer);
 
 
// This code is contributed by princiraj1992
 
</script>

Output: 
10

 

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


Article Tags :