codeparrot/xlcost-text-to-code huggingface dataset:
Find the greater number closest to N having at most one non | C # program to implement the above approach ; Function to calculate X ^ n in log ( n ) ; Stores the value of X ^ n ; If N is odd ; Function to find the closest number > N having at most 1 non - zero digit ; Stores the count of digits in N ; Stores the power of 10 ^ ( n - 1 ) ; Stores the last ( n - 1 ) digits ; Store the answer ; Driver Code ; Function call
using System ; class GFG { static int power ( int X , int n ) { int res = 1 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) res = res * X ; X = X * X ; n = n >> 1 ; } return res ; } static int closestgtNum ( int N ) { int n = ( int ) Math . Log10 ( N ) + 1 ; int P = power ( 10 , n - 1 ) ; int Y = N % P ; int res = N + ( P - Y ) ; return res ; } public static void Main ( ) { int N = 120 ; Console . Write ( closestgtNum ( N ) ) ; } }
Rearrange an array to make similar indexed elements different from that of another array | C # program for the above approach ; Function to find the arrangement of array [ ] B such that element at each index of [ ] A and [ ] B are not equal ; Length of array ; Print not possible , if arrays only have single equal element ; Reverse array B ; Traverse over arrays to check if there is any index where A [ i ] and B [ i ] are equal ; Swap B [ i ] with B [ i - 1 ] ; Break the loop ; Print required arrangement of array B ; Driver Code ; Given arrays [ ] A and [ ] B ; Function Call
using System ; class GFG { static void RearrangeB ( int [ ] A , int [ ] B ) { int n = A . Length ; if ( n == 1 && A [ 0 ] == B [ 0 ] ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 0 ; i < n / 2 ; i ++ ) { int t = B [ i ] ; B [ i ] = B [ n - i - 1 ] ; B [ n - i - 1 ] = t ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] == A [ i ] ) { int t = B [ i + 1 ] ; B [ i + 1 ] = B [ i ] ; B [ i ] = t ; break ; } } foreach ( int k in B ) Console . Write ( k + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 4 , 5 , 8 } ; int [ ] B = { 2 , 4 , 5 , 8 } ; RearrangeB ( A , B ) ; } }
Swap the elements between any two given quadrants of a Matrix | C # program for the above approach ; Function to iterate over the X quadrant and swap its element with Y quadrant ; Iterate over X quadrant ; Swap operations ; Function to swap the elements of the two given quadrants ; For Swapping 1 st and 2 nd Quadrant ; For Swapping 1 st and 3 rd Quadrant ; For Swapping 1 st and 4 th Quadrant ; For Swapping 2 nd and 3 rd Quadrant ; For Swapping 2 nd and 4 th Quadrant ; For Swapping 3 rd and 4 th Quadrant ; Print the resultant matrix ; Function to print the matrix ; Iterate over the rows ; Iterate over the cols ; Driver Code ; Given matrix ; Given quadrants ; Function Call
using System ; class GFG { static void swap ( int N , int M , int [ , ] mat , int startx_X , int starty_X , int startx_Y , int starty_Y ) { int row = 0 ; int col = 0 ; for ( int i = startx_X ; ; i ++ ) { col = 0 ; for ( int j = startx_X ; ; j ++ ) { int temp = mat [ i , j ] ; mat [ i , j ] = mat [ startx_Y + row , starty_Y + col ] ; mat [ startx_Y + row , starty_Y + col ] = temp ; col ++ ; if ( col >= M / 2 ) break ; } row ++ ; if ( row >= N / 2 ) break ; } } static void swapQuadOfMatrix ( int N , int M , int [ , ] mat , int X , int Y ) { if ( X == 1 && Y == 2 ) { swap ( N , M , mat , 0 , 0 , 0 , M / 2 ) ; } else if ( X == 1 && Y == 3 ) { swap ( N , M , mat , 0 , 0 , N / 2 , 0 ) ; } else if ( X == 1 && Y == 4 ) { swap ( N , M , mat , 0 , 0 , N / 2 , M / 2 ) ; } else if ( X == 2 && Y == 3 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , 0 ) ; } else if ( X == 2 && Y == 4 ) { swap ( N , M , mat , 0 , M / 2 , N / 2 , M / 2 ) ; } else if ( X == 3 && Y == 4 ) { swap ( N , M , mat , N / 2 , 0 , N / 2 , M / 2 ) ; } printMat ( N , M , mat ) ; } static void printMat ( int N , int M , int [ , ] mat ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( mat [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int N = 6 , M = 6 ; int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 , 17 , 18 } , { 19 , 20 , 21 , 22 , 23 , 24 } , { 25 , 26 , 27 , 28 , 29 , 30 } , { 31 , 32 , 33 , 34 , 35 , 36 } } ; int X = 1 , Y = 4 ; swapQuadOfMatrix ( N , M , mat , X , Y ) ; } }
Bottom View of a Binary Tree | C # program to print Bottom View of Binary Tree ; Tree node class ; Data of the node ; Horizontal distance of the node ; left and right references ; Constructor of tree node ; Tree class ; Root node of tree ; Default constructor ; Parameterized tree constructor ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , push ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child add it to the queue with a horizontal distance hd + 1. ; Traverse the map elements using the iterator . ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class Node { public int data ; public int hd ; public Node left , right ; public Node ( int key ) { data = key ; hd = 1000000 ; left = right = null ; } } class Tree { Node root ; public Tree ( ) { } public Tree ( Node node ) { root = node ; } public void bottomView ( ) { if ( root == null ) return ; int hd = 0 ; SortedDictionary < int , int > map = new SortedDictionary < int , int > ( ) ; Queue queue = new Queue ( ) ; root . hd = hd ; queue . Enqueue ( root ) ; while ( queue . Count != 0 ) { Node temp = ( Node ) queue . Dequeue ( ) ; hd = temp . hd ; map [ hd ] = temp . data ; if ( temp . left != null ) { temp . left . hd = hd - 1 ; queue . Enqueue ( temp . left ) ; } if ( temp . right != null ) { temp . right . hd = hd + 1 ; queue . Enqueue ( temp . right ) ; } } foreach ( int i in map . Values ) { Console . Write ( i + " ▁ " ) ; } } } public class BottomView { public static void Main ( string [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; Tree tree = new Tree ( root ) ; Console . WriteLine ( " Bottom ▁ view ▁ of ▁ the ▁ " + " given ▁ binary ▁ tree : " ) ; tree . bottomView ( ) ; } }
Minimum increments by index value required to obtain at least two equal Array elements | C # program to implement the above approach ; Function to calculate the minimum number of steps required ; Stores minimum difference ; Driver code
using System ; class GFG { static void incrementCount ( int [ ] arr , int N ) { int mini = arr [ 0 ] - arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { mini = Math . Min ( mini , arr [ i - 1 ] - arr [ i ] ) ; } Console . WriteLine ( mini ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] arr = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }
Minimum operations required to convert all characters of a String to a given Character | C # program to implement the above approach ; Function to find the minimum number of operations required ; Maximum number of characters that can be changed in one operation ; If length of the String less than maximum number of characters that can be changed in an operation ; Set the last index as the index for the operation ; Otherwise ; If size of the String is equal to the maximum number of characters in an operation ; Find the number of operations required ; Find the starting position ; Print i - th index ; Shift to next index ; Otherwise ; Find the number of operations required ; If n % div exceeds k ; Print i - th index ; Shift to next index ; Driver Code
using System ; class GFG { static void countOperations ( int n , int k ) { int div = 2 * k + 1 ; if ( n / 2 <= k ) { Console . Write ( 1 + " STRNEWLINE " ) ; if ( n > k ) Console . Write ( k + 1 ) ; else Console . Write ( n ) ; } else { if ( n % div == 0 ) { int oprn = n / div ; Console . Write ( oprn + " STRNEWLINE " ) ; int pos = k + 1 ; Console . Write ( pos + " ▁ " ) ; for ( int i = 1 ; i <= oprn ; i ++ ) { Console . Write ( pos + " ▁ " ) ; pos += div ; } } else { int oprn = n / div + 1 ; Console . Write ( oprn + " STRNEWLINE " ) ; int pos = n % div ; if ( n % div > k ) pos -= k ; for ( int i = 1 ; i <= oprn ; i ++ ) { Console . Write ( pos + " ▁ " ) ; pos += div ; } } } } public static void Main ( String [ ] args ) { String str = " edfreqwsazxet " ; int n = str . Length ; int k = 4 ; countOperations ( n , k ) ; } }
Length of largest subsequence consisting of a pair of alternating digits | C # program for the above approach ; Function to find the length of the largest subsequence consisting of a pair of alternating digits ; Variable initialization ; Nested loops for iteration ; Check if i is not equal to j ; Initialize length as 0 ; Iterate from 0 till the size of the String ; Increment length ; Increment length ; Update maxi ; Check if maxi is not equal to 1 the print it otherwise print 0 ; Driver Code ; Given String ; Function call
using System ; class GFG { static void largestSubsequence ( char [ ] s ) { int maxi = 0 ; char prev1 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { if ( i != j ) { int len = 0 ; prev1 = ( char ) ( j + '0' ) ; for ( int k = 0 ; k < s . Length ; k ++ ) { if ( s [ k ] == i + '0' && prev1 == j + '0' ) { prev1 = s [ k ] ; len ++ ; } else if ( s [ k ] == j + '0' && prev1 == i + '0' ) { prev1 = s [ k ] ; len ++ ; } } maxi = Math . Max ( len , maxi ) ; } } } if ( maxi != 1 ) Console . Write ( maxi + " STRNEWLINE " ) ; else Console . Write ( 0 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String s = "1542745249842" ; largestSubsequence ( s . ToCharArray ( ) ) ; } }
Find maximum GCD value from root to leaf in a Binary tree | C # program for the above approach ; Initialise to update the maximum gcd value from all the path ; Node structure ; Left & right child of the node ; Initialize constructor ; Function to find gcd of a and b ; Function to find the gcd of a path ; Function to find the maximum value of gcd from root to leaf in a Binary tree ; Check if root is not null ; Find the maximum gcd of path value and store in global maxm variable ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code ; Given Tree ; Function call ; Print the maximum AND value
using System ; using System . Collections . Generic ; class GFG { static int maxm = 0 ; class Node { public int val ; public Node left , right ; public Node ( int x ) { val = x ; left = null ; right = null ; } } ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int find_gcd ( List < int > arr ) { if ( arr . Count == 1 ) return arr [ 0 ] ; int g = arr [ 0 ] ; for ( int i = 1 ; i < arr . Count ; i ++ ) { g = gcd ( g , arr [ 1 ] ) ; } return g ; } static void maxm_gcd ( Node root , List < int > ans ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { ans . Add ( root . val ) ; maxm = Math . Max ( find_gcd ( ans ) , maxm ) ; return ; } ans . Add ( root . val ) ; maxm_gcd ( root . left , ans ) ; maxm_gcd ( root . right , ans ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 15 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 7 ) ; root . left . left = new Node ( 15 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 31 ) ; root . right . right = new Node ( 9 ) ; maxm_gcd ( root , new List < int > ( ) ) ; Console . Write ( maxm + " STRNEWLINE " ) ; } }
Count of pairs with sum N from first N natural numbers | C # program for the above approach ; Function to calculate the value of count ; Stores the count of pairs ; Set the two pointers ; Check if the sum of pairs is equal to N ; Increase the count of pairs ; Move to the next pair ; Driver code
using System ; class GFG { public static int numberOfPairs ( int n ) { int count = 0 ; int i = 1 , j = n - 1 ; while ( i < j ) { if ( i + j == n ) { count ++ ; } i ++ ; j -- ; } return count ; } public static void Main ( string [ ] args ) { int n = 8 ; Console . Write ( numberOfPairs ( n ) ) ; } }
Count of ways to generate a Matrix with product of each row and column as 1 or | C # implementation of the above approach ; Function to return the number of possible ways ; Check if product can be - 1 ; Driver Code
using System ; class GFG { static void Solve ( int N , int M ) { int temp = ( N - 1 ) * ( M - 1 ) ; int ans = ( int ) ( Math . Pow ( 2 , temp ) ) ; if ( ( N + M ) % 2 != 0 ) Console . Write ( ans ) ; else Console . Write ( 2 * ans ) ; } public static void Main ( string [ ] args ) { int N = 3 ; int M = 3 ; Solve ( N , M ) ; } }
Program to count leaf nodes in a binary tree | C # implementation to find leaf count of a given Binary tree ; Class containing left and right child of current node and key value ; Root of the Binary Tree ; Function to get the count of leaf nodes in a binary tree ; Driver program to test above functions ; create a tree ; get leaf count of the abve tree
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int LeafCount { get { return getLeafCount ( root ) ; } } public virtual int getLeafCount ( Node node ) { if ( node == null ) { return 0 ; } if ( node . left == null && node . right == null ) { return 1 ; } else { return getLeafCount ( node . left ) + getLeafCount ( node . right ) ; } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; 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 ( " The ▁ leaf ▁ count ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . LeafCount ) ; } }
Make a palindromic string from given string | C # implementation to find which player can form a palindromic string first in a game ; Function to find winner of the game ; Array to maintain frequency of the characters in S ; Maintain count of all distinct characters ; Finding frequency of each character ; Count unique duplicate characters ; Loop to count the unique duplicate characters ; Condition for Player - 1 to be winner ; Else Player - 2 is always winner ; Driver Code ; Function call
using System ; class GFG { static int palindromeWinner ( string S ) { int [ ] freq = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < ( int ) S . Length ; ++ i ) { if ( freq [ S [ i ] - ' a ' ] == 0 ) count ++ ; freq [ S [ i ] - ' a ' ] ++ ; } int unique = 0 ; int duplicate = 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 1 ) unique ++ ; else if ( freq [ i ] >= 2 ) duplicate ++ ; } if ( unique == 1 && ( unique + duplicate ) == count ) return 1 ; return 2 ; } public static void Main ( string [ ] s ) { string S = " abcbc " ; Console . Write ( " Player - " + palindromeWinner ( S ) ) ; } }
Number of substrings with length divisible by the number of 1 's in it | C # program to count number of subString under given condition ; Function return count of such substring ; Selection of adequate x value ; Store where 1 's are located ; If there are no ones , then answer is 0 ; For ease of implementation ; Count storage ; Iterate on all k values less than fixed x ; Keeps a count of 1 's occured  during String traversal ; Iterate on String and modify the totCount ; If this character is 1 ; Add to the readonly sum / count ; Increase totCount at exterior position ; Reduce totCount at index + k * n ; Slightly modified prefix sum storage ; Number of 1 's till i-1 ; Traversing over String considering each position and finding bounds and count using the inequalities ; Calculating bounds for l and r ; If valid then add to answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countOfSubStrings ( String s ) { int n = s . Length ; int x = ( int ) Math . Sqrt ( n ) ; List < int > ones = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) ones . Add ( i ) ; } if ( ones . Count == 0 ) return 0 ; ones . Add ( n ) ; int [ ] totCount = new int [ n * x + n ] ; int sum = 0 ; for ( int k = 0 ; k <= x ; k ++ ) { int now = 0 ; totCount [ k * n ] ++ ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; sum += totCount [ index + k * n ] ; totCount [ index + k * n ] ++ ; } now = 0 ; totCount [ k * n ] -- ; for ( int j = 1 ; j <= n ; j ++ ) { if ( s [ j - 1 ] == '1' ) now ++ ; int index = j - k * now ; totCount [ index + k * n ] -- ; } } int [ ] prefix_sum = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) prefix_sum [ i ] = - 1 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ i ] = cnt ; if ( s [ i ] == '1' ) cnt ++ ; } for ( int k = 0 ; k < n ; k ++ ) { for ( int j = 1 ; j <= ( n / x ) && prefix_sum [ k ] + j <= cnt ; j ++ ) { int l = ones [ prefix_sum [ k ] + j - 1 ] - k + 1 ; int r = ones [ prefix_sum [ k ] + j ] - k ; l = Math . Max ( l , j * ( x + 1 ) ) ; if ( l <= r ) { sum += r / j - ( l - 1 ) / j ; } } } return sum ; } public static void Main ( String [ ] args ) { String S = "1111100000" ; Console . Write ( countOfSubStrings ( S ) ) ; } }
Find the largest number smaller than integer N with maximum number of set bits | C # implementation to Find the largest number smaller than integer N with a maximum number of set bits ; Function to get no of set bits in binary representation of positive integer n ; Function to return the largest number less than N ; Iterate through all the numbers ; Find the number of set bits for the current number ; Check if this number has the highest set bits ; Return the result ; Driver code
using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int largestNum ( int n ) { int num = 0 ; int max_setBits = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { int setBits = countSetBits ( i ) ; if ( setBits >= max_setBits ) { num = i ; max_setBits = setBits ; } } return num ; } public static void Main ( String [ ] args ) { int N = 345 ; Console . Write ( largestNum ( N ) ) ; } }
Iterative program to count leaf nodes in a Binary Tree | C # program to count leaf nodes in a Binary Tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; Function to get the count of leaf Nodes in a binary tree ; If tree is empty ; Initialize empty queue . ; Do level order traversal starting from root Initialize count of leaves ; Helper function that allocates a new Node with the given data and null left and right pointers . ; Driver Code ; 1 / \ 2 3 / \ 4 5 Let us create Binary Tree shown in above example ; get leaf count of the above created tree
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } static int getLeafCount ( Node node ) { if ( node == null ) { return 0 ; } Queue < Node > q = new Queue < Node > ( ) ; int count = 0 ; q . Enqueue ( node ) ; while ( q . Count != 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; } if ( temp . left == null && temp . right == null ) { count ++ ; } } return count ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } 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 . WriteLine ( getLeafCount ( root ) ) ; } } }
Find the Kth smallest element in the sorted generated array | C # implementation of the approach ; Pair class implementation to use Pair ; Function to return the Kth element in B [ ] ; Initialize the count Array ; Reduce N repeatedly to half its value ; Add count to start ; Subtract same count after end index ; Store each element of Array [ ] with their count ; If Kth element is in range of element [ i ] return element [ i ] ; If K is out of bound ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Pair { public int first ; public int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } public int getFirst ( ) { return first ; } public int getSecond ( ) { return second ; } } static int solve ( int [ ] Array , int N , int K ) { int [ ] count_Arr = new int [ N + 2 ] ; int factor = 1 ; int size = N ; while ( size > 0 ) { int end = size ; count_Arr [ 1 ] += factor * N ; count_Arr [ end + 1 ] -= factor * N ; factor ++ ; size /= 2 ; } for ( int i = 2 ; i <= N ; i ++ ) count_Arr [ i ] += count_Arr [ i - 1 ] ; List < Pair > element = new List < Pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Pair x = new Pair ( Array [ i ] , count_Arr [ i + 1 ] ) ; element . Add ( x ) ; } int start = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int end = start + element [ 0 ] . getSecond ( ) - 1 ; if ( K >= start && K <= end ) return element [ i ] . getFirst ( ) ; start += element [ i ] . getSecond ( ) ; } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 1 } ; int N = arr . Length ; int K = 13 ; Console . WriteLine ( solve ( arr , N , K ) ) ; } }
Find the lexicographical next balanced bracket sequence | C # implementation of the approach ; makes a string containing char d c number of times ; Function to find the lexicographically next balanced bracket expression if possible ; Decrement the depth for every opening bracket ; Increment for the closing brackets ; Last opening bracket ; Generate the required String ; Driver code
using System ; class GFG { static String strings ( int c , char d ) { String s = " " ; for ( int i = 0 ; i < c ; i ++ ) s += d ; return s ; } static String next_balanced_sequence ( String s ) { String next = " - 1" ; int length = s . Length ; int depth = 0 ; for ( int i = length - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == ' ( ' ) depth -- ; else depth ++ ; if ( s [ i ] == ' ( ' && depth > 0 ) { depth -- ; int open = ( length - i - 1 - depth ) / 2 ; int close = length - i - 1 - open ; next = s . Substring ( 0 , i ) + ' ) ' + strings ( open , ' ( ' ) + strings ( close , ' ) ' ) ; break ; } } return next ; } public static void Main ( String [ ] args ) { String s = " ( ( ( ) ) ) " ; Console . WriteLine ( next_balanced_sequence ( s ) ) ; } }
Maximum number that can be display on Seven Segment Display using N segments | C # implementation of above approach ; Function to print maximum number that can be formed using N segments ; If n is odd ; use 3 three segment to print 7 ; remaining to print 1 ; If n is even ; print n / 2 1 s . ; Driver Code
using System ; class GFG { public static void printMaxNumber ( int n ) { if ( n % 2 != 0 ) { Console . Write ( "7" ) ; for ( int i = 0 ; i < ( n - 3 ) / 2 ; i ++ ) Console . Write ( "1" ) ; } else { for ( int i = 0 ; i < n / 2 ; i ++ ) Console . Write ( "1" ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; printMaxNumber ( n ) ; } }