Maximum score of Array using increasing subsequence and subarray with given conditions Last Updated : 19 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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: 2Explanation: The score of arr[1] equals 2, which is maximum possible. Input: arr[] = {2, 4, 6, 4}Output: 1Explanation: 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++ // C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to find maximum score int maxScore(vector<int>& nums) { // Size of array int n = nums.size(), i; int ans = 0; // Prefix max vector<int> pre(n, 0); // Suffix min 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; } // Driver Code int main() { int N = 3; vector<int> arr = { 1, 2, 3 }; // Function Call cout << maxScore(arr); return 0; } Java // Java program for above approach import java.util.*; public class GFG { // Function to find maximum score static int maxScore(ArrayList<Integer> nums) { // Size of array int n = nums.size(), i = 0; int ans = 0; // Prefix max int[] pre = new int[n]; // Suffix min 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; } // Driver Code public static void main(String args[]) { ArrayList<Integer> arr = new ArrayList<Integer>(); arr.add(1); arr.add(2); arr.add(3); // Function Call System.out.println(maxScore(arr)); } } // This code is contributed by Samim Hossain Mondal. Python3 # python program for above approach # Function to find maximum score def maxScore(nums): # Size of array n = len(nums) ans = 0 # Prefix max pre = [0 for _ in range(n)] # Suffix min 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 # Driver Code if __name__ == "__main__": N = 3 arr = [1, 2, 3] # Function Call print(maxScore(arr)) # This code is contributed by rakeshsahni C# // C# program for above approach using System; using System.Collections.Generic; class GFG { // Function to find maximum score static int maxScore(List<int> nums) { // Size of array int n = nums.Count, i = 0; int ans = 0; // Prefix max int[] pre = new int[n]; // Suffix min 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; } // Driver Code public static void Main() { List<int> arr = new List<int>() { 1, 2, 3 }; // Function Call Console.WriteLine(maxScore(arr)); } } // This code is contributed by ukasp. JavaScript <script> // JavaScript code for the above approach // Function to find maximum score function maxScore(nums) { // Size of array let n = nums.length, i; let ans = 0; // Prefix max let pre = new Array(n).fill(0) // Suffix min 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; } // Driver Code let N = 3; let arr = [1, 2, 3]; // Function Call document.write(maxScore(arr)); // This code is contributed by Potta Lokesh </script> Output2 Time Complexity: O(N) Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Maximum score of Array using increasing subsequence and subarray with given conditions C code_r Follow Improve Article Tags : Misc Searching Mathematical DSA Arrays subsequence subarray +3 More Practice Tags : ArraysMathematicalMiscSearching Similar Reads Length of the longest increasing subsequence which does not contain a given sequence as Subarray Given two arrays arr[] and arr1[] of lengths N and M respectively, the task is to find the longest increasing subsequence of array arr[] such that it does not contain array arr1[] as subarray. Examples: Input: arr[] = {5, 3, 9, 3, 4, 7}, arr1[] = {3, 3, 7}Output: 4Explanation: Required longest incre 14 min read Minimize the number of strictly increasing subsequences in an array | Set 2 Given an array arr[] of size N, the task is to print the minimum possible count of strictly increasing subsequences present in the array. Note: It is possible to swap the pairs of array elements. Examples: Input: arr[] = {2, 1, 2, 1, 4, 3}Output: 2Explanation: Sorting the array modifies the array to 6 min read Maximize the Sum of a Subsequence from an Array based on given conditions Given an array a[] consisting of N integers, the task is to perform the following operations: Select a subsequence and for every pth element of the subsequence, calculate the product p * a[i].Calculate the sum of the calculated values of p * a[i].The subsequence should be selected such that it maxim 7 min read Longest increasing subsequence which forms a subarray in the sorted representation of the array Given an array arr[] of N integers, the task is to find the length of the longest increasing subsequence such that it forms a subarray when the original array is sorted. Examples: Input: arr[] = { 2, 6, 4, 8, 2, 9 }Output: 3Explanation: Sorted array: {2, 2, 4, 6, 8, 9}All possible non-decreasing seq 11 min read Maximum sum subsequence of any size which is decreasing-increasing alternatively Given an array of integers arr[], find the subsequence with maximum sum whose elements are first decreasing, then increasing, or vice versa, The subsequence can start anywhere in the main sequence, not necessarily at the first element of the main sequence. A sequence {x1, x2, .. xn} is an alternatin 15+ min read Maximum length sub-array which satisfies the given conditions Given a binary array arr[], the task is to find the length of the longest sub-array of the given array such that if the sub-array is divided into two equal-sized sub-arrays then both the sub-arrays either contain all 0s or all 1s. For example, the two sub-arrays must be of the form {0, 0, 0, 0} and 12 min read Maximize count of Decreasing Consecutive Subsequences from an Array Given an array arr[] consisting of N integers, the task is to find the maximum count of decreasing subsequences possible from an array that satisfies the following conditions: Each subsequence is in its longest possible form.The difference between adjacent elements of the subsequence is always 1. Ex 8 min read Maximum sum increasing subsequence from a prefix and a given element after prefix is must Given an array of n positive integers, write a program to find the maximum sum of increasing subsequence from prefix till ith index and also including a given kth element which is after i, i.e., k > i. Examples : Input: arr[] = {1, 101, 2, 3, 100, 4, 5} i-th index = 4 (Element at 4th index is 100 14 min read Count of subarrays starting or ending at an index i such that arr[i] is maximum in subarray Given an array arr[] consisting of N integers, the task is to find the number of subarrays starting or ending at an index i such that arr[i] is the maximum element of the subarray. Examples: Input: arr[] = {3, 4, 1, 6, 2}Output: 1 3 1 5 1Explanation: The subarray starting or ending at index 0 and wi 11 min read Maximum size of sub-array that satisfies the given condition Given an array arr[] of integers. The task is to return the length of the maximum size sub-array such that either one of the condition is satisfied: arr[k] > arr[k + 1] when k is odd and arr[k] < arr[k + 1] when k is even.arr[k] > arr[k + 1] when k is even and arr[k] < arr[k + 1] when k 6 min read Like