Maximum range length such that A [ i ] is maximum in given range for all i from [ 1 , N ] | C # program for the above approach . ; Function to find maximum range for each i such that arr [ i ] is max in range ; Vector to store the left and right index for each i such that left [ i ] > arr [ i ] and right [ i ] > arr [ i ] ; Traverse the array ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify left [ i ] ; Clear the stack ; Traverse the array to find right [ i ] for each i ; While s . top ( ) . first < a [ i ] remove the top element from the stack ; Modify right [ i ] ; Print the value range for each i ; adding elements in firstlist ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void MaxRange ( List < int > A , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack s = new Stack ( ) ; s . Push ( new Tuple < int , int > ( Int32 . MaxValue , - 1 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ( ( Tuple < int , int > ) s . Peek ( ) ) . Item1 < A [ i ] ) s . Pop ( ) ; left [ i ] = ( ( Tuple < int , int > ) s . Peek ( ) ) . Item2 ; s . Push ( new Tuple < int , int > ( A [ i ] , i ) ) ; } while ( s . Count > 0 ) s . Pop ( ) ; s . Push ( new Tuple < int , int > ( Int32 . MaxValue , n ) ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ( ( Tuple < int , int > ) s . Peek ( ) ) . Item1 < A [ i ] ) s . Pop ( ) ; right [ i ] = ( ( Tuple < int , int > ) s . Peek ( ) ) . Item2 ; s . Push ( new Tuple < int , int > ( A [ i ] , i ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . WriteLine ( ( left [ i ] + 1 ) + " ▁ " + ( right [ i ] - 1 ) ) ; } } static void Main ( ) { List < int > arr = new List < int > ( ) ; arr . Add ( 1 ) ; arr . Add ( 3 ) ; arr . Add ( 2 ) ; int n = arr . Count ; MaxRange ( arr , n ) ; } }