codeparrot/xlcost-text-to-code huggingface dataset:
Maximum product of indexes of next greater on left and right | C # program to find the max LRproduct [ i ] among all i ; function to find just next greater element in left side ; checking if current element is greater than top ; on index of top store the current element index which is just greater than top element ; else push the current element in stack ; function to find just next greater element in right side ; checking if current element is greater than top ; on index of top store the current element index which is just greater than top element stored index should be start with 1 ; else push the current element in stack ; Function to find maximum LR product ; for each element storing the index of just greater element in left side ; for each element storing the index of just greater element in right side ; finding the max index product ; Drivers code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000 ; static int [ ] nextGreaterInLeft ( int [ ] a , int n ) { int [ ] left_index = new int [ MAX ] ; Stack < int > s = new Stack < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . Count != 0 && a [ i ] > a [ s . Peek ( ) - 1 ] ) { int r = s . Peek ( ) ; s . Pop ( ) ; left_index [ r - 1 ] = i + 1 ; } s . Push ( i + 1 ) ; } return left_index ; } static int [ ] nextGreaterInRight ( int [ ] a , int n ) { int [ ] right_index = new int [ MAX ] ; Stack < int > s = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { while ( s . Count != 0 && a [ i ] > a [ s . Peek ( ) - 1 ] ) { int r = s . Peek ( ) ; s . Pop ( ) ; right_index [ r - 1 ] = i + 1 ; } s . Push ( i + 1 ) ; } return right_index ; } static int LRProduct ( int [ ] arr , int n ) { int [ ] left = nextGreaterInLeft ( arr , n ) ; int [ ] right = nextGreaterInRight ( arr , n ) ; int ans = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = Math . Max ( ans , left [ i ] * right [ i ] ) ; } return ans ; } static void Main ( ) { int [ ] arr = new int [ ] { 5 , 4 , 3 , 4 , 5 } ; int n = arr . Length ; Console . Write ( LRProduct ( arr , n ) ) ; } }
The Celebrity Problem | C # program to find celebrity ; Max # of persons in the party ; Person with 2 is celebrity ; Returns - 1 if celebrity is not present . If present , returns id ( value from 0 to n - 1 ) . ; the graph needs not be constructed as the edges can be found by using knows function degree array ; ; query for all edges ; set the degrees ; find a person with indegree n - 1 and out degree 0 ; Driver code
using System ; public class GFG { static readonly int N = 8 ; static int [ , ] MATRIX = { { 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 } } ; static int knows ( int a , int b ) { return MATRIX [ a , b ] ; } static int findCelebrity ( int n ) { int [ ] indegree = new int [ n ] ; int [ ] outdegree = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int x = knows ( i , j ) ; outdegree [ i ] += x ; indegree [ j ] += x ; } } for ( int i = 0 ; i < n ; i ++ ) if ( indegree [ i ] == n - 1 && outdegree [ i ] == 0 ) return i ; return - 1 ; } public static void Main ( String [ ] args ) { int n = 4 ; int id = findCelebrity ( n ) ; if ( id == - 1 ) Console . Write ( " No ▁ celebrity " ) ; else Console . Write ( " Celebrity ▁ ID ▁ " + id ) ; } }
Convert a Binary Tree into its Mirror Tree | C # Iterative Java program to convert a Binary Tree to its mirror ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; 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 BFS . While doing BFS , keep swapping left and right children ; pop top node from queue ; swap left child with right child ; push left and right children ; Helper function to print Inorder traversal . ; Driver code ; Print inorder traversal of the input tree ; Convert tree to its mirror ; Print inorder traversal of the mirror tree
using System . Collections . Generic ; using System ; class GFG { public class Node { public int data ; public Node left ; public Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void mirror ( Node root ) { if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count > 0 ) { Node curr = q . Peek ( ) ; q . Dequeue ( ) ; Node temp = curr . left ; curr . left = curr . right ; curr . right = temp ; ; if ( curr . left != null ) q . Enqueue ( curr . left ) ; if ( curr . right != null ) q . Enqueue ( curr . right ) ; } } static void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . Write ( " STRNEWLINE ▁ Inorder ▁ traversal ▁ of ▁ the " + " ▁ coned ▁ tree ▁ is ▁ STRNEWLINE " ) ; inOrder ( root ) ; mirror ( root ) ; Console . Write ( " STRNEWLINE ▁ Inorder ▁ traversal ▁ of ▁ the ▁ " + " mirror ▁ tree ▁ is ▁ STRNEWLINE " ) ; inOrder ( root ) ; } }
Delete middle element of a stack | C # code to delete middle of a stack without using additional data structure . ; Deletes middle of stack of size n . Curr is current item number ; If stack is empty or all items are traversed ; Remove current item ; Remove other items ; Put all items back except middle ; Driver Code ; push elements into the stack ; Printing stack after deletion of middle .
using System ; using System . Collections . Generic ; class GFG { static void deleteMid ( Stack < char > st , int n , int curr = 0 ) { if ( st . Count == 0 curr == n ) return ; char x = st . Peek ( ) ; st . Pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . Push ( x ) ; } public static void Main ( ) { Stack < char > st = new Stack < char > ( ) ; st . Push ( '1' ) ; st . Push ( '2' ) ; st . Push ( '3' ) ; st . Push ( '4' ) ; st . Push ( '5' ) ; st . Push ( '6' ) ; st . Push ( '7' ) ; deleteMid ( st , st . Count ) ; while ( st . Count != 0 ) { char p = st . Peek ( ) ; st . Pop ( ) ; Console . Write ( p + " ▁ " ) ; } } }
Sorting array using Stacks | C # program to sort an array using stack ; This function return the sorted stack ; pop out the first element ; while temporary stack is not empty and top of stack is smaller than temp ; pop from temporary stack and push it to the input stack ; push temp in tempory of stack ; Push array elements to stack ; Sort the temporary stack ; Put stack elements in arrp [ ] ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static Stack < int > sortStack ( Stack < int > input ) { Stack < int > tmpStack = new Stack < int > ( ) ; while ( input . Count != 0 ) { int tmp = input . Peek ( ) ; input . Pop ( ) ; while ( tmpStack . Count != 0 && tmpStack . Peek ( ) < tmp ) { input . Push ( tmpStack . Peek ( ) ) ; tmpStack . Pop ( ) ; } tmpStack . Push ( tmp ) ; } return tmpStack ; } static void sortArrayUsingStacks ( int [ ] arr , int n ) { Stack < int > input = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) input . Push ( arr [ i ] ) ; Stack < int > tmpStack = sortStack ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack . Peek ( ) ; tmpStack . Pop ( ) ; } } static void Main ( ) { int [ ] arr = new int [ ] { 10 , 5 , 15 , 45 } ; int n = arr . Length ; sortArrayUsingStacks ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Reverse individual words | C # program to reverse individual words in a given string using STL list ; reverses individual words of a string ; Traverse given string and push all characters to stack until we see a space . ; When we see a space , we print contents of stack . ; Since there may not be space after last word . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void reverseWords ( string str ) { Stack < char > st = new Stack < char > ( ) ; for ( int i = 0 ; i < str . Length ; ++ i ) { if ( str [ i ] != ' ▁ ' ) { st . Push ( str [ i ] ) ; } else { while ( st . Count > 0 ) { Console . Write ( st . Pop ( ) ) ; } Console . Write ( " ▁ " ) ; } } while ( st . Count > 0 ) { Console . Write ( st . Pop ( ) ) ; } } public static void Main ( string [ ] args ) { string str = " Geeks ▁ for ▁ Geeks " ; reverseWords ( str ) ; } }
Count subarrays where second highest lie before highest | C # program to count number of distinct instance where second highest number lie before highest number in all subarrays . ; Pair class ; Finding the next greater element of the array . ; Finding the previous greater element of the array . ; Wrapper Function ; Finding previous largest element ; Finding next largest element ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MAXN = 100005 ; public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeNext ( int [ ] arr , int n , int [ ] nextBig ) { Stack < pair > s = new Stack < pair > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( s . Count != 0 && s . Peek ( ) . first < arr [ i ] ) s . Pop ( ) ; if ( s . Count != 0 ) nextBig [ i ] = s . Peek ( ) . second ; s . Push ( new pair ( arr [ i ] , i ) ) ; } } static void makePrev ( int [ ] arr , int n , int [ ] prevBig ) { Stack < pair > s = new Stack < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = - 1 ; while ( s . Count != 0 && s . Peek ( ) . first < arr [ i ] ) s . Pop ( ) ; if ( s . Count != 0 ) prevBig [ i ] = s . Peek ( ) . second ; s . Push ( new pair ( arr [ i ] , i ) ) ; } } static int wrapper ( int [ ] arr , int n ) { int [ ] nextBig = new int [ MAXN ] ; int [ ] prevBig = new int [ MAXN ] ; int [ ] maxi = new int [ MAXN ] ; int ans = 0 ; makePrev ( arr , n , prevBig ) ; makeNext ( arr , n , nextBig ) ; for ( int i = 0 ; i < n ; i ++ ) if ( nextBig [ i ] != i ) maxi [ nextBig [ i ] - i ] = Math . Max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) ans += maxi [ i ] ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 4 } ; int n = arr . Length ; Console . WriteLine ( wrapper ( arr , n ) ) ; } }
Largest Rectangular Area in a Histogram | Set 2 | C # program to find maximum rectangular area in linear time ; The main function to find the maximum rectangular area under given histogram with n bars ; Create an empty stack . The stack holds indexes of hist [ ] array The bars stored in stack are always in increasing order of their heights . ; Initialize max area ; To store top of stack ; To store area with top ; bar as the smallest bar Run through all bars of given histogram ; If this bar is higher than the bar on top stack , push it to stack ; If this bar is lower than top of stack , then calculate area of rectangle with stack top as the smallest ( or minimum height ) bar . ' i ' is ' right ▁ index ' for the top and element before top in stack is ' left ▁ index ' ; store the top index ; pop the top ; Calculate the area with hist [ tp ] stack as smallest bar ; update max area , if needed ; Now pop the remaining bars from stack and calculate area with every popped bar as the smallest bar ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int getMaxArea ( int [ ] hist , int n ) { Stack < int > s = new Stack < int > ( ) ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . Count == 0 || hist [ s . Peek ( ) ] <= hist [ i ] ) { s . Push ( i ++ ) ; } else { tp = s . Peek ( ) ; s . Pop ( ) ; area_with_top = hist [ tp ] * ( s . Count == 0 ? i : i - s . Peek ( ) - 1 ) ; if ( max_area < area_with_top ) { max_area = area_with_top ; } } } while ( s . Count > 0 ) { tp = s . Peek ( ) ; s . Pop ( ) ; area_with_top = hist [ tp ] * ( s . Count == 0 ? i : i - s . Peek ( ) - 1 ) ; if ( max_area < area_with_top ) { max_area = area_with_top ; } } return max_area ; } public static void Main ( string [ ] args ) { int [ ] hist = new int [ ] { 6 , 2 , 5 , 4 , 5 , 1 , 6 } ; Console . WriteLine ( " Maximum ▁ area ▁ is ▁ " + getMaxArea ( hist , hist . Length ) ) ; } }
Program for Tower of Hanoi | C # recursive program to solve tower of hanoi puzzle ; Driver method ; Number of disks ; A , B and C are names of rods
using System ; class GFG { static void towerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n == 1 ) { Console . WriteLine ( " Move ▁ disk ▁ 1 ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; Console . WriteLine ( " Move ▁ disk ▁ " + n + " ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } public static void Main ( String [ ] args ) { int n = 4 ; towerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) ; } }
Find maximum of minimum for every window size in a given array | C # program using Naive approach to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver Code
using System ; class GFG { static int [ ] arr = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = int . MinValue ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } if ( min > maxOfMin ) maxOfMin = min ; } Console . Write ( maxOfMin + " ▁ " ) ; } } public static void Main ( ) { printMaxOfMin ( arr . Length ) ; } }
Find maximum of minimum for every window size in a given array | An efficient C # program to find maximum of all minimums of windows of different size ; Used to find previous and next smaller ; Arrays to store previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element / https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all lengths computed using left [ ] and right [ ] ; length of the interval ; arr [ i ] is a possible answer for this length ' len ' interval , check x if arr [ i ] is more than max for ' len ' ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int [ ] arr = new int [ ] { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; public static void printMaxOfMin ( int n ) { Stack < int > s = new Stack < int > ( ) ; int [ ] left = new int [ n + 1 ] ; int [ ] right = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { while ( s . Count > 0 && arr [ s . Peek ( ) ] >= arr [ i ] ) { s . Pop ( ) ; } if ( s . Count > 0 ) { left [ i ] = s . Peek ( ) ; } s . Push ( i ) ; } while ( s . Count > 0 ) { s . Pop ( ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . Count > 0 && arr [ s . Peek ( ) ] >= arr [ i ] ) { s . Pop ( ) ; } if ( s . Count > 0 ) { right [ i ] = s . Peek ( ) ; } s . Push ( i ) ; } int [ ] ans = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { ans [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . Max ( ans [ len ] , arr [ i ] ) ; } for ( int i = n - 1 ; i >= 1 ; i -- ) { ans [ i ] = Math . Max ( ans [ i ] , ans [ i + 1 ] ) ; } for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { printMaxOfMin ( arr . Length ) ; } }
Length of the longest valid substring | C # program to find length of the longest valid substring ; method to get length of the longest valid ; Create a stack and push - 1 as initial index to it . ; Initialize result ; Traverse all characters of given string ; If opening bracket , push index of it ; If closing bracket , i . e . , str [ i ] = ' ) ' ; Pop the previous opening bracket 's index ; Check if this length formed with base of current valid substring is more than max so far ; If stack is empty . push current index as base for next valid substring ( if any ) ; Driver Code ; Function call ; Function call
using System ; using System . Collections . Generic ; class GFG { public static int findMaxLen ( string str ) { int n = str . Length ; Stack < int > stk = new Stack < int > ( ) ; stk . Push ( - 1 ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) { stk . Push ( i ) ; } else { if ( stk . Count > 0 ) stk . Pop ( ) ; if ( stk . Count > 0 ) { result = Math . Max ( result , i - stk . Peek ( ) ) ; } else { stk . Push ( i ) ; } } } return result ; } public static void Main ( string [ ] args ) { string str = " ( ( ( ) ( ) " ; Console . WriteLine ( findMaxLen ( str ) ) ; str = " ( ) ( ( ) ) ) ) ) " ; Console . WriteLine ( findMaxLen ( str ) ) ; } }
Convert a tree to forest of even nodes | C # program to find maximum number to be removed to convert a tree into forest containing trees of even number of nodes ; Return the number of nodes of subtree having node as a root . ; Mark node as visited . ; Traverse the adjacency list to find non - visited node . ; Finding number of nodes of the subtree of a subtree . ; If nodes are even , increment number of edges to removed . Else leave the node as child of subtree . ; Return the maximum number of edge to remove to make forest . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int N = 12 , ans ; static List < List < int > > tree = new List < List < int > > ( ) ; static int dfs ( int [ ] visit , int node ) { int num = 0 , temp = 0 ; visit [ node ] = 1 ; for ( int i = 0 ; i < tree [ node ] . Count ; i ++ ) { if ( visit [ tree [ node ] [ i ] ] == 0 ) { temp = dfs ( visit , tree [ node ] [ i ] ) ; if ( temp % 2 != 0 ) num += temp ; else ans ++ ; } } return num + 1 ; } static int minEdge ( int n ) { int [ ] visit = new int [ n + 2 ] ; ans = 0 ; dfs ( visit , 1 ) ; return ans ; } public static void Main ( String [ ] args ) { int n = 10 ; for ( int i = 0 ; i < n + 2 ; i ++ ) tree . Add ( new List < int > ( ) ) ; tree [ 1 ] . Add ( 3 ) ; tree [ 3 ] . Add ( 1 ) ; tree [ 1 ] . Add ( 6 ) ; tree [ 6 ] . Add ( 1 ) ; tree [ 1 ] . Add ( 2 ) ; tree [ 2 ] . Add ( 1 ) ; tree [ 3 ] . Add ( 4 ) ; tree [ 4 ] . Add ( 3 ) ; tree [ 6 ] . Add ( 8 ) ; tree [ 8 ] . Add ( 6 ) ; tree [ 2 ] . Add ( 7 ) ; tree [ 7 ] . Add ( 2 ) ; tree [ 2 ] . Add ( 5 ) ; tree [ 5 ] . Add ( 2 ) ; tree [ 4 ] . Add ( 9 ) ; tree [ 9 ] . Add ( 4 ) ; tree [ 4 ] . Add ( 10 ) ; tree [ 10 ] . Add ( 4 ) ; Console . WriteLine ( minEdge ( n ) ) ; } }
Length of the longest valid substring | C # program to find length of the longest valid subString ; Initialize curMax to zero ; Iterate over the String starting from second index ; Driver code ; Function call ; Function call
using System ; public class GFG { static int findMaxLen ( String s ) { if ( s . Length <= 1 ) return 0 ; int curMax = 0 ; int [ ] longest = new int [ s . Length ] ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' ) ' && i - longest [ i - 1 ] - 1 >= 0 && s [ i - longest [ i - 1 ] - 1 ] == ' ( ' ) { longest [ i ] = longest [ i - 1 ] + 2 + ( ( i - longest [ i - 1 ] - 2 >= 0 ) ? longest [ i - longest [ i - 1 ] - 2 ] : 0 ) ; curMax = Math . Max ( longest [ i ] , curMax ) ; } } return curMax ; } public static void Main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; Console . Write ( findMaxLen ( str ) + " STRNEWLINE " ) ; str = " ( ) ( ( ) ) ) ) ) " ; Console . Write ( findMaxLen ( str ) + " STRNEWLINE " ) ; } }
Length of the longest valid substring | C # program to implement the above approach ; Function to return the length of the longest valid substring ; Variables for left and right counter maxlength to store the maximum length found so far ; Iterating the string from left to right ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Iterating the string from right to left ; If " ( " is encountered , then left counter is incremented else right counter is incremented ; Whenever left is equal to right , it signifies that the subsequence is valid and ; Reseting the counters when the subsequence becomes invalid ; Driver code ; Function call
using System ; public class GFG { public static int solve ( String s , int n ) { int left = 0 , right = 0 ; int maxlength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . Max ( maxlength , 2 * right ) ; else if ( right > left ) left = right = 0 ; } left = right = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . Max ( maxlength , 2 * left ) ; else if ( left > right ) left = right = 0 ; } return maxlength ; } public static void Main ( String [ ] args ) { Console . Write ( solve ( " ( ( ( ) ( ) ( ) ( ) ( ( ( ( ) ) " , 16 ) ) ; } }
Expression contains redundant bracket or not | C # Program to check whether valid expression is redundant or not ; Function to check redundant brackets in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; If immediate pop have open parenthesis ' ( ' duplicate brackets found ; Check for operators in expression ; Fetch top element of stack ; If operators not found ; push open parenthesis ' ( ' , ; operators and operands to stack ; Function to check redundant brackets ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool checkRedundancy ( String s ) { Stack < char > st = new Stack < char > ( ) ; char [ ] str = s . ToCharArray ( ) ; foreach ( char ch in str ) { if ( ch == ' ) ' ) { char top = st . Peek ( ) ; st . Pop ( ) ; bool flag = true ; while ( top != ' ( ' ) { if ( top == ' + ' top == ' - ' top == ' * ' top == ' / ' ) { flag = false ; } top = st . Peek ( ) ; st . Pop ( ) ; } if ( flag == true ) { return true ; } } else { st . Push ( ch ) ; } } return false ; } static void findRedundant ( String str ) { bool ans = checkRedundancy ( str ) ; if ( ans == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { String str = " ( ( a + b ) ) " ; findRedundant ( str ) ; str = " ( a + ( b ) / c ) " ; findRedundant ( str ) ; str = " ( a + b * ( c - d ) ) " ; findRedundant ( str ) ; } }