codeparrot/xlcost-text-to-code huggingface dataset:
Count ancestors with smaller value for each node of a Binary Tree | C # program for the above approach ; Function to add an edge between nodes u and v ; Function to perform the DFS Traversal and store parent of each node ; Store the immediate parent ; Traverse the children of the current node ; Recursively call for function dfs for the child node ; Function to count the number of ancestors with values smaller than that of the current node ; Stores the parent of each node ; Perform the DFS Traversal ; Traverse all the nodes ; Store the number of ancestors smaller than node ; Loop until parent [ node ] != - 1 ; If the condition satisfies , increment cnt by 1 ; Print the required result for the current node ; Driver Code ; Tree Formation
using System ; using System . Collections . Generic ; class GFG { static void add_edge ( List < List < int > > adj , int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void dfs ( List < int > parent , List < List < int > > adj , int u , int par = - 1 ) { parent [ u ] = par ; foreach ( int child in adj [ u ] ) { if ( child != par ) dfs ( parent , adj , child , u ) ; } } static void countSmallerAncestors ( List < List < int > > adj , int n ) { List < int > parent = new List < int > ( ) ; for ( int i = 0 ; i < ( int ) ( 1e5 ) ; i ++ ) { parent . Add ( 0 ) ; } dfs ( parent , adj , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int node = i ; int cnt = 0 ; while ( parent [ node ] != - 1 ) { if ( parent [ node ] < i ) cnt += 1 ; node = parent [ node ] ; } Console . Write ( cnt + " ▁ " ) ; } } static void Main ( ) { int N = 6 ; List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < ( int ) ( 1e5 ) ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } add_edge ( adj , 1 , 5 ) ; add_edge ( adj , 1 , 4 ) ; add_edge ( adj , 4 , 6 ) ; add_edge ( adj , 5 , 3 ) ; add_edge ( adj , 5 , 2 ) ; countSmallerAncestors ( adj , N ) ; } }
Count subsequences having odd Bitwise XOR values from an array | C # program for above approach ; Function to count the subsequences having odd bitwise XOR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array A [ ] ; If el is odd ; If count of odd elements is 0 ; Driver code ; Given array A [ ] ; Function call to count subsequences having odd bitwise XOR value
using System ; public class GFG { public static void countSubsequences ( int [ ] A ) { int odd = 0 ; int even = 0 ; foreach ( int el in A ) { if ( el % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 ) Console . WriteLine ( 0 ) ; else Console . WriteLine ( 1 << ( A . Length - 1 ) ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 3 , 4 } ; countSubsequences ( A ) ; } }
Maximum subarray product modulo M | C # program for above approach ; Function to find maximum subarray product modulo M and minimum length of the subarray ; Stores maximum subarray product modulo M and minimum length of the subarray ; Stores the minimum length of subarray having maximum product ; Traverse the array ; Stores the product of a subarray ; Calculate Subarray whose start index is i ; Multiply product by arr [ i ] ; If product greater than ans ; Update ans ; Update length ; Print maximum subarray product mod M ; Print minimum length of subarray having maximum product ; Driver code
using System ; class GFG { static void maxModProdSubarr ( int [ ] arr , int n , int M ) { int ans = 0 ; int length = n ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product = ( product * arr [ i ] ) % M ; if ( product > ans ) { ans = product ; if ( length > j - i + 1 ) { length = j - i + 1 ; } } } } Console . WriteLine ( " Maximum ▁ subarray ▁ product ▁ is ▁ " + ans ) ; Console . WriteLine ( " Minimum ▁ length ▁ of ▁ the ▁ maximum ▁ " + " product ▁ subarray ▁ is ▁ " + length ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 2 } ; int N = arr . Length ; int M = 5 ; maxModProdSubarr ( arr , N , M ) ; } }
Number of co | C # program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Driver Code ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs
using System ; using System . Collections . Generic ; class GFG { static int GCD ( int a , int b ) { return b == 0 ? a : GCD ( b , a % b ) ; } static bool coprime ( int a , int b ) { if ( GCD ( a , b ) == 1 ) return true ; return false ; } static int numOfPairs ( List < string > arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( coprime ( Int32 . Parse ( arr [ i ] ) , Int32 . Parse ( arr [ j ] ) ) ) { count = count + 1 ; } } } return count ; } static void noOfCoPrimePairs ( int N , int d1 , int d2 ) { List < string > l = new List < string > ( ) ; l . Add ( d1 . ToString ( ) ) ; l . Add ( d2 . ToString ( ) ) ; l . Sort ( ) ; if ( N < Int32 . Parse ( l [ 1 ] ) ) return ; List < string > total = new List < string > ( l ) ; List < string > temp2 = new List < string > ( l ) ; int flag = 0 ; List < string > temp3 = new List < string > ( ) ; while ( l [ 0 ] . Length < 10 ) { for ( int i = 0 ; i < l . Count ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( Int32 . Parse ( l [ i ] + temp2 [ j ] ) > N ) { flag = 1 ; break ; } total . Add ( l [ i ] + temp2 [ j ] ) ; temp3 . Add ( l [ i ] + temp2 [ j ] ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = temp3 ; temp3 . Clear ( ) ; } int lenOfTotal = total . Count ; int ans = numOfPairs ( total , lenOfTotal ) ; Console . WriteLine ( ans ) ; } public static void Main ( ) { int N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ; } }
Minimize cost of placing tiles of dimensions 2 * 1 over a Matrix | C # program for the above approach ; Function to find the minimum cost in placing N tiles in a grid M [ ] [ ] ; Stores the minimum profit after placing i tiles ; Traverse the grid [ ] [ ] ; Update the orig_cost ; Traverse over the range [ 2 , N ] ; Place tiles horizentally or vertically ; Print the answer ; Driver Code
using System ; class GFG { static void tile_placing ( int [ , ] grid , int N ) { int [ ] dp = new int [ N + 5 ] ; for ( int i = 0 ; i < N + 5 ; i ++ ) { dp [ i ] = 0 ; } int orig_cost = 0 ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { orig_cost += grid [ i , j ] ; } } dp [ 0 ] = 0 ; dp [ 1 ] = Math . Abs ( grid [ 0 , 0 ] - grid [ 1 , 0 ] ) ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = Math . Max ( dp [ i - 1 ] + Math . Abs ( grid [ 0 , i - 1 ] - grid [ 1 , i - 1 ] ) , dp [ i - 2 ] + Math . Abs ( grid [ 0 , i - 2 ] - grid [ 0 , i - 1 ] ) + Math . Abs ( grid [ 1 , i - 2 ] - grid [ 1 , i - 1 ] ) ) ; } Console . Write ( orig_cost - dp [ N ] ) ; } public static void Main ( ) { int [ , ] M = { { 7 , 5 , 1 , 3 } , { 8 , 6 , 0 , 2 } } ; int N = 4 ; tile_placing ( M , N ) ; } }
Print indices of pair of array elements required to be removed to split array into 3 equal sum subarrays | C # program for the above approach ; Function to check if array can be split into three equal sum subarrays by removing two elements ; Stores sum of all three subarrays ; Sum of left subarray ; Sum of middle subarray ; Sum of right subarray ; Check if sum of subarrays are equal ; Print the possible pair ; If no pair exists , print - 1 ; Driver Code ; Given array ; Size of the array
using System ; class GFG { static void findSplit ( int [ ] arr , int N ) { for ( int l = 1 ; l <= N - 4 ; l ++ ) { for ( int r = l + 2 ; r <= N - 2 ; r ++ ) { int lsum = 0 , rsum = 0 , msum = 0 ; for ( int i = 0 ; i <= l - 1 ; i ++ ) { lsum += arr [ i ] ; } for ( int i = l + 1 ; i <= r - 1 ; i ++ ) { msum += arr [ i ] ; } for ( int i = r + 1 ; i < N ; i ++ ) { rsum += arr [ i ] ; } if ( lsum == rsum && rsum == msum ) { Console . WriteLine ( l + " ▁ " + r ) ; return ; } } } Console . Write ( - 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 5 , 12 , 7 , 19 , 4 , 3 } ; int N = arr . Length ; findSplit ( arr , N ) ; } }
Count the number of times a Bulb switches its state | C # program for the above approach ; Function to find the number of times a bulb switches its state ; Count of 1 s ; Traverse the array ; Update count of 1 s ; Update the status of bulb ; Traverse the array Q [ ] ; Stores previous state of the bulb ; Toggle the switch and update count of 1 s ; If the bulb switches state ; Return count ; Driver Code ; Input ; Queries ; Function call to find number of times the bulb toggles
using System ; class GFG { static int solve ( int [ ] A , int n , int [ ] Q , int q ) { int one = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] == 1 ) one ++ ; int glows = 0 , count = 0 ; if ( one >= ( int ) Math . Ceiling ( ( double ) n / 2 ) ) glows = 1 ; for ( int i = 0 ; i < q ; i ++ ) { int prev = glows ; if ( A [ Q [ i ] - 1 ] == 1 ) one -- ; if ( A [ Q [ i ] - 1 ] == 0 ) one ++ ; A [ Q [ i ] - 1 ] ^= 1 ; if ( one >= ( int ) Math . Ceiling ( ( double ) n / 2.0 ) ) { glows = 1 ; } else { glows = 0 ; } if ( prev != glows ) count ++ ; } return count ; } static public void Main ( ) { int n = 3 ; int [ ] arr = { 1 , 1 , 0 } ; int q = 3 ; int [ ] Q = { 3 , 2 , 1 } ; Console . WriteLine ( solve ( arr , n , Q , q ) ) ; } }
Count array elements having sum of digits equal to K | C # program for the above approach ; Function to calculate the sum of digits of the number N ; Stores the sum of digits ; Return the sum ; Function to count array elements ; Store the count of array elements having sum of digits K ; Traverse the array ; If sum of digits is equal to K ; Increment the count ; Print the count ; Driver code ; Given array ; Given value of K ; Size of the array ; Function call to count array elements having sum of digits equal to K
using System ; using System . Collections . Generic ; class GFG { static int sumOfDigits ( int N ) { int sum = 0 ; while ( N != 0 ) { sum += N % 10 ; N /= 10 ; } return sum ; } static void elementsHavingDigitSumK ( int [ ] arr , int N , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( sumOfDigits ( arr [ i ] ) == K ) { count ++ ; } } Console . WriteLine ( count ) ; } static void Main ( ) { int [ ] arr = { 23 , 54 , 87 , 29 , 92 , 62 } ; int K = 11 ; int N = arr . Length ; elementsHavingDigitSumK ( arr , N , K ) ; } }
Postfix to Infix | C # program to find infix for a given postfix . ; Get Infix for a given postfix expression ; Push operands ; We assume that input is a valid postfix and expect an operator . ; There must be a single element in stack now which is the required infix . ; Driver code
using System ; using System . Collections ; class GFG { static Boolean isOperand ( char x ) { return ( x >= ' a ' && x <= ' z ' ) || ( x >= ' A ' && x <= ' Z ' ) ; } static String getInfix ( String exp ) { Stack s = new Stack ( ) ; for ( int i = 0 ; i < exp . Length ; i ++ ) { if ( isOperand ( exp [ i ] ) ) { s . Push ( exp [ i ] + " " ) ; } else { String op1 = ( String ) s . Peek ( ) ; s . Pop ( ) ; String op2 = ( String ) s . Peek ( ) ; s . Pop ( ) ; s . Push ( " ( " + op2 + exp [ i ] + op1 + " ) " ) ; } } return ( String ) s . Peek ( ) ; } public static void Main ( String [ ] args ) { String exp = " ab * c + " ; Console . WriteLine ( getInfix ( exp ) ) ; } }
Change a Binary Tree so that every node stores sum of all nodes in left subtree | C # program to store sum of nodes in left subtree in every node ; A tree node ; Function to modify a Binary Tree so that every node stores sum of values in its left child including its own value ; Base cases ; Update left and right subtrees ; Add leftsum to current node ; Return sum of values under root ; Utility function to do inorder traversal ; Utility function to create a new node ; Driver code ; Let us construct below tree 1 / \ 2 3 / \ \ 4 5 6
using System ; class GfG { class node { public int data ; public node left , right ; } static int updatetree ( node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; int leftsum = updatetree ( root . left ) ; int rightsum = updatetree ( root . right ) ; root . data += leftsum ; return root . data + rightsum ; } static void inorder ( node node ) { if ( node == null ) return ; inorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inorder ( node . right ) ; } static node newNode ( int data ) { node node = new node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void Main ( ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; updatetree ( root ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ of ▁ the ▁ modified ▁ tree ▁ is " ) ; inorder ( root ) ; } }
The Stock Span Problem | C # implementation for brute force method to calculate stock span values ; method to calculate stock span values ; Span value of first day is always 1 ; Calculate span value of remaining days by linearly checking previous days ; Initialize span value ; Traverse left while the next element on left is smaller than price [ i ] ; A utility function to print elements of array ; Driver function ; Fill the span values in array S [ ] ; print the calculated span values
using System ; class GFG { static void calculateSpan ( int [ ] price , int n , int [ ] S ) { S [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { S [ i ] = 1 ; for ( int j = i - 1 ; ( j >= 0 ) && ( price [ i ] >= price [ j ] ) ; j -- ) S [ i ] ++ ; } } static void printArray ( int [ ] arr ) { string result = string . Join ( " ▁ " , arr ) ; Console . WriteLine ( result ) ; } public static void Main ( ) { int [ ] price = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . Length ; int [ ] S = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S ) ; } }
The Stock Span Problem | C # linear time solution for stock span problem ; a linear time solution for stock span problem A stack based efficient method to calculate stock span values ; Create a stack and Push index of first element to it ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; Pop elements from stack while stack is not empty and top of stack is smaller than price [ i ] ; If stack becomes empty , then price [ i ] is greater than all elements on left of it , i . e . , price [ 0 ] , price [ 1 ] , . . price [ i - 1 ] . Else price [ i ] is greater than elements after top of stack ; Push this element to stack ; A utility function to print elements of array ; Driver method ; Fill the span values in array S [ ] ; print the calculated span values
using System ; using System . Collections ; class GFG { static void calculateSpan ( int [ ] price , int n , int [ ] S ) { Stack st = new Stack ( ) ; st . Push ( 0 ) ; S [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { while ( st . Count > 0 && price [ ( int ) st . Peek ( ) ] <= price [ i ] ) st . Pop ( ) ; S [ i ] = ( st . Count == 0 ) ? ( i + 1 ) : ( i - ( int ) st . Peek ( ) ) ; st . Push ( i ) ; } } static void printArray ( int [ ] arr ) { for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] price = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . Length ; int [ ] S = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S ) ; } }
The Stock Span Problem | C # program for a linear time solution for stock span problem without using stack ; An efficient method to calculate stock span values implementing the same idea without using stack ; Span value of first element is always 1 ; Calculate span values for rest of the elements ; A utility function to print elements of array ; Driver code ; Fill the span values in array S [ ] ; print the calculated span values
using System ; public class GFG { static void calculateSpan ( int [ ] A , int n , int [ ] ans ) { ans [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int counter = 1 ; while ( ( i - counter ) >= 0 && A [ i ] >= A [ i - counter ] ) { counter += ans [ i - counter ] ; } ans [ i ] = counter ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] price = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . Length ; int [ ] S = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; } }
Next Greater Element | Simple C # program to print next greater elements in a given array ; prints element and NGE pair for all elements of arr [ ] of size n ; driver code
using System ; class GFG { static void printNGE ( int [ ] arr , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { next = arr [ j ] ; break ; } } Console . WriteLine ( arr [ i ] + " ▁ - - ▁ " + next ) ; } } public static void Main ( ) { int [ ] arr = { 11 , 13 , 21 , 3 } ; int n = arr . Length ; printNGE ( arr , n ) ; } }
Convert a Binary Tree into its Mirror Tree | C # program to convert binary tree into its mirror ; Class containing left and right child of current node and key value ; Change a tree so that the roles of the left and right pointers are swapped at every node . So the tree ... 4 / \ 2 5 / \ 1 3 is changed to ... 4 / \ 5 2 / \ 3 1 ; do the subtrees ; swap the left and right pointers ; Helper function to test mirror ( ) . Given a binary search tree , print out its data elements in increasing sorted order . ; testing for example nodes ; print inorder traversal of the input tree ; convert tree to its mirror ; print inorder traversal of the minor tree
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { public Node root ; public virtual void mirror ( ) { root = mirror ( root ) ; } public virtual Node mirror ( Node node ) { if ( node == null ) { return node ; } Node left = mirror ( node . left ) ; Node right = mirror ( node . right ) ; node . left = right ; node . right = left ; return node ; } public virtual void inOrder ( ) { inOrder ( root ) ; } public virtual void inOrder ( Node node ) { if ( node == null ) { return ; } inOrder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } public static void Main ( string [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " of ▁ input ▁ tree ▁ is ▁ : " ) ; tree . inOrder ( ) ; Console . WriteLine ( " " ) ; tree . mirror ( ) ; Console . WriteLine ( " Inorder ▁ traversal ▁ " + " of ▁ binary ▁ tree ▁ is ▁ : ▁ " ) ; tree . inOrder ( ) ; } }
Number of NGEs to the right | C # program to print number of NGEs to the right ; array to store the next greater element index ; Use stack ; Push the 0 th index to the stack ; traverse in the loop from 1 - nth index ; iterate till loop is empty ; get the topmost index in the stack ; if the current element is greater then the top index - th element , then this will be the next greatest index of the top index - th element ; initialize the cur index position 's  next greatest as index ; Pop the cur index as its greater element has been found ; if not greater then break ; Push the i index so that its next greatest can be found ; iterate for all other index left inside stack ; mark it as - 1 as no element in greater then it in right ; function to count the number of next greater numbers to the right ; initializes the next array as 0 ; calls the function to pre - calculate the next greatest element indexes ; if the i - th element has no next greater element to right ; Count of next greater numbers to right . ; answers all queries in O ( 1 ) ; returns the number of next greater elements to the right . ; Driver code ; calls the function to count the number of greater elements to the right for every element . ; query 1 answered ; query 2 answered ; query 3 answered
using System ; using System . Collections ; class GFG { static void fillNext ( int [ ] next , int [ ] a , int n ) { Stack s = new Stack ( ) ; s . Push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( s . Count > 0 ) { int cur = ( int ) s . Peek ( ) ; if ( a [ cur ] < a [ i ] ) { next [ cur ] = i ; s . Pop ( ) ; } else break ; } s . Push ( i ) ; } while ( s . Count > 0 ) { int cur = ( int ) s . Peek ( ) ; next [ cur ] = - 1 ; s . Pop ( ) ; } } static void count ( int [ ] a , int [ ] dp , int n ) { int [ ] next = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) next [ i ] = 0 ; fillNext ( next , a , n ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( next [ i ] == - 1 ) dp [ i ] = 0 ; else dp [ i ] = 1 + dp [ next [ i ] ] ; } } static int answerQuery ( int [ ] dp , int index ) { return dp [ index ] ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; int n = a . Length ; int [ ] dp = new int [ n ] ; count ( a , dp , n ) ; Console . WriteLine ( answerQuery ( dp , 3 ) ) ; Console . WriteLine ( answerQuery ( dp , 6 ) ) ; Console . WriteLine ( answerQuery ( dp , 1 ) ) ; } }
Number of NGEs to the right |  ; use of stl stack in Java ; push the ( n - 1 ) th index to the stack ; traverse in reverse order ; if no element is greater than arr [ i ] the number of NGEs to right is 0 ; number of NGEs to right of arr [ i ] is one greater than the number of NGEs to right of higher number to its right ; reverse again because values are in reverse order ; returns the vector of number of next greater elements to the right of each index . ; Driver code ; query 1 answered ; query 2 answered ; query 3 answered
using System ; using System . Collections . Generic ; public class GFG { static List < int > no_NGN ( int [ ] arr , int n ) { List < int > nxt = new List < int > ( ) ; Stack < int > s = new Stack < int > ( ) ; nxt . Add ( 0 ) ; s . Push ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( s . Count != 0 && arr [ i ] >= arr [ s . Peek ( ) ] ) s . Pop ( ) ; if ( s . Count == 0 ) nxt . Add ( 0 ) ; else nxt . Add ( nxt [ n - s . Peek ( ) - 1 ] + 1 ) ; s . Push ( i ) ; } nxt . Reverse ( ) ; return nxt ; } public static void Main ( String [ ] args ) { int n = 8 ; int [ ] arr = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; List < int > nxt = no_NGN ( arr , n ) ; Console . Write ( nxt [ 3 ] + " STRNEWLINE " ) ; Console . Write ( nxt [ 6 ] + " STRNEWLINE " ) ; Console . Write ( nxt [ 1 ] + " STRNEWLINE " ) ; } }