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 6Input: 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); } |
Java
// Java implementation to find the // minimum prizes required such // that adjacent smaller elements // gets less number of prizes import java.util.*; class GFG{ // Function to find the minimum // number of required such that // adjacent smaller elements gets // less number of prizes static 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 += Math.max(x, y ); } System.out.print(totalPrizes + "\n" ); return 0 ; } // Driver Code public static void main(String[] args) { int arr[] = { 1 , 2 , 2 , 3 }; int n = arr.length; findMinPrizes(arr, n); } } // This code is contributed by gauravrajput1 |
Python3
# Python3 implementation to find the # minimum prizes required such # that adjacent smaller elements # gets less number of prizes # Function to find the minimum # number of required such that # adjacent smaller elements gets # less number of prizes def findMinPrizes(arr, n): totalPrizes = 0 # Loop to iterate over every # elements of the array for i in range (n): x = 1 j = i # Loop to find the consecutive # smaller elements at left while (j > 0 and arr[j] > arr[j - 1 ]): x + = 1 j - = 1 j = i y = 1 # Loop to find the consecutive # smaller elements at right while (j < n - 1 and arr[j] > arr[j + 1 ]): y + = 1 j + = 1 totalPrizes + = max (x, y) print (totalPrizes) # Driver code arr = [ 1 , 2 , 2 , 3 ] n = len (arr) findMinPrizes(arr, n) # This code is contributed by stutipathak31jan |
C#
// C# implementation to find the // minimum prizes required such // that adjacent smaller elements // gets less number of prizes using System; class GFG{ // Function to find the minimum // number of required such that // adjacent smaller elements gets // less number of prizes static 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 += Math.Max(x, y); } Console.Write(totalPrizes + "\n" ); return 0; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 2, 3 }; int n = arr.Length; findMinPrizes(arr, n); } } // This code is contributed by Amit Katiyar |
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++
// 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; } |
Java
// Java implementation to find the // minimum prizes required such // that adjacent smaller elements // gets less number of prizes import java.util.*; class GFG{ // Function to find the minimum // number of required such that // adjacent smaller elements gets // less number of prizes static int minPrizes( int arr[], int n) { int []dpLeft = new int [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 = new int [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 += Math.max(dpLeft[i], dpRight[i]); } System.out.print(totalPrizes + "\n" ); return 0 ; } // Driver Code public static void main(String[] args) { int arr[] = { 1 , 2 , 2 , 3 }; int n = arr.length; minPrizes(arr, n); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 implementation to find the # minimum prizes required such # that adjacent smaller elements # gets less number of prizes # Function to find the minimum # number of required such that # adjacent smaller elements gets # less number of prizes def minPrizes(arr, n): dpLeft = [ 0 ] * n dpLeft[ 0 ] = 1 # Loop to compute the smaller # elements at the left for i in range (n): if arr[i] > arr[i - 1 ]: dpLeft[i] = dpLeft[i - 1 ] + 1 else : dpLeft[i] = 1 dpRight = [ 0 ] * n dpRight[ - 1 ] = 1 # Loop to find the smaller # elements at the right for i in range (n - 2 , - 1 , - 1 ): if arr[i] > arr[i + 1 ]: dpRight[i] = dpRight[i + 1 ] + 1 else : dpRight[i] = 1 totalPrizes = 0 # Loop to find the minimum # prizes required for i in range (n): totalPrizes + = max (dpLeft[i], dpRight[i]) print (totalPrizes) # Driver code arr = [ 1 , 2 , 2 , 3 ] n = len (arr) minPrizes(arr, n) # This code is contributed by stutipathak31jan |
C#
// C# implementation to find the // minimum prizes required such // that adjacent smaller elements // gets less number of prizes using System; class GFG{ // Function to find the minimum // number of required such that // adjacent smaller elements gets // less number of prizes static int minPrizes( int []arr, int n) { int []dpLeft = new int [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 = new int [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 += Math.Max(dpLeft[i], dpRight[i]); } Console.Write(totalPrizes + "\n" ); return 0; } // Driver Code public static void Main(String[] args) { int []arr = { 1, 2, 2, 3 }; int n = arr.Length; minPrizes(arr, n); } } // This code is contributed by Rajput-Ji |
6
Performance Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)