Minimize prize count required such that smaller value gets less prize in an adjacent pair

Given an array arr[] of length N, the task is to find the minimum number of prizes required such that if two elements are adjacent, then elements with smaller value gets a less number of prizes compared to its adjacent elements with greater value.
Note: Each elements will get at least one prize.
Examples:

Input: arr[] = {1, 2, 2, 3}
Output: 6
Explanation:
Element at index {0} will get {1} prize.
Element at index {1} will get {2} prizes.
Element at index {2} will get {1} prizes.
Element at index {3} will get {2} prizes.
So, the total number of prizes required to satisfy
the above conditions are 6

Input: arr[] = {3, 2, 2, 1}
Output: 6
Explanation:
Element at index {0} will get {2} prize.
Element at index {1} will get {1} prizes.
Element at index {2} will get {2} prizes.
Element at index {3} will get {1} prizes.
So, the total number of prizes required to satisfy
the above conditions are 6



Naive Approach: Traverse over the elements of the array and for each element of the array find the consecutive smaller elements at the left of the element and find the consecutive smaller elements on the right of that index.

Prize at index i = max(Consecutive smaller elements at left, Consecutive smaller elements at right, 1)

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
int findMinPrizes(int arr[], int n)
{
    int totalPrizes = 0, j, x, y;
  
    // Loop to iterate over every
    // elements of the array
    for (int i = 0; i < n; i++) {
        x = 1;
        j = i;
  
        // Loop to find the consecutive
        // smaller elements at left
        while (j > 0 && arr[j] > arr[j - 1]) {
            x++;
            j--;
        }
        j = i;
        y = 1;
  
        // Loop to find the consecutive
        // smaller elements at right
        while (j < n - 1 && arr[j] > arr[j + 1]) {
            y++;
            j++;
        }
  
        totalPrizes += max({ x, y });
    }
    cout << totalPrizes << endl;
  
    return 0;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    findMinPrizes(arr, n);
}

chevron_right


Output:

6

Performance Analysis:

  • Time Complexity: O(N2)
  • Auxiliary Space: O(1)

Efficient Approach: The idea is to precompute the count of consecutive smaller elements at left and right for every element of the array. It means that, if an element on the left is smaller, then all the smaller elements at the left of that element will also be smaller to the current element. i.e.

if (arr[i-1] < arr[i])
    smallerLeft[i] = smallerLeft[i-1] + 1

Similarly, the consecutive smaller elements can be computed using the fact that, if an element on the right is greater than the current element then the consecutive greater elements on the right will also be greater than the current element. i.e.

if (arr[i] < arr[i+1])
    smallerRight[i] = smallerRight[i+1] + 1

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to find the
// minimum prizes required such
// that adjacent smaller elements
// gets less number of prizes
  
#include <bits/stdc++.h>
  
using namespace std;
  
// Function to find the minimum
// number of required such that
// adjacent smaller elements gets
// less number of prizes
int minPrizes(int arr[], int n)
{
    int dpLeft[n];
  
    dpLeft[0] = 1;
  
    // Loop to compute the smaller
    // elements at the left
    for (int i = 1; i < n; i++) {
  
        if (arr[i] > arr[i - 1]) {
  
            dpLeft[i] = dpLeft[i - 1] + 1;
        }
        else {
  
            dpLeft[i] = 1;
        }
    }
  
    int dpRight[n];
  
    dpRight[n - 1] = 1;
  
    // Loop to find the smaller
    // elements at the right
    for (int i = n - 2; i >= 0; i--) {
  
        if (arr[i] > arr[i + 1]) {
  
            dpRight[i] = dpRight[i + 1] + 1;
        }
        else {
  
            dpRight[i] = 1;
        }
    }
  
    int totalPrizes = 0;
  
    // Loop to find the minimum
    // prizes required
    for (int i = 0; i < n; i++) {
  
        totalPrizes += max(dpLeft[i],
                           dpRight[i]);
    }
    cout << totalPrizes << endl;
  
    return 0;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 2, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    minPrizes(arr, n);
  
    return 0;
}

chevron_right


Output:

6

Performance Analysis:

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

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.