codeparrot/xlcost-text-to-code huggingface dataset:
Iterative function to check if two trees are identical | Iterative C # program to check if two ; A Binary Tree Node ; Iterative method to find height of Binary Tree ; Return true if both trees are empty ; Return false if one is empty and other is not ; Create an empty queues for simultaneous traversals ; Enqueue Roots of trees in respective queues ; Get front nodes and compare them ; Remove front nodes from queues ; Enqueue left children of both nodes ; If one left child is empty and other is not ; Right child code ( Similar to left child code ) ; Utility function to create a new tree node ; Driver code
using System ; using System . Collections . Generic ; class GfG { class Node { public int data ; public Node left , right ; } static bool areIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new Queue < Node > ( ) ; Queue < Node > q2 = new Queue < Node > ( ) ; q1 . Enqueue ( root1 ) ; q2 . Enqueue ( root2 ) ; while ( q1 . Count != 0 && q2 . Count != 0 ) { Node n1 = q1 . Peek ( ) ; Node n2 = q2 . Peek ( ) ; if ( n1 . data != n2 . data ) return false ; q1 . Dequeue ( ) ; q2 . Dequeue ( ) ; if ( n1 . left != null && n2 . left != null ) { q1 . Enqueue ( n1 . left ) ; q2 . Enqueue ( n2 . left ) ; } else if ( n1 . left != null n2 . left != null ) return false ; if ( n1 . right != null && n2 . right != null ) { q1 . Enqueue ( n1 . right ) ; q2 . Enqueue ( n2 . right ) ; } else if ( n1 . right != null n2 . right != null ) return false ; } return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; if ( areIdentical ( root1 , root2 ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph | C # program to implement the above approach ; Maximum number of nodes or vertices that can be present in the graph ; Store the parent of each vertex ; Stores the size of each set ; Function to initialize the parent of each vertices ; Function to find the representative of the set which contain element v ; Path compression technique to optimize the time complexity ; Function to merge two different set into a single set by finding the representative of each set and merge the smallest set with the larger one ; Finding the set representative of each element ; Check if they have different set repersentative ; Compare the set sizes ; Assign parent of smaller set to the larger one ; Add the size of smaller set to the larger one ; Function to check the vertices are on the same set or not ; Check if they have same set representative or not ; Driver Code ; Connected vertices and taking them into single set ; Function call
using System ; class GFG { static readonly int MAX_NODES = 100005 ; static int [ ] parent = new int [ MAX_NODES ] ; static int [ ] size_set = new int [ MAX_NODES ] ; static void make_set ( int v ) { parent [ v ] = v ; size_set [ v ] = 1 ; } static int find_set ( int v ) { if ( v == parent [ v ] ) return v ; return parent [ v ] = find_set ( parent [ v ] ) ; } static void union_set ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; if ( a != b ) { if ( size_set [ a ] < size_set [ b ] ) { a = a + b ; b = a - b ; a = a - b ; } parent [ b ] = a ; size_set [ a ] += size_set [ b ] ; } } static String check ( int a , int b ) { a = find_set ( a ) ; b = find_set ( b ) ; return ( a == b ) ? " Yes " : " No " ; } public static void Main ( String [ ] args ) { int n = 5 , m = 3 ; make_set ( 1 ) ; make_set ( 2 ) ; make_set ( 3 ) ; make_set ( 4 ) ; make_set ( 5 ) ; union_set ( 1 , 3 ) ; union_set ( 3 , 4 ) ; union_set ( 3 , 5 ) ; Console . Write ( check ( 1 , 5 ) + " STRNEWLINE " ) ; Console . Write ( check ( 3 , 2 ) + " STRNEWLINE " ) ; Console . Write ( check ( 5 , 2 ) + " STRNEWLINE " ) ; } }
Count of N | C # program to implement the above approach ; Function to calculate the total count of N - digit numbers such that the sum of digits at even positions and odd positions are divisible by A and B respectively ; For single digit numbers ; Largest possible number ; Count of possible odd digits ; Count of possible even digits ; Calculate total count of sequences of length even_count with sum divisible by A where first digit can be zero ; Calculate total count of sequences of length odd_count with sum divisible by B where cannot be zero ; Return their product as answer ; Driver Code
using System ; class GFG { public static long count ( int N , int A , int B ) { if ( N == 1 ) { return 9 / B + 1 ; } int max_sum = 9 * N ; int odd_count = N / 2 + N % 2 ; int even_count = N - odd_count ; long [ , ] dp = new long [ even_count , max_sum + 1 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) dp [ 0 , i % A ] ++ ; for ( int i = 1 ; i < even_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp [ i - 1 , k ] > 0 ) dp [ i , ( j + k ) % A ] += dp [ i - 1 , k ] ; } } } long [ , ] dp1 = new long [ odd_count , max_sum + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) dp1 [ 0 , i % B ] ++ ; for ( int i = 1 ; i < odd_count ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { for ( int k = 0 ; k <= max_sum ; k ++ ) { if ( dp1 [ i - 1 , k ] > 0 ) dp1 [ i , ( j + k ) % B ] += dp1 [ i - 1 , k ] ; } } } return dp [ even_count - 1 , 0 ] * dp1 [ odd_count - 1 , 0 ] ; } public static void Main ( String [ ] args ) { int N = 2 , A = 2 , B = 5 ; Console . WriteLine ( count ( N , A , B ) ) ; } }
Maximum possible difference of two subsets of an array | C # find maximum difference of subset sum ; function for maximum subset diff ; if frequency of any element is two make both equal to zero ; driver program
using System ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { bool isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . Abs ( SubsetSum_1 - SubsetSum_2 ) ; } static public void Main ( ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Count of all possible Paths in a Tree such that Node X does not appear before Node Y | C # Program to implement the above approach ; Maximum number of nodes ; List to store the tree ; Function to perform DFS Traversal ; Mark the node as visited ; Initialize the subtree size of each node as 1 ; If the node is same as A ; Mark check_subtree [ node ] as true ; Otherwise ; Iterate over the adjacent nodes ; If the adjacent node is not visited ; Update the size of the subtree of current node ; Check if the subtree of current node contains node A ; Return size of subtree of node ; Function to add edges to the tree ; Function to calculate the number of possible paths ; Stores the size of subtree of each node ; Stores which nodes are visited ; Stores if the subtree of a node contains node A ; DFS Call ; Stores the difference between total number of nodes and subtree size of an immediate child of Y lies between the path from A to B ; Iterate over the adjacent nodes B ; If the node is in the path from A to B ; Calculate the difference ; Return the readonly answer ; Driver Code ; Insert Edges
using System ; using System . Collections . Generic ; class GFG { static int NN = ( int ) 3e5 ; static List < int > [ ] G = new List < int > [ NN + 1 ] ; static int dfs ( int node , int A , int [ ] subtree_size , int [ ] visited , int [ ] check_subtree ) { visited [ node ] = 1 ; subtree_size [ node ] = 1 ; if ( node == A ) { check_subtree [ node ] = 1 ; } else check_subtree [ node ] = 0 ; foreach ( int v in G [ node ] ) { if ( visited [ v ] == 0 ) { subtree_size [ node ] += dfs ( v , A , subtree_size , visited , check_subtree ) ; check_subtree [ node ] = check_subtree [ node ] | check_subtree [ v ] ; } } return subtree_size [ node ] ; } static void addedge ( int node1 , int node2 ) { G [ node1 ] . Add ( node2 ) ; G [ node2 ] . Add ( node1 ) ; } static int numberOfPairs ( int N , int B , int A ) { int [ ] subtree_size = new int [ N + 1 ] ; int [ ] visited = new int [ N + 1 ] ; int [ ] check_subtree = new int [ N + 1 ] ; dfs ( B , A , subtree_size , visited , check_subtree ) ; int difference = 0 ; foreach ( int v in G [ B ] ) { if ( check_subtree [ v ] > 0 ) { difference = N - subtree_size [ v ] ; break ; } } return ( N * ( N - 1 ) ) - difference * ( subtree_size [ A ] ) ; } public static void Main ( String [ ] args ) { int N = 9 ; int X = 5 , Y = 3 ; for ( int i = 0 ; i < G . Length ; i ++ ) G [ i ] = new List < int > ( ) ; addedge ( 0 , 2 ) ; addedge ( 1 , 2 ) ; addedge ( 2 , 3 ) ; addedge ( 3 , 4 ) ; addedge ( 4 , 6 ) ; addedge ( 4 , 5 ) ; addedge ( 5 , 7 ) ; addedge ( 5 , 8 ) ; Console . Write ( numberOfPairs ( N , Y , X ) ) ; } }
Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them | C # program to implement the above approach ; Function to find required number M ; Initialising m ; Finding the index of the most significant bit of N ; Calculating required number ; Driver Code
using System ; class GFG { static int equalXORandOR ( int n ) { int m = 0 ; int MSB = ( int ) Math . Log ( n ) ; for ( int i = 0 ; i <= MSB ; i ++ ) { if ( ( n & ( 1 << i ) ) <= 0 ) { m += ( 1 << i ) ; } } return m ; } public static void Main ( String [ ] args ) { int n = 14 ; Console . Write ( equalXORandOR ( n ) ) ; } }
Maximum possible difference of two subsets of an array | C # find maximum difference of subset sum ; function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; driver program
using System ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . Abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . Abs ( arr [ n - 1 ] ) ; return result ; } static public void Main ( ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Count of Palindromic Strings possible by swapping of a pair of Characters | C # Program to implement the above approach ; Function to return the count of possible palindromic Strings ; Stores the frequencies of each character ; Stores the length of the String ; Increase the number of swaps , the current character make with its previous occurrences ; Increase frequency ; Driver Code
using System ; class GFG { static long findNewString ( String s ) { long ans = 0 ; int [ ] freq = new int [ 26 ] ; int n = s . Length ; for ( int i = 0 ; i < ( int ) s . Length ; ++ i ) { ans += freq [ s [ i ] - ' a ' ] ; freq [ s [ i ] - ' a ' ] ++ ; } return ans ; } public static void Main ( String [ ] args ) { String s = " aaabaaa " ; Console . Write ( findNewString ( s ) ) ; } }
Maximum possible difference of two subsets of an array | C # find maximum difference of subset sum ; Function for maximum subset diff ; Construct hash for positive elements ; Calculate subset sum for positive elements ; Construct hash for negative elements ; Calculate subset sum for negative elements ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxDiff ( int [ ] arr , int n ) { Dictionary < int , int > hashPositive = new Dictionary < int , int > ( ) ; Dictionary < int , int > hashNegative = new Dictionary < int , int > ( ) ; int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . ContainsKey ( arr [ i ] ) ) { hashPositive [ arr [ i ] ] += 1 ; } else { hashPositive . Add ( arr [ i ] , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . ContainsKey ( arr [ i ] ) ) { if ( hashPositive [ arr [ i ] ] == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . ContainsKey ( Math . Abs ( arr [ i ] ) ) ) { hashNegative [ Math . Abs ( arr [ i ] ) ] += 1 ; } else { hashNegative . Add ( Math . Abs ( arr [ i ] ) , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . ContainsKey ( Math . Abs ( arr [ i ] ) ) ) { if ( hashNegative [ Math . Abs ( arr [ i ] ) ] == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . Abs ( SubsetSum_1 - SubsetSum_2 ) ; } static void Main ( ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
Change the given string according to the given conditions | C # program for the above approach ; Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Driver code ; Given string str ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static string proofreadSpell ( string str ) { ArrayList result = new ArrayList ( ) ; List < char > result = new List < char > ( ) ; foreach ( char c in str ) { result . Add ( c ) ; int n = result . Count ; if ( n >= 3 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 1 ] == result [ n - 3 ] ) { result . RemoveAt ( n - 1 ) ; } } n = result . Count ; if ( n >= 4 ) { if ( result [ n - 1 ] == result [ n - 2 ] && result [ n - 3 ] == result [ n - 4 ] ) { result . RemoveAt ( n - 1 ) ; } } } string resultStr = " " ; foreach ( char c in result ) { resultStr += c ; } return resultStr ; } public static void Main ( string [ ] args ) { string str = " hello " ; Console . Write ( proofreadSpell ( str ) ) ; } }
Smallest subarray with k distinct numbers | C # program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code
using System ; using System . Collections . Generic ; public class GFG { public static void minRange ( int [ ] arr , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { ISet < int > s = new HashSet < int > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . Add ( arr [ j ] ) ; if ( s . Count == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) { break ; } } if ( l == 0 && r == n ) { Console . WriteLine ( " Invalid ▁ k " ) ; } else { Console . WriteLine ( l + " ▁ " + r ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 3 ; minRange ( arr , n , k ) ; } }
Smallest subarray with k distinct numbers | C # program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; Increment right side . ; If number of distinct elements less than k . ; If distinct elements are equal to k and length is less than previous length . ; If number of distinct elements less than k , then break . ; If distinct elements equals to k then try to increment left side . ; Increment left side . ; It is same as explained in above loop . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void minRange ( int [ ] arr , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; Dictionary < int , int > hm = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . Count < k ) if ( hm . ContainsKey ( arr [ j ] ) ) hm [ arr [ j ] ] = hm [ arr [ j ] ] + 1 ; else hm . Add ( arr [ j ] , 1 ) ; if ( hm . Count == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . Count < k ) break ; while ( hm . Count == k ) { if ( hm . ContainsKey ( arr [ i ] ) && hm [ arr [ i ] ] == 1 ) hm . Remove ( arr [ i ] ) ; else { if ( hm . ContainsKey ( arr [ i ] ) ) hm [ arr [ i ] ] = hm [ arr [ i ] ] - 1 ; } i ++ ; if ( hm . Count == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . ContainsKey ( arr [ i ] ) && hm [ arr [ i ] ] == 1 ) hm . Remove ( arr [ i ] ) ; else if ( hm . ContainsKey ( arr [ i ] ) ) hm [ arr [ i ] ] = hm [ arr [ i ] ] - 1 ; } if ( l == 0 && r == n ) Console . WriteLine ( " Invalid ▁ k " ) ; else Console . WriteLine ( l + " ▁ " + r ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . Length ; int k = 3 ; minRange ( arr , n , k ) ; } }
Sum of f ( a [ i ] , a [ j ] ) over all pairs in an array of n integers | C # program to calculate the sum of f ( a [ i ] , aj ] ) ; Function to calculate the sum ; Map to keep a count of occurrences ; Traverse in the list from start to end number of times a [ i ] can be in a pair and to get the difference we subtract pre_sum ; If the ( a [ i ] - 1 ) is present then subtract that value as f ( a [ i ] , a [ i ] - 1 ) = 0 ; If the ( a [ i ] + 1 ) is present then add that value as f ( a [ i ] , a [ i ] - 1 ) = 0 here we add as a [ i ] - ( a [ i ] - 1 ) < 0 which would have been added as negative sum , so we add to remove this pair from the sum value ; keeping a counter for every element ; Driver code
using System ; using System . Collections . Generic ; public class GfG { public static int sum ( int [ ] a , int n ) { IDictionary < int , int > cnt = new Dictionary < int , int > ( ) ; int ans = 0 , pre_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . ContainsKey ( a [ i ] - 1 ) ) { ans -= cnt [ a [ i ] - 1 ] ; } if ( cnt . ContainsKey ( a [ i ] + 1 ) ) { ans += cnt [ a [ i ] + 1 ] ; } if ( cnt . ContainsKey ( a [ i ] ) ) { cnt [ a [ i ] ] = cnt [ a [ i ] ] + 1 ; } else { cnt [ a [ i ] ] = 1 ; } } return ans ; } public static void Main ( string [ ] args ) { int [ ] a = new int [ ] { 1 , 2 , 3 , 1 , 3 } ; int n = a . Length ; Console . WriteLine ( sum ( a , n ) ) ; } }
Minimum distance between the maximum and minimum element of a given Array | C # program to implement the above approach ; Function to find the minimum distance between the minimum and the maximum element ; Stores the minimum and maximum array element ; Stores the most recently traversed indices of the minimum and the maximum element ; Stores the minimum distance between the minimum and the maximium ; Find the maximum and the minimum element from the given array ; Find the minimum distance ; Check if current element is equal to minimum ; Check if current element is equal to maximum ; If both the minimum and the maximum element has occurred at least once ; Driver Code
using System ; class GFG { static int minDistance ( int [ ] a , int n ) { int max = - 1 , min = Int32 . MaxValue ; int min_index = - 1 , max_index = - 1 ; int min_dist = n + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > max ) max = a [ i ] ; if ( a [ i ] < min ) min = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == min ) min_index = i ; if ( a [ i ] == max ) max_index = i ; if ( min_index != - 1 && max_index != - 1 ) min_dist = Math . Min ( min_dist , Math . Abs ( min_index - max_index ) ) ; } return min_dist ; } public static void Main ( ) { int n = 12 ; int [ ] a = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 8 , 2 } ; Console . WriteLine ( minDistance ( a , n ) ) ; } }
Kth diagonal from the top left of a given matrix | C # implementation of the above approach ; Function returns required diagonal ; Initialize values to print upper diagonals ; Initialize values to print lower diagonals ; Traverse the diagonal ; Print its contents ; Driver Code
using System ; class GFG { static void printDiagonal ( int K , int N , int [ , ] M ) { int startrow , startcol ; if ( K - 1 < N ) { startrow = K - 1 ; startcol = 0 ; } else { startrow = N - 1 ; startcol = K - N ; } for ( ; startrow >= 0 && startcol < N ; startrow -- , startcol ++ ) { Console . Write ( M [ startrow , startcol ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 3 , K = 4 ; int [ , ] M = { { 4 , 7 , 8 } , { 9 , 2 , 3 } , { 0 , 4 , 1 } } ; printDiagonal ( K , N , M ) ; } }
Longest substring that starts with X and ends with Y | C # program for the above approach ; Function returns length of longest substring starting with X and ending with Y ; Length of string ; Find the length of the string starting with X from the beginning ; Find the length of the string ending with Y from the end ; Longest substring ; Print the length ; Driver code ; Given string str ; Starting and Ending characters ; Function call
using System ; class GFG { static void longestSubstring ( string str , char X , char Y ) { int N = str . Length ; int start = 0 ; int end = N - 1 ; int xPos = 0 ; int yPos = 0 ; while ( true ) { if ( str [ start ] == X ) { xPos = start ; break ; } start ++ ; } while ( true ) { if ( str [ end ] == Y ) { yPos = end ; break ; } end -- ; } int length = ( yPos - xPos ) + 1 ; Console . Write ( length ) ; } public static void Main ( ) { string str = " HASFJGHOGAKZZFEGA " ; char X = ' A ' , Y = ' Z ' ; longestSubstring ( str , X , Y ) ; } }
Find parent of given node in a Binary Tree with given postorder traversal | C # implementation to find the parent of the given node K in a binary tree whose post - order traversal is N natural numbers ; Function to find the parent of the given node ; Condition to check whether the given node is a root node . if it is then return - 1 because root node has no parent ; Loop till we found the given node ; Finding the middle node of the tree because at every level tree parent is divided into two halves ; if the node is found return the parent always the child nodes of every node is node 2 or ( node - 1 ) ; if the node to be found is greater than the mid search for left subtree else search in right subtree ; Driver Code
using System ; class GFG { static int findParent ( int height , int node ) { int start = 1 ; int end = ( int ) Math . Pow ( 2 , height ) - 1 ; if ( end == node ) return - 1 ; while ( node >= 1 ) { end = end - 1 ; int mid = start + ( end - start ) / 2 ; if ( mid == node end == node ) { return ( end + 1 ) ; } else if ( node < mid ) { end = mid ; } else { start = mid ; } } return - 1 ; } public static void Main ( String [ ] args ) { int height = 4 ; int node = 6 ; int k = findParent ( height , node ) ; Console . Write ( k ) ; } }