Longest subsequence with first and last element greater than all other elements | Python implementation of the above approach ; Function to update the value in Fenwick tree ; Function to return the query result ; Function to return the length of subsequence ; Store the value in struct Array ; If less than 2 elements are present return that element . ; Set the left and right pointers to extreme ; Calculate left and right pointer index . ; Store the queries from [ L + 1 , R - 1 ] . ; Sort array and queries for fenwick updates ; For each query calculate maxx for the answer and store it in ans array . ; if queries [ i ] . index < q : ; Mx will be mx + 2 as while calculating mx , we excluded element at index left and right ; Driver Code
from typing import List NEW_LINE class Query : NEW_LINE INDENT def __init__ ( self ) -> None : NEW_LINE INDENT self . l = 0 NEW_LINE self . r = 0 NEW_LINE self . x = 0 NEW_LINE self . index = 0 NEW_LINE DEDENT DEDENT class Arrays : NEW_LINE INDENT def __init__ ( self ) -> None : NEW_LINE INDENT self . val = 0 NEW_LINE self . index = 0 NEW_LINE DEDENT DEDENT def update ( Fenwick : List [ int ] , index : int , val : int , n : int ) -> None : NEW_LINE INDENT while ( index <= n ) : NEW_LINE INDENT Fenwick [ index ] += val NEW_LINE index += index & ( - index ) NEW_LINE DEDENT DEDENT def query ( Fenwick : List [ int ] , index : int , n : int ) -> int : NEW_LINE INDENT summ = 0 NEW_LINE while ( index > 0 ) : NEW_LINE INDENT summ = summ + Fenwick [ index ] NEW_LINE index -= index & ( - index ) NEW_LINE DEDENT return summ NEW_LINE DEDENT def maxLength ( n : int , v : List [ int ] ) -> int : NEW_LINE INDENT where = [ 0 for _ in range ( n + 2 ) ] NEW_LINE arr = [ Arrays ( ) for _ in range ( n ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT v [ i - 1 ] = v [ i - 1 ] - 1 NEW_LINE x = v [ i - 1 ] NEW_LINE where [ x ] = i - 1 NEW_LINE arr [ i - 1 ] . val = x NEW_LINE arr [ i - 1 ] . index = i - 1 NEW_LINE DEDENT if ( n <= 2 ) : NEW_LINE INDENT print ( n ) NEW_LINE return 0 NEW_LINE DEDENT left = n NEW_LINE right = 0 NEW_LINE mx = 0 NEW_LINE queries = [ Query ( ) for _ in range ( 4 * n ) ] NEW_LINE j = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT left = min ( left , where [ i ] ) NEW_LINE right = max ( right , where [ i ] ) NEW_LINE diff = right - left NEW_LINE if ( diff == 0 or diff == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT val1 = v [ left ] NEW_LINE val2 = v [ right ] NEW_LINE minn = min ( val1 , val2 ) NEW_LINE queries [ j ] . l = left + 1 NEW_LINE queries [ j ] . r = right - 1 NEW_LINE queries [ j ] . x = minn NEW_LINE queries [ j ] . index = j NEW_LINE j += 1 NEW_LINE DEDENT Fenwick = [ 0 for _ in range ( n + 1 ) ] NEW_LINE q = j - 1 NEW_LINE arr [ : n + 1 ] . sort ( key = lambda x : x . val ) NEW_LINE queries [ : q + 1 ] . sort ( key = lambda val : val . x ) NEW_LINE curr = 0 NEW_LINE ans = [ 0 for _ in range ( q + 1 ) ] NEW_LINE for i in range ( q + 1 ) : NEW_LINE INDENT while ( arr [ curr ] . val <= queries [ i ] . x and curr < n ) : NEW_LINE INDENT update ( Fenwick , arr [ curr ] . index + 1 , 1 , n ) NEW_LINE curr += 1 NEW_LINE DEDENT ans [ queries [ i ] . index ] = query ( Fenwick , queries [ i ] . r + 1 , n ) - query ( Fenwick , queries [ i ] . l , n ) NEW_LINE DEDENT for i in range ( q + 1 ) : NEW_LINE INDENT mx = max ( mx , ans [ i ] ) NEW_LINE DEDENT mx = mx + 2 NEW_LINE return mx NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE v = [ 4 , 2 , 6 , 5 , 3 , 1 ] NEW_LINE print ( maxLength ( n , v ) ) NEW_LINE DEDENT