"Length of longest subsequence whose XOR value is odd | C # program for the above approach ; Function for find max XOR subsequence having odd value ; Initialize odd and even count ; Count the number of odd and even numbers in given array ; If all values are odd in given array ; If all values are even in given array ; If both odd and even are present in given array ; Driver Code ; Given array arr [ ] ; Function Call"
Solution in C#:

using System ; class GFG { static int maxXORSubsequence ( int [ ] arr , int n ) { int i , odd = 0 , even = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } int maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } return maxlen ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . Length ; Console . Write ( maxXORSubsequence ( arr , n ) ) ; } }


"Length of longest subsequence whose difference between maximum and minimum ASCII value of characters is exactly one | C # program for the above approach ; Function to find the maximum length of subsequence having difference of ASCII value of longest and smallest character as 1 ; Stores frequency of characters ; Iterate over characters of the string ; Stores the resultant length of subsequence ; Check if there exists any elements with ASCII value one less than character ch ; Size of current subsequence ; Update the value of ans ; Print the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void maximumLengthSubsequence ( String str ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ch ) ) mp [ ch ] = mp [ ch ] + 1 ; else mp . Add ( ch , 1 ) ; } int ans = 0 ; foreach ( char ch in str . ToCharArray ( ) ) { if ( mp . ContainsKey ( ( char ) ( ch - 1 ) ) ) { int curr_max = mp [ ch ] + mp [ ( char ) ( ch - 1 ) ] ; ans = Math . Max ( ans , curr_max ) ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String S = " acbbebcg " ; maximumLengthSubsequence ( S ) ; } }


"Length of longest subset consisting of A 0 s and B 1 s from an array of strings |  ; Function to count number of 0 s present in the string ; Stores the count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive Function to find the length of longest subset from given array of strings with at most A 0 s and B 1 s ; If idx is equal to N or A + B is equal to 0 ; If the state is already calculated ; Stores the count of 0 's ; Stores the count of 1 's ; Stores the length of longest by including arr [ idx ] ; If zero is less than A and one is less than B ; Stores the length of longest subset by excluding arr [ idx ] ; Assign ; Return ; Function to find the length of the longest subset of an array of strings with at most A 0 s and B 1 s ; Stores all Dp - states ; Return ; Driver Code"
Solution in C#:

using System ; public class GFG { static int count0 ( string s ) { int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { count ++ ; } } return count ; } static int solve ( string [ ] vec , int A , int B , int idx , int [ , , ] dp ) { if ( idx == vec . Length A + B == 0 ) { return 0 ; } if ( dp [ A , B , idx ] > 0 ) { return dp [ A , B , idx ] ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . Length - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 , dp ) ; } int exc = solve ( vec , A , B , idx + 1 , dp ) ; dp [ A , B , idx ] = Math . Max ( inc , exc ) ; return dp [ A , B , idx ] ; } static int MaxSubsetlength ( string [ ] arr , int A , int B ) { int [ , , ] dp = new int [ A + 1 , B + 1 , arr . Length + 1 ] ; return solve ( arr , A , B , 0 , dp ) ; } static public void Main ( ) { string [ ] arr = { "1" , "0" , "10" } ; int A = 1 , B = 1 ; Console . WriteLine ( MaxSubsetlength ( arr , A , B ) ) ; } }


"Length of longest subset consisting of A 0 s and B 1 s from an array of strings | C # program for the above approach ; Function to count 0 's in a string ; Stores count of 0 s ; Iterate over characters of string ; If current character is '0' ; Recursive function to find the length of longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; If idx is equal to N or A + B is equal to 0 ; Stores the count of 0 's in arr[idx] ; Stores the count of 1 's in arr[idx] ; Stores the length of the subset if arr [ i ] is included ; If zero is less than or equal to A and one is less than or equal to B ; Stores the length of the subset if arr [ i ] is excluded ; Returns max of inc and exc ; Function to find the length of the longest subset from an array of strings with at most A 0 ' s ▁ and ▁ B ▁ 1' s ; Return ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int count0 ( string s ) { int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '0' ) { count ++ ; } } return count ; } static int solve ( List < string > vec , int A , int B , int idx ) { if ( idx == vec . Count A + B == 0 ) { return 0 ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . Length - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } int exc = solve ( vec , A , B , idx + 1 ) ; return Math . Max ( inc , exc ) ; } static int MaxSubsetlength ( List < string > arr , int A , int B ) { return solve ( arr , A , B , 0 ) ; } public static void Main ( ) { List < string > arr = new List < string > { "1" , "0" , "10" } ; int A = 1 , B = 1 ; Console . WriteLine ( MaxSubsetlength ( arr , A , B ) ) ; } }


"Length of longest substring having all characters as K | C # program for the above approach ; Function to find the length of longest sub - string having all characters same as character K ; Initialize variables ; Iterate till size of string ; Check if current character is K ; Assingning the max value to max_len ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int length_substring ( string S , char K ) { int curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( int i = 0 ; i < S . Length ; i ++ ) { if ( S [ i ] == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . Max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . Max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } static public void Main ( ) { string S = " abcd1111aabc " ; char K = '1' ; Console . WriteLine ( length_substring ( S , K ) ) ; } }


"Length of longest substring to be deleted to make a string equal to another string | C # program to implement the above approach ; Function to print the length of longest substring to be deleted ; Stores the length of string ; Store the position of previous matched character of str1 ; Store the position of first occurrence of str2 in str1 ; Find the position of the first occurrence of str2 ; Store the index of str1 ; If both characters not matched ; Store the length of the longest deleted substring ; Store the position of last occurrence of str2 in str1 ; If both characters not matched ; Update res ; Update res . ; Driver code ; Given string ; Function call"
Solution in C#:

using System ; class GFG { static int longDelSub ( string str1 , string str2 ) { int N = str1 . Length ; int M = str2 . Length ; int prev_pos = 0 ; int [ ] pos = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { int index = prev_pos ; while ( index < N && str1 [ index ] != str2 [ i ] ) { index ++ ; } pos [ i ] = index ; prev_pos = index + 1 ; } int res = N - prev_pos ; prev_pos = N - 1 ; for ( int i = M - 1 ; i >= 0 ; i -- ) { int index = prev_pos ; while ( index >= 0 && str1 [ index ] != str2 [ i ] ) { index -- ; } if ( i != 0 ) { res = Math . Max ( res , index - pos [ i - 1 ] - 1 ) ; } prev_pos = index - 1 ; } res = Math . Max ( res , prev_pos + 1 ) ; return res ; } public static void Main ( ) { string str1 = " GeeksforGeeks " ; string str2 = " forks " ; Console . Write ( longDelSub ( str1 , str2 ) ) ; } }


"Length of maximum product subarray | C # program to find maximum length subarray having non zero product ; Function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; If zeroindex list is empty then Maxlength is as size of array ; If zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before index 2 so as their product is positive to ; Checking for other indexex ; If the difference is greater than maxlen then maxlen is updated ; To check the length of remaining array after last zeroindex ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void Maxlength ( int [ ] arr , int N ) { int [ ] zeroindex = new int [ 20000 ] ; int maxlen ; int size = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zeroindex [ size ++ ] = i ; } if ( size == 0 ) { maxlen = N ; } else { maxlen = zeroindex [ 0 ] ; for ( int i = 0 ; i < size ; i ++ ) { if ( zeroindex [ i + 1 ] - zeroindex [ i ] - 1 > maxlen ) { maxlen = zeroindex [ i + 1 ] - zeroindex [ i ] - 1 ; } } if ( N - zeroindex [ size - 1 ] - 1 > maxlen ) { maxlen = N - zeroindex [ size - 1 ] - 1 ; } } Console . WriteLine ( maxlen ) ; } public static void Main ( ) { int N = 9 ; int [ ] arr = { 7 , 1 , 0 , 1 , 2 , 0 , 9 , 2 , 1 } ; Maxlength ( arr , N ) ; } }


"Length of remaining two sides of a Triangle from a given side and its adjacent angles | C # program for above approach ; Function for computing other 2 side of the trianlgle ; Computing angle C ; Converting A in to radian ; Converting B in to radian ; Converting C in to radian ; Computing length of side b ; Computing length of side c ; Driver code ; Calling function"
Solution in C#:

using System ; class GFG { static void findSide ( float a , double B , double C ) { double A = 180 - C - B ; double radA = ( Math . PI * ( A / 180 ) ) ; double radB = ( Math . PI * ( B / 180 ) ) ; double radC = ( Math . PI * ( C / 180 ) ) ; double b = ( a / Math . Sin ( radA ) * Math . Sin ( radB ) ) ; double c = ( a / Math . Sin ( radA ) * Math . Sin ( radC ) ) ; Console . Write ( " { 0 : F15 } " , b ) ; Console . Write ( " { 0 : F15 } " , c ) ; } public static void Main ( String [ ] args ) { int a = 12 , B = 60 , C = 30 ; findSide ( a , B , C ) ; } }


"Length of rope tied around three equal circles touching each other | C # code to find the length of rope ; Function to find the length of rope ; Driver code"
Solution in C#:

using System ; class GFG { static double PI = 3.14159265 ; public static double length_rope ( double r ) { return ( ( 2 * PI * r ) + 6 * r ) ; } public static void Main ( ) { double r = 7.0 ; Console . Write ( length_rope ( r ) ) ; } }


"Length of second longest sequence of consecutive 1 s in a binary array | C # implementation of the above approach ; Function to find maximum and second maximum length ; Initialise maximum length ; Initialise second maximum length ; Initialise count ; Iterate over the array ; If sequence ends ; Reset count ; Otherwise ; Increase length of current sequence ; Update maximum ; Traverse the given array ; If sequence continues ; Increase length of current sequence ; Update second max ; Reset count when 0 is found ; Print the result ; Driver code ; Given array ; Function Call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static void FindMax ( int [ ] arr , int N ) { int maxi = - 1 ; int maxi2 = - 1 ; int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( arr [ i ] == 0 ) count = 0 ; else { count ++ ; maxi = Math . Max ( maxi , count ) ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { count ++ ; if ( count > maxi2 && count < maxi ) { maxi2 = count ; } } if ( arr [ i ] == 0 ) count = 0 ; } maxi = Math . Max ( maxi , 0 ) ; maxi2 = Math . Max ( maxi2 , 0 ) ; Console . WriteLine ( maxi2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; FindMax ( arr , n ) ; } }


"Length of smallest meeting that can be attended | C # program to implement the above approach ; Function to find the minimum time to attend exactly one meeting ; Stores minimum time to attend exactly one meeting ; Sort entrance [ ] array ; Sort exit [ ] time ; Traverse meeting [ , ] ; Stores start time of current meeting ; Stores end time of current meeting ; Find just greater value of u in entrance [ ] ; Find just greater or equal value of u in entrance [ ] ; Console . WriteLine ( exit . Count ) ; Stores enter time to attend the current meeting ; Stores exist time after attending the meeting ; Update start lies in range [ 0 , m - 1 ] and end lies in the range [ 0 , p - 1 ] ; Update ans ; Return answer ; vec . Clear ( ) ; ; Driver Code ; Stores interval of meeting ; Stores entrance timings ; Stores exit timings ; Stores total count of meetings ; Stores total entrance timings ; Stores total exit timings ; Minimum time"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static List < int > exit = new List < int > ( ) ; static int minTime ( int [ , ] meeting , int n , int [ ] entrance , int m , int p ) { int ans = int . MaxValue ; Array . Sort ( entrance ) ; exit . Sort ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int u = meeting [ i , 0 ] ; int v = meeting [ i , 1 ] ; int it1 = upper_bound ( entrance , 0 , entrance . Length , u ) ; int it2 = lowerBound ( exit , 0 , exit . Count , v ) ; int start = it1 - 1 ; int end = it2 ; if ( start >= 0 && start < m && end >= 0 && end < p ) ans = Math . Min ( ans , exit [ end ] - entrance [ start ] ) ; } return ans >= int . MaxValue ? - 1 : ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } static int lowerBound ( List < int > vec , int low , int high , int element ) { int [ ] array = new int [ vec . Count ] ; int k = 0 ; foreach ( int val in vec ) { array [ k ] = val ; k ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low ; } public static void Main ( String [ ] args ) { int [ , ] meeting = { { 15 , 19 } , { 5 , 10 } , { 7 , 25 } } ; int [ ] entrance = { 4 , 13 , 25 , 2 } ; exit . Add ( 10 ) ; exit . Add ( 25 ) ; int n = meeting . GetLength ( 0 ) ; int m = entrance . Length ; int p = exit . Count ; Console . Write ( minTime ( meeting , n , entrance , m , p ) + "
 " ) ; } }


"Length of smallest sequence having sum X and product Y | C # program for the above approach ; Function for checking valid or not ; Function for checking boundary of binary search ; Function to calculate the minimum sequence size using binary search ; Initialize high and low ; Base case ; Print - 1 if a sequence cannot be generated ; Otherwise ; Iterate until difference between high and low exceeds 1 ; Calculate mid ; Reset values of high and low accordingly ; Print the answer ; Driver Code ; Function call"
Solution in C#:

 using System ; class GFG { static double temp ( int n , int x ) { return Math . Pow ( x * 1.0 / n , n ) ; } static bool check ( int n , int y , int x ) { double v = temp ( n , x ) ; return ( v >= y ) ; } static void find ( int x , int y ) { int high = ( int ) Math . Floor ( x / Math . Exp ( 1.0 ) ) ; int low = 1 ; if ( x == y ) Console . Write ( 1 + "
 " ) ; else if ( ! check ( high , y , x ) ) Console . Write ( - 1 + "
 " ) ; else { while ( high - low > 1 ) { int mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } Console . Write ( high + "
 " ) ; } } public static void Main ( String [ ] args ) { int x = 9 , y = 8 ; find ( x , y ) ; } }


"Length of smallest subarray required to be removed to make remaining elements consecutive | C # program for the above approach ; Function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; Store the ending index of the longest prefix consecutive array ; Traverse the array to find the longest prefix consecutive sequence ; A [ 0. . . left_index ] is the prefix consecutive sequence ; Store the starting index of the longest suffix consecutive sequence ; Traverse the array to find the longest suffix consecutive sequence ; A [ right_index ... N - 1 ] is the consecutive sequence ; Store the smallest subarray required to be removed ; Check if subarray from the middle can be removed ; Update the right index s . t . A [ 0 , N - 1 ] is consecutive ; If updated_right < N , then update the minimumLength ; Print the required result ; Driver code"
Solution in C#:

using System ; class GFG { static void shortestSubarray ( int [ ] A , int N ) { int i ; int left_index ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] + 1 != A [ i + 1 ] ) break ; } left_index = i ; int right_index ; for ( i = N - 1 ; i >= 1 ; i -- ) { if ( A [ i ] != A [ i - 1 ] + 1 ) break ; } right_index = i ; int updated_right ; int minLength = Math . Min ( N - left_index - 1 , right_index ) ; if ( A [ right_index ] <= A [ left_index ] + 1 ) { updated_right = right_index + A [ left_index ] - A [ right_index ] + 1 ; if ( updated_right < N ) minLength = Math . Min ( minLength , updated_right - left_index - 1 ) ; } Console . WriteLine ( minLength ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 7 , 4 , 3 , 5 } ; int N = arr . Length ; shortestSubarray ( arr , N ) ; } }


"Length of smallest subarray to be removed such that the remaining array is sorted | C # program for the above approach ; Find the length of the shortest subarray ; To store the result ; Calculate the possible length of the sorted subarray from left ; Array is sorted ; Calculate the possible length of the sorted subarray from left ; Update the result ; Calculate the possible length in the middle we can delete and update the result ; Update the result ; Return the result ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static int findLengthOfShortestSubarray ( int [ ] arr , int N ) { int minlength = int . MaxValue ; int left = 0 ; int right = N - 1 ; while ( left < right && arr [ left + 1 ] >= arr [ left ] ) { left ++ ; } if ( left == N - 1 ) return 0 ; while ( right > left && arr [ right - 1 ] <= arr [ right ] ) { right -- ; } minlength = Math . Min ( N - left - 1 , right ) ; int j = right ; for ( int i = 0 ; i < left + 1 ; i ++ ) { if ( arr [ i ] <= arr [ j ] ) { minlength = Math . Min ( minlength , j - i - 1 ) ; } else if ( j < N - 1 ) { j ++ ; } else { break ; } } return minlength ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 } ; int N = arr . Length ; Console . Write ( findLengthOfShortestSubarray ( arr , N ) ) ; } }


"Length of smallest subarray to be removed to make sum of remaining elements divisible by K | C # program for the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is divisible by K ; Stores the remainder of each arr [ i ] when divided by K ; Stores total sum of elements ; K has been added to each arr [ i ] to handle - ve integers ; Update the total sum ; Remainder when total_sum is divided by K ; If given array is already divisible by K ; Stores curr_remainder and the most recent index at which curr_remainder has occurred ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; If not possible ; Print the result ; Driver Code ; Given array [ ] arr ; Size of array ; Given K ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void removeSmallestSubarray ( int [ ] arr , int n , int k ) { int [ ] mod_arr = new int [ n ] ; int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mod_arr [ i ] = ( arr [ i ] + k ) % k ; total_sum += arr [ i ] ; } int target_remainder = total_sum % k ; if ( target_remainder == 0 ) { Console . Write ( "0" ) ; return ; } Dictionary < int , int > map1 = new Dictionary < int , int > ( ) ; map1 . Add ( 0 , - 1 ) ; int curr_remainder = 0 ; int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 [ curr_remainder ] = i ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . ContainsKey ( mod ) ) res = Math . Min ( res , i - map1 [ mod ] ) ; } if ( res == int . MaxValue res == n ) { res = - 1 ; } Console . Write ( res ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 4 , 2 } ; int N = arr . Length ; int K = 6 ; removeSmallestSubarray ( arr , N , K ) ; } }


"Length of the Diagonal of the Octagon | C # Program to find the diagonal of the octagon ; Function to find the diagonal of the octagon ; side cannot be negative ; diagonal of the octagon ; Driver code"
Solution in C#:

using System ; class GFG { static double octadiagonal ( double a ) { if ( a < 0 ) return - 1 ; return a * Math . Sqrt ( 4 + ( 2 * Math . Sqrt ( 2 ) ) ) ; } public static void Main ( ) { double a = 4 ; Console . WriteLine ( octadiagonal ( a ) ) ; } }


"Length of the Smallest Subarray that must be removed in order to Maximise the GCD | C # program to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Function to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Store the maximum possible GCD of the resulting subarray ; Two pointers initially pointing to the first and last element respectively ; Moving the left pointer to the right if the elements are divisible by the maximum GCD ; Moving the right pointer to the left if the elements are divisible by the maximum GCD ; Return the length of the subarray ; Driver code"
Solution in C#:

using System ; class GFG { static int GetMinSubarrayLength ( int [ ] a , int n ) { int ans = Math . Max ( a [ 0 ] , a [ n - 1 ] ) ; int lo = 0 , hi = n - 1 ; while ( lo < n && a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo && a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 8 , 2 , 1 , 4 } ; int N = arr . Length ; int Length = GetMinSubarrayLength ( arr , N ) ; Console . WriteLine ( Length ) ; } }


"Length of the chord the circle if length of the another chord which is equally inclined through the diameter is given | C # program to find the length of the chord the circle if length of the another chord which is equally inclined through the diameter is given ; Driver code"
Solution in C#:

using System ; class GFG { static void lengchord ( int z ) { Console . WriteLine ( " The ▁ length ▁ is ▁ " + z ) ; } static public void Main ( ) { int z = 48 ; lengchord ( z ) ; } }


"Length of the direct common tangent between two externally touching circles | C # program to find the length of the direct common tangent between two circles which externally touch each other ; Function to find the length of the direct common tangent ; Driver code"
Solution in C#:

using System ; class GFG { static void lengtang ( double r1 , double r2 ) { Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ " + " direct ▁ common ▁ tangent ▁ is ▁ " + ( 2 * Math . Sqrt ( r1 * r2 ) ) ) ; } static public void Main ( ) { double r1 = 5 , r2 = 9 ; lengtang ( r1 , r2 ) ; } }


"Length of the largest subarray with contiguous elements | Set 1 |  ; Returns length of the longest contiguous subarray ; Initialize result ; Initialize min and max for all subarrays starting with i ; Consider all subarrays starting with i and ending with j ; Update min and max in this subarray if needed ; If current subarray has all contiguous elements ; Return result ; Driver Code"
Solution in C#:

using System ; class GFG { static int findLength ( int [ ] arr , int n ) { int max_len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int mn = arr [ i ] , mx = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { mn = Math . Min ( mn , arr [ j ] ) ; mx = Math . Max ( mx , arr [ j ] ) ; if ( ( mx - mn ) == j - i ) max_len = Math . Max ( max_len , mx - mn + 1 ) ; } } return max_len ; } public static void Main ( ) { int [ ] arr = { 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ the ▁ longest " + " ▁ contiguous ▁ subarray ▁ is ▁ " + findLength ( arr , n ) ) ; } }


"Length of the largest substring which have character with frequency greater than or equal to half of the substring | C # implementation of the above approach ; Function to return the length of the longest sub string having frequency of a character greater than half of the length of the sub string ; for each of the character ' a ' to ' z ' ; finding frequency prefix array of the character ; Finding the r [ ] and l [ ] arrays . ; for each j from 0 to n ; Finding the lower bound of i . ; storing the maximum value of i - j + 1 ; clearing all the vector so that it can be used for other characters . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int maxLength ( String s , int n ) { int ans = int . MinValue ; List < int > A = new List < int > ( ) ; List < int > L = new List < int > ( ) ; List < int > R = new List < int > ( ) ; int [ ] freq = new int [ n + 5 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( s [ j ] - ' a ' == i ) count ++ ; freq [ j ] = count ; } for ( int j = 1 ; j < n ; j ++ ) { L . Add ( ( 2 * freq [ j - 1 ] ) - j ) ; R . Add ( ( 2 * freq [ j ] ) - j ) ; } int max_len = int . MinValue ; int min_val = int . MaxValue ; for ( int j = 0 ; j < L . Count ; j ++ ) { min_val = Math . Min ( min_val , L [ j ] ) ; A . Add ( min_val ) ; int l = 0 , r = j ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( A [ mid ] <= R [ j ] ) { max_len = Math . Max ( max_len , j - mid + 1 ) ; r = mid - 1 ; } else { l = mid + 1 ; } } } ans = Math . Max ( ans , max_len ) ; A . Clear ( ) ; R . Clear ( ) ; L . Clear ( ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = " ababbbacbcbcca " ; int n = s . Length ; Console . WriteLine ( maxLength ( s , n ) ) ; } }


"Length of the longest ZigZag subarray of the given array | C # implementation to find the length of longest zigzag subarray of the given array ; Function to find the length of longest zigZag contiguous subarray ; ' max ' to store the length of longest zigZag subarray ; ' len ' to store the lengths of longest zigZag subarray at different instants of time ; Traverse the array from the beginning ; Check if ' max ' length is less than the length of the current zigzag subarray . If true , then update ' max ' ; Reset ' len ' to 1 as from this element , again the length of the new zigzag subarray is being calculated ; Comparing the length of the last zigzag subarray with ' max ' ; Return required maximum length ; Driver Code"
Solution in C#:

using System ; class GFG { static int lenOflongZigZagArr ( int [ ] a , int n ) { int max = 1 , len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && ( a [ i ] < a [ i + 1 ] ) ) len ++ ; else if ( i % 2 == 1 && ( a [ i ] > a [ i + 1 ] ) ) len ++ ; else { if ( max < len ) max = len ; len = 1 ; } } if ( max < len ) max = len ; return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( lenOflongZigZagArr ( arr , n ) ) ; } }


"Length of the longest alternating even odd subarray | C # program to find the Length of the longest alternating even odd subarray ; Function to find the longest subarray ; Length of longest alternating subarray ; Iterate in the array ; Increment count if consecutive elements has an odd sum ; Store maximum count in longest ; Reinitialize cnt as 1 consecutive elements does not have an odd sum ; Length of ' longest ' can never be 1 since even odd has to occur in pair or more so return 0 if longest = 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int longestEvenOddSubarray ( int [ ] a , int n ) { int longest = 1 ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] + a [ i + 1 ] ) % 2 == 1 ) { cnt ++ ; } else { longest = Math . Max ( longest , cnt ) ; cnt = 1 ; } } if ( longest == 1 ) return 0 ; return Math . Max ( cnt , longest ) ; } static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int n = a . Length ; Console . WriteLine ( longestEvenOddSubarray ( a , n ) ) ; } }


"Length of the longest increasing subsequence such that no two adjacent elements are coprime | C # program to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; Function to find the length of the longest increasing sub sequence from the given array such that no two adjacent elements are co prime ; To store dp and d value ; To store required answer ; For all elements in the array ; Initially answer is one ; For all it 's divisors ; Update the dp value ; Update the divisor value ; Check for required answer ; Update divisor of a [ i ] ; Return required answer ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 100005 ; static int LIS ( int [ ] a , int n ) { int [ ] dp = new int [ N ] ; int [ ] d = new int [ N ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ a [ i ] ] = 1 ; for ( int j = 2 ; j * j <= a [ i ] ; j ++ ) { if ( a [ i ] % j == 0 ) { dp [ a [ i ] ] = Math . Max ( dp [ a [ i ] ] , dp [ d [ j ] ] + 1 ) ; dp [ a [ i ] ] = Math . Max ( dp [ a [ i ] ] , dp [ d [ a [ i ] / j ] ] + 1 ) ; d [ j ] = a [ i ] ; d [ a [ i ] / j ] = a [ i ] ; } } ans = Math . Max ( ans , dp [ a [ i ] ] ) ; d [ a [ i ] ] = a [ i ] ; } return ans ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; Console . WriteLine ( LIS ( a , n ) ) ; } }


"Length of the longest increasing subsequence which does not contain a given sequence as Subarray | C # program for the above approach ; Initialize dp and KMP array ; Length of the given sequence [ ] ; Function to find the max - length subsequence that does not have subarray sequence [ ] ; Stores the subsequence explored so far ; Base Case ; Using memoization to avoid re - computation ; Using KMP array to find corresponding index in arr1 [ ] ; Incrementing k2 as we are including this element in the subsequence ; Possible answer for current state ; Maximum answer for current state ; Memoizing the answer for the corresponding state ; Return the answer for current state ; Function that generate KMP Array ; Previous longest prefix suffix ; KMPArray [ 0 ] is a always 0 ; The loop calculates KMPArray [ i ] for i = 1 to M - 1 ; If current character is same ; Update the KMP array ; Otherwise ; Update the KMP array ; Function to print the maximum possible length ; Length of the given sequence ; Initialize kmp array ; Generate KMP array ; Initialize dp ; Initialize the states to - 1 ; Get answer ; Print answer ; Driver code ; Given array ; Give arr1 ; Function Call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int [ , , ] dp ; static int [ ] KMPArray ; static int m ; private static int findSubsequence ( int [ ] a , int [ ] sequence , int i , int prev , int k ) { if ( k == m ) return int . MinValue ; if ( i == a . Length ) return 0 ; if ( prev != - 1 && dp [ i , prev , k ] != - 1 ) { return dp [ i , prev , k ] ; } int include = 0 ; if ( prev == - 1 a [ i ] >= a [ prev ] ) { int k2 = k ; while ( k2 > 0 && a [ i ] != sequence [ k2 ] ) k2 = KMPArray [ k2 - 1 ] ; if ( a [ i ] == sequence [ k2 ] ) k2 ++ ; include = 1 + findSubsequence ( a , sequence , i + 1 , i , k2 ) ; } int ans = Math . Max ( include , findSubsequence ( a , sequence , i + 1 , prev , k ) ) ; if ( prev != - 1 ) { dp [ i , prev , k ] = ans ; } return ans ; } private static void fillKMPArray ( int [ ] pattern ) { int j = 0 ; int i = 1 ; KMPArray [ 0 ] = 0 ; while ( i < m ) { if ( pattern [ i ] == pattern [ j ] ) { j ++ ; KMPArray [ i ] = j ; i ++ ; } else { if ( j != 0 ) j = KMPArray [ j - 1 ] ; else { KMPArray [ i ] = j ; i ++ ; } } } } static void printAnswer ( int [ ] a , int [ ] sequence ) { m = sequence . Length ; KMPArray = new int [ m ] ; fillKMPArray ( sequence ) ; dp = new int [ a . Length , a . Length , a . Length ] ; for ( int i = 0 ; i < a . Length ; i ++ ) for ( int j = 0 ; j < a . Length ; j ++ ) for ( int k = 0 ; k < a . Length ; k ++ ) dp [ i , j , k ] = - 1 ; int ans = findSubsequence ( a , sequence , 0 , - 1 , 0 ) ; Console . WriteLine ( ( ans < 0 ) ? 0 : ans ) ; } public static void Main ( ) { int [ ] arr = { 5 , 3 , 9 , 3 , 4 , 7 } ; int [ ] arr1 = { 3 , 4 } ; printAnswer ( arr , arr1 ) ; } }


"Length of the longest path ending at vertex V in a Graph | C # program for the above approach ; Function to perform DFS Traversal from source node to the deepest node and update maximum distance to the deepest node ; Mark source as visited ; Update the maximum distance ; Traverse the adjacency list of the current source node ; Recursively call for the child node ; Backtracking step ; Function to calculate maximum length of the path ending at vertex v from any source node ; Stores the maximum length of the path ending at vertex v ; Stores the size of the matrix ; Stores the adjacency list of the given graph ; Traverse the matrix to create adjacency list ; Perform DFS Traversal to update the maximum distance ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int distance ; static void dfs ( int src , List < List < int > > Adj , List < bool > visited , int level ) { visited [ src ] = true ; distance = Math . Max ( distance , level ) ; foreach ( int child in Adj [ src ] ) { if ( ( child != src ) && ( visited [ child ] == false ) ) { dfs ( child , Adj , visited , level + 1 ) ; } } visited [ src ] = false ; } static int maximumLength ( int [ , ] mat , int v ) { distance = 0 ; int N = mat . GetLength ( 0 ) ; List < bool > visited = new List < bool > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { visited . Add ( false ) ; } List < List < int > > Adj = new List < List < int > > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { Adj . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 0 ) ; j ++ ) { if ( mat [ i , j ] == 1 ) { Adj [ i ] . Add ( j ) ; } } } dfs ( v , Adj , visited , 0 ) ; return distance ; } static void Main ( ) { int [ , ] mat = { { 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 } , { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } } ; int v = 2 ; Console . Write ( maximumLength ( mat , v ) ) ; } }


"Length of the longest subarray whose Bitwise XOR is K | C # program to implement the above approach ; Function to find the length of the longest subarray whose bitwise XOR is equal to K ; Stores prefix XOR of the array ; Stores length of longest subarray having bitwise XOR equal to K ; Stores index of prefix XOR of the array ; Insert 0 into the map ; Traverse the array ; Update prefixXOR ; If ( prefixXOR ^ K ) present in the map ; Update maxLen ; If prefixXOR not present in the Map ; Insert prefixXOR into the map ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int longestLenXORK ( int [ ] arr , int N , int K ) { int prefixXOR = 0 ; int maxLen = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . ContainsKey ( prefixXOR ^ K ) ) { maxLen = Math . Max ( maxLen , ( i - mp [ prefixXOR ^ K ] ) ) ; } if ( ! mp . ContainsKey ( prefixXOR ) ) { mp . Add ( prefixXOR , i ) ; } } return maxLen ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 7 , 2 } ; int N = arr . Length ; int K = 1 ; Console . Write ( longestLenXORK ( arr , N , K ) ) ; } }


"Length of the longest subsequence such that XOR of adjacent elements is equal to K | C # program for above approach ; Function to find maximum length of subsequence ; Stores maximum length of subsequence ; Dictionary to store the longest length of subsequence ending at an integer , say X ; Stores the maximum length of subsequence ending at index i ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Retrieve the longest length of subsequence ending at integer [ ] a ^ K ; If dpj is not NULL ; Update dp [ i ] ; Update ans ; Update the maximum length of subsequence ending at element is a [ i ] in Dictionary ; Return the ans if ans >= 2. Otherwise , return 0 ; Driver Code ; Input ; Print the length of the longest subsequence"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static int xorSubsequence ( int [ ] a , int n , int k ) { int ans = 0 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int [ ] dp = new int [ n ] ; map . Add ( a [ 0 ] , 1 ) ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int dpj = map . ContainsKey ( a [ i ] ^ k ) ? map [ a [ i ] ^ k ] : - 1 ; if ( dpj != 0 ) dp [ i ] = Math . Max ( dp [ i ] , dpj + 1 ) ; ans = Math . Max ( ans , dp [ i ] ) ; if ( map . ContainsKey ( a [ i ] ) ) { map [ a [ i ] ] = Math . Max ( map [ a [ i ] ] + 1 , dp [ i ] ) ; ; } else { map . Add ( a [ i ] , Math . Max ( 1 , dp [ i ] ) ) ; } } return ans >= 2 ? ans : 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 3 , 5 } ; int N = arr . Length ; int K = 1 ; Console . WriteLine ( xorSubsequence ( arr , N , K ) ) ; } }


"Length of the longest subsequence such that XOR of adjacent elements is equal to K | C # program of the above approach ; Function to find maximum length of subsequence having XOR of adjacent elements equal to K ; Store maximum length of subsequence ; Stores the dp - states ; Base case ; Iterate over the range [ 1 , N - 1 ] ; Iterate over the range [ 0 , i - 1 ] ; If arr [ i ] ^ arr [ j ] = = K ; Update the dp [ i ] ; Update the maximum subsequence length ; If length of longest subsequence is less than 2 then return 0 ; Driver code ; Input ; Print the length of longest subsequence"
Solution in C#:

using System ; class GFG { static int xorSubsequence ( int [ ] a , int n , int k ) { int ans = 0 ; int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( a [ i ] ^ a [ j ] ) == k ) dp [ i ] = Math . Max ( dp [ i ] , dp [ j ] + 1 ) ; } ans = Math . Max ( ans , dp [ i ] ) ; dp [ i ] = Math . Max ( 1 , dp [ i ] ) ; } return ans >= 2 ? ans : 0 ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 3 , 5 } ; int K = 1 ; int N = arr . Length ; Console . WriteLine ( xorSubsequence ( arr , N , K ) ) ; } }


"Length of the longest substring that do not contain any palindrome | C # implementation of the above approach ; Function to find the length of the longest substring ; initializing the variables ; checking palindrome of size 2 example : aa ; checking palindrome of size 3 example : aba ; else incrementing length of substring ; max1 = Math . Max ( max1 , len + 1 ) ; finding maximum ; if there exits single character then it is always palindrome ; Driver Code"
Solution in C#:

using System ; class GFG { public static int lenoflongestnonpalindrome ( String s ) { int max1 = 1 , len = 0 ; char [ ] new_str = s . ToCharArray ( ) ; for ( int i = 0 ; i < new_str . Length - 1 ; i ++ ) { if ( new_str [ i ] == new_str [ i + 1 ] ) len = 0 ; else if ( i > 0 && ( new_str [ i + 1 ] == new_str [ i - 1 ] ) ) len = 1 ; len ++ ; } if ( max1 == 1 ) return 0 ; else return max1 ; } public static void Main ( String [ ] args ) { String s = " synapse " ; Console . WriteLine ( lenoflongestnonpalindrome ( s ) ) ; } }


"Length of the longest substring with consecutive characters | C # implementation of the approach ; Function to return the ending index for the largest valid sub - string starting from index i ; If the current character appears after the previous character according to the given circular alphabetical order ; Function to return the length of the longest sub - string of consecutive characters from str ; Valid sub - string exists from index i to end ; Update the length ; Driver code"
Solution in C#:

using System ; class GFG { static int getEndingIndex ( string str , int n , int i ) { i ++ ; while ( i < n ) { char curr = str [ i ] ; char prev = str [ i - 1 ] ; if ( ( curr == ' a ' && prev == ' z ' ) || ( curr - prev == 1 ) ) i ++ ; else break ; } return i - 1 ; } static int largestSubStr ( string str , int n ) { int len = 0 ; int i = 0 ; while ( i < n ) { int end = getEndingIndex ( str , n , i ) ; len = Math . Max ( end - i + 1 , len ) ; i = end + 1 ; } return len ; } public static void Main ( ) { string str = " abcabcdefabc " ; int n = str . Length ; Console . Write ( largestSubStr ( str , n ) ) ; } }


"Length of the longest substring with equal 1 s and 0 s | C # program to find the length of the longest balanced substring ; Function to check if a string contains equal number of one and zeros or not ; Function to find the length of the longest balanced substring ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static bool isValid ( string p ) { int n = p . Length ; int c1 = 0 , c0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] == '0' ) { c0 ++ ; } if ( p [ i ] == '1' ) { c1 ++ ; } } if ( c0 == c1 ) { return true ; } else { return false ; } } public static int longestSub ( string s ) { int max_len = 0 ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( isValid ( s . Substring ( i , j - i + 1 ) ) && max_len < j - i + 1 ) { max_len = j - i + 1 ; } } } return max_len ; } static public void Main ( ) { string s = "101001000" ; Console . WriteLine ( longestSub ( s ) ) ; } }


"Length of the longest substring with every character appearing even number of times | C # program for the above approach ; Function to find length of the longest subString with each element occurring even number of times ; Initialize unordered_map ; Stores the length of the longest required subString ; Traverse the String ; Stores the value of the digit present at current index ; Bitwise XOR of the mask with 1 left - shifted by val ; Check if the value of mask is already present in ind or not ; Update the readonly answer ; Otherwise ; Return the answer ; Driver Code ; Given String ; Length of the given String ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int lenOflongestReqSubstr ( String s , int N ) { Dictionary < int , int > ind = new Dictionary < int , int > ( ) ; int mask = 0 ; ind . Add ( 0 , - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = s [ i ] - '0' ; mask ^= ( 1 << val ) ; if ( ind . ContainsKey ( mask ) ) { ans = Math . Max ( ans , i - ind [ mask ] ) ; } else ind . Add ( mask , i ) ; } return ans ; } public static void Main ( String [ ] args ) { String s = "223015150" ; int N = s . Length ; Console . Write ( lenOflongestReqSubstr ( s , N ) ) ; } }


"Length of the longest substring with no consecutive same letters | C # implementation of the approach ; Function to return the length of the required sub - string ; Get the length of the string ; Iterate in the string ; Check for not consecutive ; If cnt greater than maxi ; Re - initialize ; Check after iteration is complete ; Driver code"
Solution in C#:

using System ; class GfG { static int longestSubstring ( string s ) { int cnt = 1 , maxi = 1 ; int n = s . Length ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] != s [ i - 1 ] ) cnt ++ ; else { maxi = Math . Max ( cnt , maxi ) ; cnt = 1 ; } } maxi = Math . Max ( cnt , maxi ) ; return maxi ; } static void Main ( ) { string s = " ccccdeededff " ; Console . WriteLine ( longestSubstring ( s ) ) ; } }


"Length of the longest substring without repeating characters | C # program to find the length of the longest substring without repeating characters ; Result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code"
Solution in C#:

using System ; class GFG { static int longestUniqueSubsttr ( string str ) { int n = str . Length ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool [ ] visited = new bool [ 256 ] ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str [ j ] ] == true ) break ; else { res = Math . Max ( res , j - i + 1 ) ; visited [ str [ j ] ] = true ; } } visited [ str [ i ] ] = false ; } return res ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( " The ▁ input ▁ string ▁ is ▁ " + str ) ; int len = longestUniqueSubsttr ( str ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " + " non - repeating ▁ character ▁ " + " substring ▁ is ▁ " + len ) ; } }


"Length of the longest substring without repeating characters | C # program to find the length of the longest substring without repeating characters ; This function returns true if all characters in str [ i . . j ] are distinct , otherwise returns false ; Note : Default values in visited are false ; Returns length of the longest substring with all distinct characters . ; Result ; Driver code"
Solution in C#:

using System ; class GFG { public static bool areDistinct ( string str , int i , int j ) { bool [ ] visited = new bool [ 26 ] ; for ( int k = i ; k <= j ; k ++ ) { if ( visited [ str [ k ] - ' a ' ] == true ) return false ; visited [ str [ k ] - ' a ' ] = true ; } return true ; } public static int longestUniqueSubsttr ( string str ) { int n = str . Length ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( areDistinct ( str , i , j ) ) res = Math . Max ( res , j - i + 1 ) ; return res ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( " The ▁ input ▁ string ▁ is ▁ " + str ) ; int len = longestUniqueSubsttr ( str ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ longest ▁ " + " non - repeating ▁ character ▁ " + " substring ▁ is ▁ " + len ) ; } }


"Length of the longest substring without repeating characters | Creating a set to store the last positions of occurrence ; starting the initial point of window to index 0 ; Checking if we have already seen the element or not ; If we have seen the number , move the start pointer to position after the last occurrence ; Updating the last seen value of the character ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int longestUniqueSubsttr ( string s ) { Dictionary < char , int > seen = new Dictionary < char , int > ( ) ; int maximum_length = 0 ; int start = 0 ; for ( int end = 0 ; end < s . Length ; end ++ ) { if ( seen . ContainsKey ( s [ end ] ) ) { start = Math . Max ( start , seen [ s [ end ] ] + 1 ) ; } seen [ s [ end ] ] = end ; maximum_length = Math . Max ( maximum_length , end - start + 1 ) ; } return maximum_length ; } static void Main ( ) { string s = " geeksforgeeks " ; Console . WriteLine ( " The ▁ input ▁ string ▁ is ▁ " + s ) ; int length = longestUniqueSubsttr ( s ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ longest ▁ non - repeating ▁ character ▁ substring ▁ is ▁ " + length ) ; } }


"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"
Solution in C#:

 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 ) + "
 " ) ; str = " ( ) ( ( ) ) ) ) ) " ; Console . Write ( findMaxLen ( str ) + "
 " ) ; } }


"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"
Solution in C#:

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 ) ) ; } }


"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"
Solution in C#:

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 ) ) ; } }


"Length of the normal from origin on a straight line whose intercepts are given | C # implementation of the approach ; Function to find the normal of the straight line ; Length of the normal ; Driver code"
Solution in C#:

using System ; class GFG { static float normal ( float m , float n ) { float N = ( float ) ( ( Math . Abs ( m ) * Math . Abs ( n ) ) / Math . Sqrt ( ( Math . Abs ( m ) * Math . Abs ( m ) ) + ( Math . Abs ( n ) * Math . Abs ( n ) ) ) ) ; return N ; } public static void Main ( ) { float m = - 5 , n = 3 ; Console . Write ( normal ( m , n ) ) ; } }


"Length of the perpendicular bisector of the line joining the centers of two circles | C # program to find the Length of the perpendicular bisector of the line joining the centers of two circles in which one lies completely inside touching the bigger circle at one point ; Driver code"
Solution in C#:

using System ; class GFG { static void lengperpbisect ( double r1 , double r2 ) { double z = 2 * Math . Sqrt ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ; Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ " + " perpendicular ▁ bisector ▁ is ▁ " + z ) ; } public static void Main ( ) { double r1 = 5 , r2 = 3 ; lengperpbisect ( r1 , r2 ) ; } }


"Length of the smallest number which is divisible by K and formed by using 1 's only | C # implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Generate all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's ; If number is divisible by k then return the length ; Driver code"
Solution in C#:

using System ; class GFG { static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return - 1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) return len ; } return - 1 ; } static void Main ( ) { int K = 7 ; Console . WriteLine ( numLen ( K ) ) ; } }


"Length of the smallest number which is divisible by K and formed by using 1 's only | C # implementation of the approach ; Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's  Take remainder with K ; If number is divisible by k then remainder will be 0 ; Driver code"
Solution in C#:

using System ; class GFG { public static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) return - 1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = ( number * 10 + 1 ) % K ; if ( number == 0 ) return len ; } return - 1 ; } public static void Main ( ) { int K = 7 ; Console . WriteLine ( numLen ( K ) ) ; } }


"Length of the smallest sub | C # program to find the length of the smallest substring consisting of maximum distinct characters ; Find maximum distinct characters in any string ; Initialize all character 's count with 0 ; Increase the count in array if a character is found ; size of given string ; Find maximum distinct characters in any string ; result ; Brute force approach to find all substrings ; We have to check here both conditions together 1. substring ' s ▁ distinct ▁ characters ▁ is ▁ equal ▁ ▁ to ▁ maximum ▁ distinct ▁ characters ▁ ▁ 2 . ▁ substring ' s length should be minimum ; Driver program to test above function ; Input String"
Solution in C#:

using System ; class GFG { static int NO_OF_CHARS = 256 ; static int max_distinct_char ( String str , int n ) { int [ ] count = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < n ; i ++ ) { count [ str [ i ] ] ++ ; } int max_distinct = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } static int smallesteSubstr_maxDistictChar ( String str ) { int n = str . Length ; int max_distinct = max_distinct_char ( str , n ) ; int minl = n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { String subs = null ; if ( i < j ) subs = str . Substring ( i , str . Length - j ) ; else subs = str . Substring ( j , str . Length - i ) ; int subs_lenght = subs . Length ; int sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } static public void Main ( String [ ] args ) { String str = " AABBBCBB " ; int len = smallesteSubstr_maxDistictChar ( str ) ; Console . WriteLine ( " ▁ The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " + " ▁ consisting ▁ of ▁ maximum ▁ distinct ▁ " + " characters ▁ : ▁ " + len ) ; } }


"Length of the smallest subarray with maximum possible sum | C # program for the above approach ; Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver code"
Solution in C#:

using System ; class GFG { static int minimumSizeSubarray ( int [ ] arr , int N ) { int i = 0 , j = N - 1 ; while ( i < N && arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 && arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } public static void Main ( ) { int [ ] arr = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 } ; int N = arr . Length ; Console . Write ( minimumSizeSubarray ( arr , N ) ) ; } }


"Length of the smallest substring which contains all vowels | C # program to find the length of the smallest subString of which contains all vowels ; Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current subString ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid subString ; Driver code"
Solution in C#:

using System ; class GFG { static int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return - 1 ; } static int findMinLength ( String s ) { int n = s . Length ; int ans = n + 1 ; int start = 0 ; int [ ] count = new int [ 5 ] ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s [ x ] ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s [ start ] ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s [ start ] ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . Min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } public static void Main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; Console . Write ( findMinLength ( s ) ) ; } }


"Length of the smallest substring which contains all vowels | C # program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static int findMinLength ( String s ) { int n = s . Length ; int i = 0 ; Dictionary < char , int > counts = new Dictionary < char , int > ( ) ; List < int > indices = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' o ' s [ i ] == ' i ' s [ i ] == ' u ' ) { if ( counts . ContainsKey ( s [ i ] ) ) { counts [ s [ i ] ] = counts [ s [ i ] ] + 1 ; } else { counts . Add ( s [ i ] , 1 ) ; } indices . Add ( i ) ; } } if ( counts . Count < 5 ) return - 1 ; int flag1 = 0 , flag2 = 0 ; i = 0 ; int j = indices . Count - 1 ; while ( ( j - i ) >= 4 ) { if ( flag1 == 0 && counts [ s [ indices [ i ] ] ] > 1 ) { counts [ s [ indices [ i ] ] ] = counts [ s [ indices [ i ] ] ] - 1 ; i ++ ; } else flag1 = 1 ; if ( flag2 == 0 && counts [ s [ indices [ j ] ] ] > 1 ) { counts [ s [ indices [ j ] ] ] = counts [ s [ indices [ j ] ] ] - 1 ; j -- ; } else flag2 = 1 ; if ( flag1 == 1 && flag2 == 1 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } public static void Main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; Console . Write ( findMinLength ( s ) ) ; } }


"Length of the transverse common tangent between the two non intersecting circles | C # program to find the length of the transverse common tangent between two circles which do not touch each other ; Function to find the length of the transverse common tangent ; Driver code"
Solution in C#:

using System ; class GFG { static void lengthOfTangent ( double r1 , double r2 , double d ) { Console . WriteLine ( " The ▁ length ▁ of ▁ the ▁ transverse " + " ▁ common ▁ tangent ▁ is ▁ " + Math . Sqrt ( Math . Pow ( d , 2 ) - Math . Pow ( ( r1 + r2 ) , 2 ) ) ) ; } static public void Main ( ) { double r1 = 4 , r2 = 6 , d = 12 ; lengthOfTangent ( r1 , r2 , d ) ; } }


"Lengths of maximized partitions of a string such that each character of the string appears in one substring | C # program for the above approach ; Function to find the length of all partitions oof a String such that each characters occurs in a single subString ; Stores last index of String s ; Find the last position of each letter in the String ; Update the last index ; Iterate the given String ; Get the last index of s [ i ] ; Extend the current partition characters last pos ; Increase len of partition ; if the current pos of character equals the min pos then the end of partition ; Store the length ; Print all the partition lengths ; Driver Code ; Given String str ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void partitionString ( String s ) { int n = s . Length ; List < int > ans = new List < int > ( ) ; if ( n == 0 ) { Console . Write ( " - 1" ) ; return ; } int [ ] last_pos = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; ++ i ) { last_pos [ i ] = - 1 ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( last_pos [ s [ i ] - ' a ' ] == - 1 ) { last_pos [ s [ i ] - ' a ' ] = i ; } } int minp = - 1 , plen = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int lp = last_pos [ s [ i ] - ' a ' ] ; minp = Math . Max ( minp , lp ) ; ++ plen ; if ( i == minp ) { ans . Add ( plen ) ; minp = - 1 ; plen = 0 ; } } for ( int i = 0 ; i < ( int ) ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " acbbcc " ; partitionString ( str ) ; } }


"Leonardo Number | A simple recursive program to find n - th leonardo number . ; Driver code"
Solution in C#:

using System ; class GFG { static int leonardo ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; } public static void Main ( ) { Console . WriteLine ( leonardo ( 3 ) ) ; } }


"Level of Each node in a Tree from source node ( using BFS ) | C # Program to determine level of each node and print level ; function to determine level of each node starting from x using BFS ; array to store level of each node ; create a queue ; enqueue element x ; initialize level of source node to 0 ; marked it as visited ; do until queue is empty ; get the first element of queue ; dequeue element ; traverse neighbors of node x ; b is neighbor of node x ; if b is not marked already ; enqueue b in queue ; level of b is level of x + 1 ; mark b ; display all nodes and their levels ; Driver Code ; adjacency graph for tree ; call levels function with source as 0"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printLevels ( List < List < int > > graph , int V , int x ) { int [ ] level = new int [ V ] ; Boolean [ ] marked = new Boolean [ V ] ; Queue < int > que = new Queue < int > ( ) ; que . Enqueue ( x ) ; level [ x ] = 0 ; marked [ x ] = true ; while ( que . Count > 0 ) { x = que . Peek ( ) ; que . Dequeue ( ) ; for ( int i = 0 ; i < graph [ x ] . Count ; i ++ ) { int b = graph [ x ] [ i ] ; if ( ! marked [ b ] ) { que . Enqueue ( b ) ; level [ b ] = level [ x ] + 1 ; marked [ b ] = true ; } } } Console . WriteLine ( " Nodes " + " ▁ " + " Level " ) ; for ( int i = 0 ; i < V ; i ++ ) Console . WriteLine ( " ▁ " + i + " ▁ - - > ▁ " + level [ i ] ) ; } public static void Main ( String [ ] args ) { int V = 8 ; List < List < int > > graph = new List < List < int > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) graph . Add ( new List < int > ( ) ) ; graph [ 0 ] . Add ( 1 ) ; graph [ 0 ] . Add ( 2 ) ; graph [ 1 ] . Add ( 3 ) ; graph [ 1 ] . Add ( 4 ) ; graph [ 1 ] . Add ( 5 ) ; graph [ 2 ] . Add ( 5 ) ; graph [ 2 ] . Add ( 6 ) ; graph [ 6 ] . Add ( 7 ) ; printLevels ( graph , V , 0 ) ; } }


"Level order traversal line by line | Set 3 ( Using One Queue ) | C # program to do level order traversal line by line ; A Binary Tree Node ; Prints level order traversal line by line using two queues . ; Create an empty queue for level order traversal ; Pushing root node into the queue . ; Pushing delimiter into the queue . ; Executing loop till queue becomes empty ; condition to check the occurence of next level ; Pushing left child current node ; Pushing right child current node ; Driver code ; Let us create binary tree shown above"
Solution in C#:

using System ; using System . Collections ; class GFG { public class Node { public int data ; public Node left ; public Node right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } static void levelOrder ( Node root ) { if ( root == null ) return ; Queue q = new Queue ( ) ; q . Enqueue ( root ) ; q . Enqueue ( null ) ; while ( q . Count > 0 ) { Node curr = ( Node ) q . Peek ( ) ; q . Dequeue ( ) ; if ( curr == null ) { if ( q . Count > 0 ) { q . Enqueue ( null ) ; Console . WriteLine ( ) ; } } else { if ( curr . left != null ) q . Enqueue ( curr . left ) ; if ( curr . right != null ) q . Enqueue ( curr . right ) ; Console . Write ( curr . data + " ▁ " ) ; } } } static public void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; levelOrder ( root ) ; } }


"Level order traversal with direction change after every two levels | C # program to print Zig - Zag traversal in groups of size 2. ; A Binary Tree Node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; For null root ; Maintain a queue for normal level order traversal ; Maintain a stack for printing nodes in reverse order after they are popped out from queue . ; sz is used for storing the count of nodes in a level ; Used for changing the direction of level order traversal ; Used for changing the direction of level order traversal ; Push root node to the queue ; Run this while loop till queue got empty ; Do a normal level order traversal ; For printing nodes from left to right , simply print the nodes in the order in which they are being popped out from the queue . ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of printing nodes after every two levels . ; Utility function to create a new tree node ; Driver Code ; Let us create binary tree"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { public class Node { public Node left ; public int data ; public Node right ; } ; static void modifiedLevelOrder ( Node node ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { Console . Write ( node . data ) ; return ; } Queue < Node > myQueue = new Queue < Node > ( ) ; Stack < Node > myStack = new Stack < Node > ( ) ; Node temp = null ; int sz ; int ct = 0 ; bool rightToLeft = false ; myQueue . Enqueue ( node ) ; while ( myQueue . Count != 0 ) { ct ++ ; sz = myQueue . Count ; for ( int i = 0 ; i < sz ; i ++ ) { temp = myQueue . Peek ( ) ; myQueue . Dequeue ( ) ; if ( rightToLeft == false ) Console . Write ( temp . data + " ▁ " ) ; else myStack . Push ( temp ) ; if ( temp . left != null ) myQueue . Enqueue ( temp . left ) ; if ( temp . right != null ) myQueue . Enqueue ( temp . right ) ; } if ( rightToLeft == true ) { while ( myStack . Count != 0 ) { temp = myStack . Peek ( ) ; myStack . Pop ( ) ; Console . Write ( temp . data + " ▁ " ) ; } } if ( ct == 2 ) { rightToLeft = ! rightToLeft ; ct = 0 ; } Console . Write ( "
 " ) ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } 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 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root...


"Level order traversal with direction change after every two levels | C # program to print Zig - Zag traversal in groups of size 2. ; A Binary Tree Node ; Utility function to create a new tree node ; Function to print the level order of given binary tree . Direction of printing level order traversal of binary tree changes after every two levels ; Run this while loop till queue got empty ; For printing nodes from right to left , push the nodes to stack instead of printing them . ; for printing the nodes in order from right to left ; Change the direction of traversal . ; Driver code ; Let us create binary tree"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static readonly int LEFT = 0 ; static readonly int RIGHT = 1 ; static int ChangeDirection ( int Dir ) { Dir = 1 - Dir ; return Dir ; } public class node { public int data ; public node left , right ; } ; static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void modifiedLevelOrder ( node root ) { if ( root == null ) return ; int dir = LEFT ; node temp ; Queue < node > Q = new Queue < node > ( ) ; Stack < node > S = new Stack < node > ( ) ; S . Push ( root ) ; while ( Q . Count != 0 S . Count != 0 ) { while ( S . Count != 0 ) { temp = S . Peek ( ) ; S . Pop ( ) ; Console . Write ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) Q . Enqueue ( temp . left ) ; if ( temp . right != null ) Q . Enqueue ( temp . right ) ; } else { if ( temp . right != null ) Q . Enqueue ( temp . right ) ; if ( temp . left != null ) Q . Enqueue ( temp . left ) ; } } Console . WriteLine ( ) ; while ( Q . Count != 0 ) { temp = Q . Peek ( ) ; Q . Dequeue ( ) ; Console . Write ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) S . Push ( temp . left ) ; if ( temp . right != null ) S . Push ( temp . right ) ; } else { if ( temp . right != null ) S . Push ( temp . right ) ; if ( temp . left != null ) S . Push ( temp . left ) ; } } Console . WriteLine ( ) ; dir = ChangeDirection ( dir ) ; } } 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 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 )...


"Level with maximum number of nodes | C # implementation to find the level having maximum number of Nodes ; 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 . ; function to find the level having maximum number of Nodes ; Current level ; Maximum Nodes at same level ; Level having maximum Nodes ; Count Nodes in a level ; If it is maximum till now Update level_no to current level ; Pop complete current level ; Increment for next level ; Driver program to test above ; binary tree formation 2 ; / \ ; 1 3 ; / \ \ ; 4 6 8 ; / ; 5 ;"
Solution in C#:

using System ; using System . Collections . Generic ; public class GfG { public class Node { public int data ; public Node left ; public Node right ; } public static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static int maxNodeLevel ( Node root ) { if ( root == null ) { return - 1 ; } LinkedList < Node > q = new LinkedList < Node > ( ) ; q . AddLast ( root ) ; int level = 0 ; int max = int . MinValue ; int level_no = 0 ; while ( true ) { int NodeCount = q . Count ; if ( NodeCount == 0 ) { break ; } if ( NodeCount > max ) { max = NodeCount ; level_no = level ; } while ( NodeCount > 0 ) { Node Node = q . First . Value ; q . RemoveFirst ( ) ; if ( Node . left != null ) { q . AddLast ( Node . left ) ; } if ( Node . right != null ) { q . AddLast ( Node . right ) ; } NodeCount -- ; } level ++ ; } return level_no ; } public static void Main ( string [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 8 ) ; root . left . right . left = newNode ( 5 ) ; Console . WriteLine ( " Level ▁ having ▁ maximum ▁ number ▁ of ▁ Nodes ▁ : ▁ " + maxNodeLevel ( root ) ) ; } }


"Levelwise Alternating OR and XOR operations in Segment Tree | C # program to build levelwise OR / XOR alternating Segment tree ; A utility function to get the middle index from corner indexes . ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st operation denotes which operation is carried out at that level to merge the left and right child . It 's either 0 or 1. ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the sum of values in this node ; Build the left and the right subtrees by using the fact that operation at level ( i + 1 ) = ! ( operation at level i ) ; Merge the left and right subtrees by checking the operation to be carried . If operation = 1 , then do OR else XOR ; OR operation ; XOR operation ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Operation = 1 ( XOR ) if Height of tree is even else it is 0 ( OR ) for the root node ; Fill the allocated memory st ; Return the constructed segment tree ; Driver Code ; Leaf nodes ; Build the segment tree ; Root node is at index 0 considering 0 - based indexing in segment Tree ; Print value at rootIndex"
Solution in C#:

using System ; class GFG { static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static void constructSTUtil ( int [ ] arr , int ss , int se , int [ ] st , int si , bool operation ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return ; } int mid = getMid ( ss , se ) ; constructSTUtil ( arr , ss , mid , st , si * 2 + 1 , ! operation ) ; constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 , ! operation ) ; if ( operation ) { st [ si ] = ( st [ 2 * si + 1 ] st [ 2 * si + 2 ] ) ; } else { st [ si ] = ( st [ 2 * si + 1 ] ^ st [ 2 * si + 2 ] ) ; } } static int [ ] constructST ( int [ ] arr , int n ) { int x = ( int ) ( Math . Ceiling ( Math . Log ( n ) / Math . Log ( 2 ) ) ) ; int max_size = 2 * ( int ) Math . Pow ( 2 , x ) - 1 ; int [ ] st = new int [ max_size ] ; bool operationAtRoot = ! ( x % 2 == 0 ) ; constructSTUtil ( arr , 0 , n - 1 , st , 0 , operationAtRoot ) ; return st ; } static public void Main ( ) { int [ ] leaves = { 1 , 6 , 3 , 7 , 5 , 9 , 10 , 4 } ; int n = leaves . Length ; int [ ] segmentTree = constructST ( leaves , n ) ; int rootIndex = 0 ; Console . WriteLine ( " Value ▁ at ▁ Root ▁ Node ▁ = ▁ " + segmentTree [ rootIndex ] ) ; } }


"Lexicographic rank of a Binary String | C # program to implement the above approach ; Function to find the rank of a String ; Store the length of the String ; Stores its equivalent decimal value ; Traverse the String ; Store the number of string s of length less than N occurring before the given String ; Store the decimal equivalent number of String bin ; Store the rank in answer ; Print the answer ; Driver Code"
Solution in C#:

using System ; class GFG { static void findRank ( string s ) { int N = s . Length ; string bin = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '0' ) bin += "0" ; else bin += "1" ; } int X = 1 << N ; int res = 0 , val = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) res += ( val ) ; val *= 2 ; } int Y = res ; int ans = X + Y - 1 ; Console . Write ( ans ) ; } public static void Main ( ) { string S = "001" ; findRank ( S ) ; } }


"Lexicographic rank of a string among all its substrings | C # program for the above approach ; Function to find lexicographic rank of String among all its subString ; Length of String ; Traverse the given String and store the indices of each character ; Extract the index ; Store it in the vector ; Traverse the alphaIndex array lesser than the index of first character of given String ; If alphaIndex [ i ] size exceeds 0 ; Traverse over the indices ; Add count of subString equal to n - alphaIndex [ i , j ] ; Store all subStrings in a vector str starting with the first character of the given String ; Insert the current character to subString ; Store the subString formed ; Sort the subString in the lexicographical order ; Find the rank of given String ; increase the rank until the given String is same ; If subString is same as the given String ; Add 1 to rank of the given String ; Driver Code ; Given String ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int lexicographicRank ( char [ ] s ) { int n = s . Length ; List < int > [ ] alphaIndex = new List < int > [ 26 ] ; for ( int i = 0 ; i < alphaIndex . Length ; i ++ ) alphaIndex [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) { int x = s [ i ] - ' a ' ; alphaIndex [ x ] . Add ( i ) ; } int rank = 0 ; for ( int i = 0 ; i < 26 && ' a ' + i < s [ 0 ] ; i ++ ) { if ( alphaIndex [ i ] . Count > 0 ) { for ( int j = 0 ; j < alphaIndex [ i ] . Count ; j ++ ) { rank = rank + ( n - alphaIndex [ i ] [ j ] ) ; } } } List < String > str = new List < String > ( ) ; for ( int i = 0 ; i < alphaIndex [ s [ 0 ] - ' a ' ] . Count ; i ++ ) { String subString = " " ; int j = alphaIndex [ s [ 0 ] - ' a ' ] [ i ] ; for ( ; j < n ; j ++ ) { subString += ( s [ j ] ) ; str . Add ( subString ) ; } } str . Sort ( ) ; for ( int i = 0 ; i < str . Count ; i ++ ) { if ( ! str [ i ] . Equals ( String . Join ( " " , s ) ) ) { rank ++ ; } else { break ; } } return rank + 1 ; } public static void Main ( String [ ] args ) { String str = " enren " ; Console . Write ( lexicographicRank ( str . ToCharArray ( ) ) ) ; } }


"Lexicographic rank of a string | A O ( n ) solution for finding rank of string ; all elements of count [ ] are initialized with 0 ; A utility function to find factorial of n ; Construct a count array where value at every index contains count of smaller characters in whole string ; Removes a character ch from count [ ] array constructed by populateAndIncreaseCount ( ) ; A function to find rank of a string in all permutations of characters ; Populate the count array such that count [ i ] contains count of characters which are present in str and are smaller than i ; count number of chars smaller than str [ i ] fron str [ i + 1 ] to str [ len - 1 ] ; Reduce count of characters greater than str [ i ] ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 256 ; int [ ] count = new int [ MAX_CHAR ] ; static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static void populateAndIncreaseCount ( int [ ] count , char [ ] str ) { int i ; for ( i = 0 ; i < str . Length ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } static void updatecount ( int [ ] count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } static int findRank ( char [ ] str ) { int len = str . Length ; int mul = fact ( len ) ; int rank = 1 , i ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } public static void Main ( String [ ] args ) { char [ ] str = " string " . ToCharArray ( ) ; Console . WriteLine ( findRank ( str ) ) ; } }


"Lexicographic rank of a string | C # program to find lexicographic rank of a string ; A utility function to find factorial of n ; A utility function to count smaller characters on right of arr [ low ] ; A function to find rank of a string in all permutations of characters ; count number of chars smaller than str [ i ] from str [ i + 1 ] to str [ len - 1 ] ; Driver program to test above function"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static int findSmallerInRight ( string str , int low , int high ) { int countRight = 0 , i ; for ( i = low + 1 ; i <= high ; ++ i ) if ( str [ i ] < str [ low ] ) ++ countRight ; return countRight ; } static int findRank ( string str ) { int len = str . Length ; int mul = fact ( len ) ; int rank = 1 ; int countRight ; for ( int i = 0 ; i < len ; ++ i ) { mul /= len - i ; countRight = findSmallerInRight ( str , i , len - 1 ) ; rank += countRight * mul ; } return rank ; } public static void Main ( ) { string str = " string " ; Console . Write ( findRank ( str ) ) ; } }


"Lexicographic smallest permutation of a String containing the second String as a Substring | C # program to implement the above approach ; Function to print the desired lexicographic smaller String ; Calculate length of the String ; Stores the frequencies of characters of String str1 ; Stores the frequencies of characters of String str2 ; Decrease the frequency of second String from that of of the first String ; To store the resultant String ; To find the index of first character of the String str2 ; Append the characters in lexicographical order ; Append all the current character ( i + ' a ' ) ; If we reach first character of String str2 append str2 ; Return the resultant String ; Driver Code"
Solution in C#:

using System ; class GFG { static String findSmallestString ( String str1 , String str2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; int n1 = str1 . Length ; int n2 = str2 . Length ; for ( int i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } String res = " " ; int minIndex = str2 [ 0 ] - ' a ' ; for ( int i = 0 ; i < 26 ; ++ i ) { for ( int j = 0 ; j < freq1 [ i ] ; ++ j ) { res += ( char ) ( i + ' a ' ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } public static void Main ( String [ ] args ) { String str1 = " geeksforgeeksfor " ; String str2 = " for " ; Console . Write ( findSmallestString ( str1 , str2 ) ) ; } }


"Lexicographical Maximum substring of string | C # program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static string LexicographicalMaxString ( string str ) { char maxchar = ' a ' ; List < int > index = new List < int > ( ) ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] >= maxchar ) { maxchar = str [ i ] ; index . Add ( i ) ; } } string maxstring = " " ; for ( int i = 0 ; i < index . Count ; i ++ ) { if ( str . Substring ( index [ i ] ) . CompareTo ( maxstring ) > 0 ) { maxstring = str . Substring ( index [ i ] ) ; } } return maxstring ; } static public void Main ( ) { string str = " acbacbc " ; Console . Write ( LexicographicalMaxString ( str ) ) ; } }


"Lexicographical Maximum substring of string | C # program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; Driver code"
Solution in C#:

using System ; public class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . Length ; ++ i ) { if ( mx . CompareTo ( str . Substring ( i ) ) <= 0 ) { mx = str . Substring ( i ) ; } } return mx ; } public static void Main ( ) { String str = " ababaa " ; Console . WriteLine ( LexicographicalMaxString ( str ) ) ; } }


"Lexicographical concatenation of all substrings of a string | C # Program to create concatenation of all substrings in lexicographic order . ; Creating an array to store substrings ; finding all substrings of string ; Sort all substrings in lexicographic order ; Concatenating all substrings ; Driver code"
Solution in C#:

using System ; class GFG { static String lexicographicSubConcat ( String s ) { int n = s . Length ; int sub_count = n * ( n + 1 ) / 2 ; String [ ] arr = new String [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) { arr [ index ++ ] = s . Substring ( i , len ) ; } Array . Sort ( arr ) ; String res = " " ; for ( int i = 0 ; i < sub_count ; i ++ ) res += arr [ i ] ; return res ; } public static void Main ( String [ ] args ) { String s = " abc " ; Console . WriteLine ( lexicographicSubConcat ( s ) ) ; } }


"Lexicographical ordering using Heap Sort | C # implementation to print the string in Lexicographical order ; Used for index in heap ; Predefining the heap array ; Defining formation of the heap ; Iterative heapiFy ; Just swapping if the element is smaller than already stored element ; Swapping the current index with its child ; Moving upward in the heap ; Defining heap sort ; Taking output of the minimum element ; Set first element as a last one ; Decrement of the size of the string ; Initializing the left and right index ; Process of heap sort If root element is minimum than its both of the child then break ; Otherwise checking that the child which one is smaller , swap them with parent element ; Swapping ; Changing the left index and right index ; Utility function ; To heapiFy ; Calling heap sort function ; Driver Code"
Solution in C#:

using System ; class GFG { static int x = - 1 ; static String [ ] heap = new String [ 1000 ] ; static void heapForm ( String k ) { x ++ ; heap [ x ] = k ; int child = x ; String tmp ; int index = x / 2 ; while ( index >= 0 ) { if ( heap [ index ] . CompareTo ( heap [ child ] ) > 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ child ] ; heap [ child ] = tmp ; child = index ; index = index / 2 ; } else { break ; } } } static void heapSort ( ) { int left1 , right1 ; while ( x >= 0 ) { String k ; k = heap [ 0 ] ; Console . Write ( k + " ▁ " ) ; heap [ 0 ] = heap [ x ] ; x = x - 1 ; String tmp ; int index = 0 ; int length = x ; left1 = 1 ; right1 = left1 + 1 ; while ( left1 <= length ) { if ( heap [ index ] . CompareTo ( heap [ left1 ] ) <= 0 && heap [ index ] . CompareTo ( heap [ right1 ] ) <= 0 ) { break ; } else { if ( heap [ left1 ] . CompareTo ( heap [ right1 ] ) < 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ left1 ] ; heap [ left1 ] = tmp ; index = left1 ; } else { tmp = heap [ index ] ; heap [ index ] = heap [ right1 ] ; heap [ right1 ] = tmp ; index = right1 ; } } left1 = 2 * left1 ; right1 = left1 + 1 ; } } } static void sort ( String [ ] k , int n ) { for ( int i = 0 ; i < n ; i ++ ) { heapForm ( k [ i ] ) ; } heapSort ( ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " } ; int n = arr . Length ; sort ( arr , n ) ; } }


"Lexicographically Kth smallest way to reach given coordinate from origin | C # Program to find Lexicographically Kth smallest way to reach given coordinate from origin ; Return ( a + b ) ! / a ! b ! ; finding ( a + b ) ! ; finding ( a + b ) ! / a ! ; finding ( a + b ) ! / b ! ; Return the Kth smallest way to reach given coordinate from origin ; if at origin ; if on y - axis ; decrement y . ; Move vertical ; recursive call to take next step . ; If on x - axis ; decrement x . ; Move horizontal . ; recursive call to take next step . ; If x + y C x is greater than K ; Move Horizontal ; recursive call to take next step . ; Move vertical ; recursive call to take next step . ; Driver Code"
Solution in C#:

using System ; class GFG { static int factorial ( int a , int b ) { int res = 1 ; for ( int i = 1 ; i <= ( a + b ) ; i ++ ) res = res * i ; for ( int i = 1 ; i <= a ; i ++ ) res = res / i ; for ( int i = 1 ; i <= b ; i ++ ) res = res / i ; return res ; } static void Ksmallest ( int x , int y , int k ) { if ( x == 0 && y == 0 ) return ; else if ( x == 0 ) { y -- ; Console . Write ( " V " ) ; Ksmallest ( x , y , k ) ; } else if ( y == 0 ) { x -- ; Console . Write ( " H " ) ; Ksmallest ( x , y , k ) ; } else { if ( factorial ( x - 1 , y ) > k ) { Console . Write ( " H " ) ; Ksmallest ( x - 1 , y , k ) ; } else { Console . Write ( " V " ) ; Ksmallest ( x , y - 1 , k - factorial ( x - 1 , y ) ) ; } } } public static void Main ( String [ ] args ) { int x = 2 , y = 2 , k = 2 ; Ksmallest ( x , y , k ) ; } }


"Lexicographically Kth | C # program for the above approach ; Function to fill dp array ; Initialize all the entries with 0 ; Update dp [ 0 ] [ 0 ] to 1 ; Traverse the dp array ; Update the value of dp [ i ] [ j ] ; Recursive function to find the Kth lexicographical smallest string ; Handle the base cases ; If there are more than or equal to K strings which start with a , then the first character is ' a ' ; Otherwise the first character of the resultant string is ' b ' ; Function to find the Kth lexicographical smallest string ; Function call to fill the dp array ; Print the resultant string ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static int MAX = 30 ; static void findNumString ( int X , int Y , int [ , ] dp ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = 0 ; } } dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i <= X ; ++ i ) { for ( int j = 0 ; j <= Y ; ++ j ) { if ( i > 0 ) { dp [ i , j ] += dp [ i - 1 , j ] ; } if ( j > 0 ) { dp [ i , j ] += dp [ i , j - 1 ] ; } } } } static string kthString ( int X , int Y , int K , int [ , ] dp ) { string x1 = " " ; string y1 = " " ; for ( int i = 0 ; i < Y ; i ++ ) { x1 += ' b ' ; } for ( int i = 0 ; i < X ; i ++ ) { y1 += ' a ' ; } if ( X == 0 ) return x1 ; if ( Y == 0 ) return y1 ; if ( K <= dp [ X - 1 , Y ] ) { return ( " a " + kthString ( X - 1 , Y , K , dp ) ) ; } else { return ( " b " + kthString ( X , Y - 1 , K - dp [ X - 1 , Y ] , dp ) ) ; } } static void kthStringUtil ( int X , int Y , int K ) { int [ , ] dp = new int [ MAX , MAX ] ; findNumString ( X , Y , dp ) ; Console . WriteLine ( kthString ( X , Y , K , dp ) ) ; } static void Main ( ) { int X = 4 ; int Y = 3 ; int K = 4 ; kthStringUtil ( X , Y , K ) ; } }


"Lexicographically all Shortest Palindromic Substrings from a given string | C # program to find Lexicographically all shortest palindromic substrings from a given string ; Function to find all lexicographically shortest palindromic substring ; Array to keep track of alphabetic characters ; Iterate to print all lexicographically shortest substring ; Driver code"
Solution in C#:

using System ; class GFG { static void shortestPalindrome ( string s ) { int [ ] abcd = new int [ 26 ] ; for ( int i = 0 ; i < s . Length ; i ++ ) abcd [ s [ i ] - 97 ] = 1 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( abcd [ i ] == 1 ) { Console . Write ( ( char ) ( i + 97 ) + " ▁ " ) ; } } } static public void Main ( string [ ] args ) { string s = " geeksforgeeks " ; shortestPalindrome ( s ) ; } }


"Lexicographically first alternate vowel and consonant string | C # implementation of lexicographically first alternate vowel and consonant String ; ' ch ' is vowel or not ; create alternate vowel and consonant String str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required lexicographically first alternate vowel and consonant String ; hash table to store frequencies of each character in ' str ' ; count vowels ; count consonants ; update frequency of ' ch ' in char_freq [ ] ; no such String can be formed ; form the vowel String ' vstr ' and consonant String ' cstr ' which contains characters in lexicographical order ; remove first character of vowel String then create alternate String with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant String then create alternate String with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating String with consonant ; start creating String with vowel ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int SIZE = 26 ; static bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) { finalStr = ( finalStr + str1 [ i ] ) + str2 [ j ] ; } return finalStr ; } static String findAltStr ( String str ) { int [ ] char_freq = new int [ SIZE ] ; int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . Length ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str [ i ] ; if ( isVowel ( ch ) ) { nv ++ ; } else { nc ++ ; } char_freq [ ch - 97 ] ++ ; } if ( Math . Abs ( nv - nc ) >= 2 ) { return " no ▁ such ▁ String " ; } for ( int i = 0 ; i < SIZE ; i ++ ) { char ch = ( char ) ( i + 97 ) ; for ( int j = 1 ; j <= char_freq [ i ] ; j ++ ) { if ( isVowel ( ch ) ) { vstr += ch ; } else { cstr += ch ; } } } if ( nv > nc ) { return ( vstr [ 0 ] + createAltStr ( cstr , vstr , 1 , nv ) ) ; } if ( nc > nv ) { return ( cstr [ 0 ] + createAltStr ( vstr , cstr , 1 , nc ) ) ; } if ( cstr [ 0 ] < vstr [ 0 ] ) { return createAltStr ( cstr , vstr , 0 , nv ) ; } return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void Main ( String [ ] args ) { String str = " aeroplane " ; Console . WriteLine ( findAltStr ( str ) ) ; } }


"Lexicographically first palindromic string | C # program to find first palindromic permutation of given string ; Function to count frequency of each char in the string . freq [ 0 ] for ' a ' , ... . , freq [ 25 ] for ' z ' ; Cases to check whether a palindr0mic string can be formed or not ; count_odd to count no of chars with odd frequency ; For even length string no odd freq character ; For odd length string one odd freq character ; Function to find odd freq char and reducing its freq by 1 returns " " if odd freq char is not present ; To find lexicographically first palindromic string . ; Assigning odd freq character if present else empty string . ; Traverse characters in increasing order ; Divide all occurrences into two halves . Note that odd character is removed by findOddAndRemoveItsFreq ( ) ; creating front string ; creating rear string ; Final palindromic string which is lexicographically first ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; static void countFreq ( string str , int [ ] freq , int len ) { for ( int i = 0 ; i < len ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } } static bool canMakePalindrome ( int [ ] freq , int len ) { int count_odd = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { count_odd ++ ; } } if ( len % 2 == 0 ) { if ( count_odd > 0 ) { return false ; } else { return true ; } } if ( count_odd != 1 ) { return false ; } return true ; } static string findOddAndRemoveItsFreq ( int [ ] freq ) { string odd_str = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { freq [ i ] -- ; odd_str = odd_str + ( char ) ( i + ' a ' ) ; return odd_str ; } } return odd_str ; } static string findPalindromicString ( string str ) { int len = str . Length ; int [ ] freq = new int [ MAX_CHAR ] ; countFreq ( str , freq , len ) ; if ( ! canMakePalindrome ( freq , len ) ) { return " No ▁ Palindromic ▁ String " ; } string odd_str = findOddAndRemoveItsFreq ( freq ) ; string front_str = " " , rear_str = " ▁ " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { String temp = " " ; if ( freq [ i ] != 0 ) { char ch = ( char ) ( i + ' a ' ) ; for ( int j = 1 ; j <= freq [ i ] / 2 ; j ++ ) { temp = temp + ch ; } front_str = front_str + temp ; rear_str = temp + rear_str ; } } return ( front_str + odd_str + rear_str ) ; } public static void Main ( ) { string str = " malayalam " ; Console . Write ( findPalindromicString ( str ) ) ; } }


"Lexicographically largest N | C # program for the above approach ; Function to find the lexicographically largest bitonic sequence of size N elements lies in the range [ low , high ] ; Store index of highest element ; If high_index > ( N - 1 ) / 2 , then remaining N / 2 elements cannot be placed in bitonic order ; If high_index <= 0 , then set high_index as 1 ; Stores the resultant sequence ; Store the high value ; Maintain strictly decreasing sequence from index high_index to 0 starting with temp ; Store the value and decrement the temp variable by 1 ; Maintain the strictly decreasing sequence from index high_index + 1 to N - 1 starting with high - 1 ; Store the value and decrement high by 1 ; Print the resultant sequence ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void LargestArray ( int N , int low , int high ) { int high_index = N - ( high - low + 1 ) ; if ( high_index > ( N - 1 ) / 2 ) { Console . Write ( " Not ▁ Possible " ) ; return ; } if ( high_index <= 0 ) high_index = 1 ; int [ ] A = new int [ N ] ; int temp = high ; for ( int i = high_index ; i >= 0 ; i -- ) { A [ i ] = temp -- ; } high -= 1 ; for ( int i = high_index + 1 ; i < N ; i ++ ) A [ i ] = high -- ; for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 5 , low = 2 , high = 6 ; LargestArray ( N , low , high ) ; } }