# 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++

`// 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*

*filter_none*

**Output:**

6

**Performance Analysis:**

**Time Complexity:***O(N*^{2})**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++

`// 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*

*filter_none*

**Output:**

6

**Performance Analysis:**

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

## Recommended Posts:

- Count of array elements which is smaller than both its adjacent elements
- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum difference of adjacent elements after at most K insertions
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Minimize the number of steps required to reach the end of the array
- Minimize steps required to move all 1's in a matrix to a given index
- Minimize operations required to make each element of Array equal to it's index value
- Count of minimum reductions required to get the required sum K
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Count of lexicographically smaller characters on right
- Count numbers (smaller than or equal to N) with given digit sum
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count of subsets not containing adjacent elements
- Count of strings where adjacent characters are of difference one
- Count of subsets of integers from 1 to N having no adjacent elements
- Count of arrays in which all adjacent elements are such that one of them divide the another
- Count possible combinations of pairs with adjacent elements from first N numbers
- Count binary strings with k times appearing adjacent two set bits
- Count the number of carry operations required to add two numbers

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.