Maximum score of Array using increasing subsequence and subarray with given conditions
Last Updated :
19 Dec, 2021
Given an array arr[]. The task is to find the maximum score that can be achieved from arr[] for i=[1, N-2]. The conditions for scoring are given below.
- If arr[0…j] < arr[i] < arr[i+1…N-1], then score = 2.
- If arr[i-1] < arr[i] < arr[i+1] and previous condition is not satisfied, then score = 1.
- If none of the conditions holds, then score = 0.
Examples:
Input: arr[] = {1, 2, 3}
Output: 2
Explanation: The score of arr[1] equals 2, which is maximum possible.
Input: arr[] = {2, 4, 6, 4}
Output: 1
Explanation: For each index i in the range 1 <= i <= 2:
The score of nums[1] equals 1.
The score of nums[2] equals 0.
Hence 1 is the maximum possible score.
Approach: This problem can be solved by using Prefix Max and Suffix Min. Follow the steps below to solve the given problem.
- For an element score to be 2, it should be greater than every element on its left and smaller than every element on its right.
- So Precompute to find prefix max and suffix min for each array element.
- Now check for each array arr[] element at i:
- If it is greater than prefix max at i-1, and smaller than suffix min at i+1, the score will be 2.
- else if it is greater than arr[i-1] and smaller than arr[i+1], score will be 1.
- else score will be 0.
- Sum up all the scores and return that as the final answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxScore(vector< int >& nums)
{
int n = nums.size(), i;
int ans = 0;
vector< int > pre(n, 0);
vector< int > suf(n, 0);
pre[0] = nums[0];
for (i = 1; i < n; i++)
pre[i] = max(pre[i - 1], nums[i]);
suf[n - 1] = nums[n - 1];
for (i = n - 2; i >= 0; i--)
suf[i] = min(suf[i + 1], nums[i]);
for (i = 1; i < n - 1; i++) {
if (nums[i] > pre[i - 1]
&& nums[i] < suf[i + 1])
ans += 2;
else if (nums[i] > nums[i - 1]
&& nums[i] < nums[i + 1])
ans += 1;
}
return ans;
}
int main()
{
int N = 3;
vector< int > arr = { 1, 2, 3 };
cout << maxScore(arr);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int maxScore(ArrayList<Integer> nums)
{
int n = nums.size(), i = 0 ;
int ans = 0 ;
int [] pre = new int [n];
int [] suf = new int [n];
pre[ 0 ] = ( int )nums.get( 0 );
for (i = 1 ; i < n; i++)
pre[i] = Math.max(pre[i - 1 ], ( int )nums.get(i));
suf[n - 1 ] = ( int )nums.get(n - 1 );
for (i = n - 2 ; i >= 0 ; i--)
suf[i] = Math.min(suf[i + 1 ], ( int )nums.get(i));
for (i = 1 ; i < n - 1 ; i++) {
if (( int )nums.get(i) > pre[i - 1 ]
&& ( int )nums.get(i) < suf[i + 1 ])
ans += 2 ;
else if (( int )nums.get(i) > ( int )nums.get(i - 1 )
&& ( int )nums.get(i) < ( int )nums.get(i + 1 ))
ans += 1 ;
}
return ans;
}
public static void main(String args[])
{
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add( 1 );
arr.add( 2 );
arr.add( 3 );
System.out.println(maxScore(arr));
}
}
|
Python3
def maxScore(nums):
n = len (nums)
ans = 0
pre = [ 0 for _ in range (n)]
suf = [ 0 for _ in range (n)]
pre[ 0 ] = nums[ 0 ]
for i in range ( 1 , n):
pre[i] = max (pre[i - 1 ], nums[i])
suf[n - 1 ] = nums[n - 1 ]
for i in range (n - 2 , - 1 , - 1 ):
suf[i] = min (suf[i + 1 ], nums[i])
for i in range ( 1 , n - 1 ):
if (nums[i] > pre[i - 1 ] and nums[i] < suf[i + 1 ]):
ans + = 2
elif (nums[i] > nums[i - 1 ] and nums[i] < nums[i + 1 ]):
ans + = 1
return ans
if __name__ = = "__main__" :
N = 3
arr = [ 1 , 2 , 3 ]
print (maxScore(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxScore(List< int > nums)
{
int n = nums.Count, i = 0;
int ans = 0;
int [] pre = new int [n];
int [] suf = new int [n];
pre[0] = nums[0];
for (i = 1; i < n; i++)
pre[i] = Math.Max(pre[i - 1], nums[i]);
suf[n - 1] = nums[n - 1];
for (i = n - 2; i >= 0; i--)
suf[i] = Math.Min(suf[i + 1], nums[i]);
for (i = 1; i < n - 1; i++) {
if (nums[i] > pre[i - 1]
&& nums[i] < suf[i + 1])
ans += 2;
else if (nums[i] > nums[i - 1]
&& nums[i] < nums[i + 1])
ans += 1;
}
return ans;
}
public static void Main()
{
List< int > arr = new List< int >() { 1, 2, 3 };
Console.WriteLine(maxScore(arr));
}
}
|
Javascript
<script>
function maxScore(nums) {
let n = nums.length, i;
let ans = 0;
let pre = new Array(n).fill(0)
let suf = new Array(n).fill(0);
pre[0] = nums[0];
for (i = 1; i < n; i++)
pre[i] = Math.max(pre[i - 1], nums[i]);
suf[n - 1] = nums[n - 1];
for (i = n - 2; i >= 0; i--)
suf[i] = Math.min(suf[i + 1], nums[i]);
for (i = 1; i < n - 1; i++) {
if (nums[i] > pre[i - 1]
&& nums[i] < suf[i + 1])
ans += 2;
else if (nums[i] > nums[i - 1]
&& nums[i] < nums[i + 1])
ans += 1;
}
return ans;
}
let N = 3;
let arr = [1, 2, 3];
document.write(maxScore(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...