"Find all distinct subset ( or subsequence ) sums of an array | Set | C # program to find total sum of all distinct subset sums in O ( sum ) space . ; Function to print all th distinct sum ; Declare a boolean array of size equal to total sum of the array ; Fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; Iterate from maxSum to 1 and avoid lookup on any other row ; Do not change the dp array for j less than arr [ i ] ; If dp [ j ] is true then print ; Function to find the total sum and print the distinct sum ; find the sum of array elements ; Function to print all the distinct sum ; Driver Code"
Solution in C#:

using System ; class GFG { static void subsetSum ( int [ ] arr , int n , int maxSum ) { bool [ ] dp = new bool [ maxSum + 1 ] ; Array . Fill ( dp , false ) ; dp [ arr [ 0 ] ] = true ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = maxSum ; j >= 1 ; j -- ) { if ( arr [ i ] <= j ) { if ( arr [ i ] == j || dp [ j ] || dp [ ( j - arr [ i ] ) ] ) dp [ j ] = true ; else dp [ j ] = false ; } } } Console . Write ( 0 + " ▁ " ) ; for ( int j = 0 ; j < maxSum + 1 ; j ++ ) { if ( dp [ j ] == true ) Console . Write ( j + " ▁ " ) ; } Console . Write ( "21" ) ; } static void printDistinct ( int [ ] a , int n ) { int maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxSum += a [ i ] ; } subsetSum ( a , n , maxSum ) ; } static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; printDistinct ( arr , n ) ; } }


"Find all divisors of first N natural numbers | C # implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code"
Solution in C#:

 using System ; class GFG { static void factors ( int n ) { int i , j ; Console . Write ( "1 ▁ - - > 1
 " ) ; for ( i = 2 ; i <= n ; i ++ ) { Console . Write ( i + " ▁ - - > " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { Console . Write ( j + " , ▁ " ) ; if ( i / j != j ) Console . Write ( i / j + " , ▁ " ) ; } } Console . Write ( "
 " ) ; } } public static void Main ( ) { int n = 5 ; factors ( n ) ; } }


"Find all divisors of first N natural numbers | C # implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { static int MAX = ( int ) 1e5 ; static List < int > [ ] divisor = new List < int > [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . Add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( i + " - - > " ) ; foreach ( int divi in divisor [ i ] ) { Console . Write ( divi + " , ▁ " ) ; } Console . Write ( "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . Length ; i ++ ) divisor [ i ] = new List < int > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }


"Find all even length binary sequences with same sum of first and second half bits | Iterative | C # code implementation for above approach ; Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to print sequence ; Creating the array ; Looping over powers of 2 ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code"
Solution in C#:

using System ; class GFG { static void convertToBinary ( int num , int [ ] a , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } static void checkforsum ( int [ ] a , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] ) ; Console . WriteLine ( ) ; } } static void print_seq ( int m ) { int n = ( 2 * m ) ; int [ ] a = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) Math . Pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } public static void Main ( String [ ] args ) { int m = 2 ; print_seq ( m ) ; } }


"Find all factorial numbers less than or equal to n | C # program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code"
Solution in C#:

using System ; class GFG { static void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { Console . Write ( fact + " ▁ " ) ; fact = fact * x ; x ++ ; } } public static void Main ( ) { int n = 100 ; printFactorialNums ( n ) ; } }


"Find all good indices in the given Array | C # program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void niceIndices ( int [ ] A , int n ) { int sum = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( A [ i ] ) ) { var val = mp [ A [ i ] ] ; mp . Remove ( A [ i ] ) ; mp . Add ( A [ i ] , val + 1 ) ; sum += A [ i ] ; } else { mp . Add ( A [ i ] , 0 ) ; sum += A [ i ] ; } } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( mp . ContainsKey ( k ) ) { if ( ( A [ i ] == k && mp [ k ] > 1 ) || ( A [ i ] != k ) ) Console . Write ( ( i + 1 ) + " ▁ " ) ; } } } } public static void Main ( String [ ] args ) { int [ ] A = { 8 , 3 , 5 , 2 } ; int n = A . Length ; niceIndices ( A , n ) ; } }


"Find all matrix elements which are minimum in their row and maximum in their column | C # program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver code ; Function call ; If no such matrix element is found"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static List < int > minmaxNumbers ( int [ , ] matrix , List < int > res ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < matrix . GetLength ( 0 ) ; i ++ ) { int minr = int . MaxValue ; for ( int j = 0 ; j < matrix . GetLength ( 1 ) ; j ++ ) { minr = Math . Min ( minr , matrix [ i , j ] ) ; } set . Add ( minr ) ; } for ( int j = 0 ; j < matrix . GetLength ( 0 ) ; j ++ ) { int maxc = int . MinValue ; for ( int i = 0 ; i < matrix . GetLength ( 1 ) ; i ++ ) { maxc = Math . Max ( maxc , matrix [ i , j ] ) ; } if ( set . Contains ( maxc ) ) { res . Add ( maxc ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; List < int > ans = new List < int > ( ) ; ans = minmaxNumbers ( mat , ans ) ; if ( ans . Count == 0 ) Console . WriteLine ( " - 1" ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . WriteLine ( ans [ i ] ) ; } }


"Find all missing numbers from a given sorted array | C # program for the above approach ; Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Driver Code ; Given array [ ] arr ; Function call"
Solution in C#:

using System ; class GFG { static void printMissingElements ( int [ ] arr , int N ) { int [ ] b = new int [ arr [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { Console . Write ( i + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . Length ; printMissingElements ( arr , N ) ; } }


"Find all missing numbers from a given sorted array | C # program for the above approach ; Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Driver code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; class GFG { static void printMissingElements ( int [ ] arr , int N ) { int diff = arr [ 0 ] - 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { Console . Write ( i + diff + " ▁ " ) ; diff ++ ; } } } } static void Main ( ) { int [ ] arr = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . Length ; printMissingElements ( arr , N ) ; } }


"Find all numbers in range [ 1 , N ] that are not present in given Array | C # program for above approach ; Function to find the missing numbers ; traverse the array arr [ ] ; Update ; Traverse the array arr [ ] ; If Num is not present ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void getMissingNumbers ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ Math . Abs ( arr [ i ] ) - 1 ] = - ( Math . Abs ( arr [ Math . Abs ( arr [ i ] ) - 1 ] ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) Console . Write ( i + 1 + " ▁ " ) ; } } public static void Main ( ) { int N = 5 ; int [ ] arr = { 5 , 5 , 4 , 4 , 2 } ; getMissingNumbers ( arr , N ) ; } }


"Find all occurrences of a given word in a matrix | C # Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D bool matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver code"
Solution in C#:

 using System ; class GFG { static readonly int ROW = 3 ; static readonly int COL = 5 ; static bool isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } static int [ ] rowNum = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int [ ] colNum = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static void DFS ( char [ , ] mat , int row , int col , int prevRow , int prevCol , char [ ] word , String path , int index , int n ) { if ( index > n mat [ row , col ] != word [ index ] ) return ; path += ( word [ index ] ) + " ( " + String . Join ( " " , row ) + " , ▁ " + String . Join ( " " , col ) + " ) ▁ " ; if ( index == n ) { Console . Write ( path + "
 " ) ; return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } static void findWords ( char [ , ] mat , char [ ] word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i , j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } public static void Main ( String [ ] args ) { char [ , ] mat = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char [ ] word = " DES " . ToCharArray ( ) ; findWords ( mat , word , word . Length - 1 ) ; } }


"Find all pairs ( a , b ) in an array such that a % b = k | C # implementation to find such pair ; method to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver method"
Solution in C#:

using System ; public class GFG { static bool printPairs ( int [ ] arr , int n , int k ) { bool isPairFound = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j && arr [ i ] % arr [ j ] == k ) { Console . Write ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " ▁ " ) ; isPairFound = true ; } } } return isPairFound ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 4 , 7 } ; int k = 3 ; if ( printPairs ( arr , arr . Length , k ) == false ) Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } }


"Find all pairs ( a , b ) in an array such that a % b = k | C # program to find all pairs such that a % b = k . ; Utility method to find the divisors of n and store in vector v [ ] ; Vector is used to store the divisors ; If n is a square number , push only one occurrence ; method to find pairs such that ( a % b = k ) ; Store all the elements in the map to use map as hash for finding elements in O ( 1 ) time . ; Print all the pairs with ( a , b ) as ( k , numbers greater than k ) as k % ( num ( > k ) ) = k i . e . 2 % 4 = 2 ; Now check for the current element as ' a ' how many b exists such that a % b = k ; find all the divisors of ( arr [ i ] - k ) ; Check for each divisor i . e . arr [ i ] % b = k or not , if yes then print that pair . ; Clear vector ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static List < int > findDivisors ( int n ) { List < int > v = new List < int > ( ) ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { v . Add ( i ) ; } else { v . Add ( i ) ; v . Add ( n / i ) ; } } } return v ; } public static bool printPairs ( int [ ] arr , int n , int k ) { Dictionary < int , bool > occ = new Dictionary < int , bool > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { occ [ arr [ i ] ] = true ; } bool isPairFound = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( occ [ k ] && k < arr [ i ] ) { Console . Write ( " ( " + k + " , ▁ " + arr [ i ] + " ) ▁ " ) ; isPairFound = true ; } if ( arr [ i ] >= k ) { List < int > v = findDivisors ( arr [ i ] - k ) ; for ( int j = 0 ; j < v . Count ; j ++ ) { if ( arr [ i ] % v [ j ] == k && arr [ i ] != v [ j ] && occ [ v [ j ] ] ) { Console . Write ( " ( " + arr [ i ] + " , ▁ " + v [ j ] + " ) ▁ " ) ; isPairFound = true ; } } v . Clear ( ) ; } } return isPairFound ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 3 , 1 , 2 , 5 , 4 } ; int k = 2 ; if ( printPairs ( arr , arr . Length , k ) == false ) { Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } } }


"Find all pairs raised to power K differs by exactly N | C # program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code"
Solution in C#:

using System ; class GFG { static void ValidPairs ( int X , int K ) { int count = 0 ; for ( int A = - 1000 ; A <= 1000 ; A ++ ) { for ( int B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . Pow ( A , K ) - Math . Pow ( B , K ) == X ) { count ++ ; Console . WriteLine ( A + " ▁ " + B ) ; } } } if ( count == 0 ) { Console . WriteLine ( " - 1" ) ; } } static public void Main ( ) { int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; } }


"Find all palindrome numbers of given digits | C # implementation of the approach ; Function to return the reverse of num ; Function that returns true if num is palindrome ; If the number is equal to the reverse of it then it is a palindrome ; Function to print all the d - digit palindrome numbers ; Smallest and the largest d - digit numbers ; Starting from the smallest d - digit number till the largest ; If the current number is palindrome ; Driver code"
Solution in C#:

using System ; class GFG { static int reverse ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num = num / 10 ; } return rev ; } static bool isPalindrome ( int num ) { if ( num == reverse ( num ) ) return true ; return false ; } static void printPalindromes ( int d ) { if ( d <= 0 ) return ; int smallest = ( int ) Math . Pow ( 10 , d - 1 ) ; int largest = ( int ) Math . Pow ( 10 , d ) - 1 ; for ( int i = smallest ; i <= largest ; i ++ ) { if ( isPalindrome ( i ) ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int d = 2 ; printPalindromes ( d ) ; } }


"Find all possible binary trees with given Inorder Traversal | C # program to find binary tree with given inorder traversal ; Class containing left and right child of current node and key value ; Class to print Level Order Traversal ; A utility function to do preorder traversal of BST ; Function for constructing all possible trees with given inorder traversal stored in an array from arr [ start ] to arr [ end ] . This function returns a vector of trees . ; List to store all possible trees ; if start > end then subtree will be empty so returning NULL in the list ; Iterating through all values from start to end for constructing left and right subtree recursively ; Constructing left subtree ; Constructing right subtree ; Now looping through all left and right subtrees and connecting them to ith root below ; Making arr [ i ] as root ; Connecting left subtree ; Connecting right subtree ; Adding this tree to list ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class GFG { public Node root ; public virtual void preOrder ( Node node ) { if ( node != null ) { Console . Write ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } } public virtual List < Node > getTrees ( int [ ] arr , int start , int end ) { List < Node > trees = new List < Node > ( ) ; if ( start > end ) { trees . Add ( null ) ; return trees ; } for ( int i = start ; i <= end ; i ++ ) { List < Node > ltrees = getTrees ( arr , start , i - 1 ) ; List < Node > rtrees = getTrees ( arr , i + 1 , end ) ; for ( int j = 0 ; j < ltrees . Count ; j ++ ) { for ( int k = 0 ; k < rtrees . Count ; k ++ ) { Node node = new Node ( arr [ i ] ) ; node . left = ltrees [ j ] ; node . right = rtrees [ k ] ; trees . Add ( node ) ; } } } return trees ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 4 , 5 , 7 } ; int n = arr . Length ; GFG tree = new GFG ( ) ; List < Node > trees = tree . getTrees ( arr , 0 , n - 1 ) ; Console . WriteLine ( " Preorder ▁ traversal ▁ of ▁ different ▁ " + " ▁ binary ▁ trees ▁ are : " ) ; for ( int i = 0 ; i < trees . Count ; i ++ ) { tree . preOrder ( trees [ i ] ) ; Console . WriteLine ( " " ) ; } } }


"Find all possible coordinates of parallelogram | C # program to all possible points of a parallelogram ; Driver code ; coordinates of A ; coordinates of B ; coordinates of C"
Solution in C#:

using System ; public class ParallelogramPoints { public static void Main ( ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; Console . WriteLine ( ax + bx - cx + " , ▁ " + ( ay + by - cy ) ) ; Console . WriteLine ( ax + cx - bx + " , ▁ " + ( ay + cy - by ) ) ; Console . WriteLine ( cx + bx - ax + " , ▁ " + ( cy + by - ax ) ) ; } }


"Find all possible pairs with given Bitwise OR and Bitwise XOR values | C # code for the above approach ; Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver code"
Solution in C#:

using System ; class GFG { static void findPairs ( int A , int B ) { for ( int i = 1 ; i <= B ; i ++ ) { int y = A ^ i ; if ( y > 0 && ( i y ) == B ) { Console . WriteLine ( i + " ▁ " + y ) ; } } } static void Main ( ) { int A = 8 , B = 10 ; findPairs ( A , B ) ; } }


"Find all possible subarrays having product less than or equal to K | C # Program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static List < List < int > > maxSubArray ( int [ ] arr , int K ) { List < List < int > > solution = new List < List < int > > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . Length <= 1 K < 0 ) { return new List < List < int > > ( ) ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < int > list = new List < int > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . Insert ( 0 , arr [ j ] ) ; solution . Add ( new List < int > ( list ) ) ; } } return solution ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; List < List < int > > list = maxSubArray ( arr , K ) ; foreach ( List < int > i in list ) { Console . Write ( " [ " ) ; foreach ( int j in i ) { Console . Write ( j ) ; if ( i . Count > 1 ) Console . Write ( " , " ) ; } Console . Write ( " ] " ) ; } } }


"Find all possible values of K such that the sum of first N numbers starting from K is G | C # program for the above approach ; Function to find the count the value of K such that sum of the first N numbers from K is G ; Stores the total count of K ; Iterate till square root of g ; If the number is factor of g ; If the second factor is not equal to first factor ; Check if two factors are odd or not ; If second factor is the same as the first factor then check if the first factor is odd or not ; Print the resultant count ; Driver code"
Solution in C#:

using System ; class GFG { static void findValuesOfK ( int g ) { int count = 0 ; for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { if ( i != g / i ) { if ( i % 2 == 1 ) { count ++ ; } if ( ( g / i ) % 2 == 1 ) { count ++ ; } } else if ( i % 2 == 1 ) { count ++ ; } } } Console . WriteLine ( count ) ; } public static void Main ( ) { int G = 125 ; findValuesOfK ( G ) ; } }


"Find all possible ways to Split the given string into Primes | C # program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool [ ] primes = new bool [ 1000001 ] ; static int maxn = 1000000 ; static void sieve ( ) { for ( int i = 0 ; i < primes . Length ; i ++ ) { primes [ i ] = true ; } primes [ 0 ] = false ; primes [ 1 ] = false ; for ( int i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( int j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } static String toBinary ( int n ) { String r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? "0" : "1" ) + r ; n /= 2 ; } return ( r == " " ) ? "0" : r ; } static void PrimeSplit ( String str ) { String temp ; List < String > ans = new List < String > ( ) ; int bt = 1 << ( str . Length - 1 ) ; int n = str . Length ; for ( int i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + "0" ; int j = 0 , x = n - temp . Length , y ; while ( j < x ) { temp = "0" + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; String sp = " " , tp = " " ; bool flag = false ; while ( j < n ) { sp += str [ j ] ; if ( temp [ j ] == '1' ) { tp += sp + ' , ' ; y = Int32 . Parse ( sp ) ; if ( ! primes [ y ] ) { flag = true ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = Int32 . Parse ( sp ) ; if ( ! primes [ y ] ) flag = true ; } if ( ! flag ) ans . Add ( tp ) ; } if ( ans . Count == 0 ) { Console . WriteLine ( - 1 ) ; } foreach ( String i in ans ) { Console . WriteLine ( i ) ; } } public static void Main ( String [ ] args ) { String str = "11373" ; sieve ( ) ; PrimeSplit ( str ) ; } }


"Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | C # program to get right angle triangle , given hypotenuse and area of triangle ; limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver code to test above methods"
Solution in C#:

using System ; public class GFG { static double eps = ( double ) 1e-6 ; static double getArea ( double base1 , double hypotenuse ) { double height = Math . Sqrt ( hypotenuse * hypotenuse - base1 * base1 ) ; return 0.5 * base1 * height ; } static void printRightAngleTriangle ( int hypotenuse , int area ) { int hsquare = hypotenuse * hypotenuse ; double sideForMaxArea = Math . Sqrt ( hsquare / 2.0 ) ; double maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { Console . Write ( " Not ▁ possible " ) ; return ; } double low = 0.0 ; double high = sideForMaxArea ; double base1 = 0 ; while ( Math . Abs ( high - low ) > eps ) { base1 = ( low + high ) / 2.0 ; if ( getArea ( base1 , hypotenuse ) >= area ) { high = base1 ; } else { low = base1 ; } } double height = Math . Sqrt ( hsquare - base1 * base1 ) ; Console . WriteLine ( Math . Round ( base1 ) + " ▁ " + Math . Round ( height ) ) ; } static public void Main ( ) { int hypotenuse = 5 ; int area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ; } }


"Find all the pairs with given sum in a BST | Set 2 | C # program to implement the above approach ; A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { public class Node { public int data ; public Node left , right , root ; public Node ( int data ) { this . data = data ; } } public static Node AddNode ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } public static void TargetPair ( Node node , int tar ) { List < Node > LeftList = new List < Node > ( ) ; List < Node > RightList = new List < Node > ( ) ; Node curr_left = node ; Node curr_right = node ; while ( curr_left != null curr_right != null LeftList . Count > 0 && RightList . Count > 0 ) { while ( curr_left != null ) { LeftList . Add ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . Add ( curr_right ) ; curr_right = curr_right . right ; } Node LeftNode = LeftList [ LeftList . Count - 1 ] ; Node RightNode = RightList [ RightList . Count - 1 ] ; int leftVal = LeftNode . data ; int rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . RemoveAt ( RightList . Count - 1 ) ; curr_right = RightNode . left ; } else { Console . WriteLine ( LeftNode . data + " ▁ " + RightNode . data ) ; RightList . RemoveAt ( RightList . Count - 1 ) ; LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } public static void Main ( String [ ] b ) { Node root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; } }


"Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | C # Code to count 1 ( 0 + ) 1 patterns in a string ; Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ ▁ got ▁ a ▁ ' 1 ',counter incremented ; Last character stored ; Driver Code"
Solution in C#:

using System ; class GFG { static int patternCount ( String str ) { char last = str [ 0 ] ; int i = 1 , counter = 0 ; while ( i < str . Length ) { if ( str [ i ] == '0' && last == '1' ) { while ( str [ i ] == '0' ) i ++ ; if ( str [ i ] == '1' ) counter ++ ; } last = str [ i ] ; i ++ ; } return counter ; } public static void Main ( ) { String str = "1001ab010abc01001" ; Console . Write ( patternCount ( str ) ) ; } }


"Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | C # implementation of the approach ; Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findRemainders ( long n ) { List < long > vc = new List < long > ( ) ; for ( long i = 1 ; i <= Math . Ceiling ( Math . Sqrt ( n ) ) ; i ++ ) vc . Add ( n / i ) ; for ( long i = ( long ) ( n / Math . Ceiling ( Math . Sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . Add ( i ) ; vc . Reverse ( ) ; foreach ( long it in vc ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }


"Find amount of water wasted after filling the tank | C # program to find the volume of water wasted ; Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code"
Solution in C#:

using System ; class GFG { static double wastedWater ( double V , double M , double N ) { double wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } public static void Main ( ) { double V , M , N ; V = 700 ; M = 10 ; N = 3 ; Console . WriteLine ( wastedWater ( V , M , N ) ) ; V = 1000 ; M = 100 ; N = 50 ; Console . WriteLine ( wastedWater ( V , M , N ) ) ; } }


"Find amount to be added to achieve target ratio in a given mixture | C # program to find amount of water to be added to achieve given target ratio . ; Driver code"
Solution in C#:

using System ; class GFG { public static double findAmount ( double X , double W , double Y ) { return ( X * ( Y - W ) ) / ( 100 - Y ) ; } public static void Main ( ) { double X = 100 , W = 50 , Y = 60 ; Console . WriteLine ( " Water ▁ to ▁ be ▁ added ▁ = ▁ { 0 } " , findAmount ( X , W , Y ) ) ; } }


"Find an N x N grid whose xor of every row and column is equal | C # implementation of the approach ; Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code"
Solution in C#:

using System ; class GFG { static void findGrid ( int n ) { int [ , ] arr = new int [ n , n ] ; int x = 0 ; for ( int i = 0 ; i < n / 4 ; i ++ ) { for ( int j = 0 ; j < n / 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { for ( int l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k , j * 4 + l ] = x ; x ++ ; } } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( " ▁ " ) ; } } public static void Main ( String [ ] args ) { int n = 4 ; findGrid ( n ) ; } }


"Find an N | C # implementation of the above approach ; Function to print the required permutation ; Driver code"
Solution in C#:

using System ; class GFG { static void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; } public static void Main ( ) { int N = 5 ; findPermutation ( N ) ; } }


"Find an N | C # program for the above approach ; Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printBinaryString ( int [ , ] arr , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) { Console . Write ( 0 ) ; } else { Console . Write ( 1 ) ; } } } public static void Main ( ) { int N = 5 ; int [ , ] arr = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; } }


"Find an anagram of given String having different characters at corresponding indices | C # program for the above approach ; Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findAnagram ( string s ) { string check = s ; int i = 0 , j = s . Length - 1 ; while ( i < s . Length && j >= 0 ) { if ( s [ i ] != s [ j ] && check [ i ] != s [ j ] && check [ j ] != s [ i ] ) { char temp = s [ i ] ; s = s . Substring ( 0 , i ) + s [ j ] + s . Substring ( i + 1 ) ; s = s . Substring ( 0 , j ) + temp + s . Substring ( j + 1 ) ; i ++ ; j = s . Length - 1 ; } else { j -- ; } } if ( s . Length % 2 != 0 ) { int mid = s . Length / 2 ; if ( check [ mid ] == s [ mid ] ) { for ( i = 0 ; i < s . Length ; i ++ ) { if ( check [ i ] != s [ mid ] && s [ i ] != s [ mid ] ) { char temp = s [ i ] ; s = s . Substring ( 0 , i ) + s [ mid ] + s . Substring ( i + 1 ) ; s = s . Substring ( 0 , mid ) + temp + s . Substring ( mid + 1 ) ; break ; } } } } bool ok = true ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( check [ i ] == s [ i ] ) { ok = false ; break ; } } if ( ok ) Console . Write ( s ) ; else Console . Write ( - 1 ) ; } public static void Main ( ) { string S = " geek " ; findAnagram ( S ) ; } }


"Find an array element such that all elements are divisible by it | C # Program to find the smallest number that divides all numbers in an array ; function to find the smallest element ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code"
Solution in C#:

using System ; class GFG { static int min_element ( int [ ] a ) { int min = int . MaxValue ; int i ; for ( i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } static int findSmallest ( int [ ] a , int n ) { int smallest = min_element ( a ) ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } public static void Main ( ) { int [ ] a = { 25 , 20 , 5 , 10 , 100 } ; int n = a . Length ; Console . WriteLine ( findSmallest ( a , n ) ) ; } }


"Find an array element such that all elements are divisible by it | C # program to find an array element that divides all numbers in the array using naive approach ; function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; Driver code"
Solution in C#:

using System ; class GFG { static int findSmallest ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void Main ( ) { int [ ] a = { 25 , 20 , 5 , 10 , 100 } ; int n = a . Length ; Console . WriteLine ( findSmallest ( a , n ) ) ; } }


"Find an array of size N having exactly K subarrays with sum S | C # program to find array with K subarrays with sum S ; Function to find array with K subarrays with sum S ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) Console . Write ( s + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) Console . Write ( s + 1 + " ▁ " ) ; } public static void Main ( String [ ] args ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; } }


"Find an array of size N that satisfies the given conditions | C # implementation of the approach ; Utility function to print the contents of an array ; Function to generate and print the required array ; Initially all the positions are empty ; To store the count of positions i such that arr [ i ] = s ; To store the final array elements ; Set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; Mark the i 'th position as visited  as we put arr[i] = s ; Increment the count ; Finding the next odd number after s ; If the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; Print the final array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void findArray ( int n , int k , int s ) { int [ ] vis = new int [ n ] ; int cnt = 0 ; int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n && cnt < k ; i += 2 ) { arr [ i ] = s ; vis [ i ] = 1 ; cnt ++ ; } int val = s ; if ( s % 2 == 0 ) val ++ ; else val = val + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { arr [ i ] = val ; } } printArr ( arr , n ) ; } public static void Main ( ) { int n = 8 , k = 3 , s = 12 ; findArray ( n , k , s ) ; } }


"Find an equal point in a string of brackets | C # program to find an index k which decides the number of opening brackets is equal to the number of closing brackets ; Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; check if there is no opening or closing brackets ; check if there is any index at which both brackets are equal ; Driver Code"
Solution in C#:

using System ; class GFG { static int findIndex ( string str ) { int len = str . Length ; int [ ] open = new int [ len + 1 ] ; int [ ] close = new int [ len + 1 ] ; int index = - 1 ; open [ 0 ] = 0 ; close [ len ] = 0 ; if ( str [ 0 ] == ' ( ' ) open [ 1 ] = 1 ; if ( str [ len - 1 ] == ' ) ' ) close [ len - 1 ] = 1 ; for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == ' ( ' ) open [ i + 1 ] = open [ i ] + 1 ; else open [ i + 1 ] = open [ i ] ; } for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ) ' ) close [ i ] = close [ i + 1 ] + 1 ; else close [ i ] = close [ i + 1 ] ; } if ( open [ len ] == 0 ) return len ; if ( close [ 0 ] == 0 ) return 0 ; for ( int i = 0 ; i <= len ; i ++ ) if ( open [ i ] == close [ i ] ) index = i ; return index ; } public static void Main ( ) { string str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) " ; Console . Write ( findIndex ( str ) ) ; } }


"Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { public static void solve ( int [ ] Array , int N ) { double [ ] Arraynew = new double [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . Log ( Array [ i ] ) ; } double [ ] prefixsum = new double [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = Math . Abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = Math . Abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } Console . WriteLine ( " Index ▁ is : ▁ " + answer ) ; } public static void Main ( string [ ] args ) { int [ ] Array = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; } }


"Find an index such that difference between product of elements before and after it is minimum | C # implementation of the approach ; Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code"
Solution in C#:

using System ; class GFG { static int findIndex ( int [ ] a , int n ) { int res = 0 ; long min_diff = Int64 . MaxValue ; long [ ] prod = new long [ n ] ; prod [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long curr_diff = Math . Abs ( ( prod [ n - 1 ] / prod [ i ] ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 5 , 7 , 2 , 9 } ; int N = arr . Length ; Console . WriteLine ( findIndex ( arr , N ) ) ; } }


"Find an integer in the given range that satisfies the given conditions | C # implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 10 ; static bool distinctDigits ( int x ) { bool [ ] present = new bool [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = int . MinValue ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void Main ( String [ ] args ) { int l = 50 , r = 60 ; Console . Write ( findK ( l , r ) ) ; } }


"Find an integer that is common in the maximum number of given arithmetic progressions | C # implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code"
Solution in C#:

using System ; class GFG { static int MAXN = 1000000 ; static int max_element ( int [ ] A , int n ) { int max = A [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] > max ) max = A [ i ] ; return max ; } static int maxCommonElement ( int [ ] A , int [ ] D , int N ) { int [ ] cnt = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } int ans = 0 ; int com = 0 ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } public static void Main ( ) { int [ ] A = { 13 , 1 , 2 , 5 } ; int [ ] D = { 5 , 10 , 1 , 12 } ; int N = A . Length ; Console . WriteLine ( maxCommonElement ( A , D , N ) ) ; } }


"Find an integral solution of the non | C # program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code"
Solution in C#:

using System ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( ( N & 1 ) != 0 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void findValX_Y ( int N ) { if ( N <= 1 ) { Console . WriteLine ( - 1 ) ; return ; } int xMax ; xMax = ( int ) Math . Log ( N ) ; int yMax ; yMax = ( int ) ( Math . Log ( N ) / Math . Log ( 5.0 ) ) ; for ( int i = 1 ; i <= xMax ; i ++ ) { for ( int j = 1 ; j <= yMax ; j ++ ) { int a = power ( 2 , i ) ; int b = power ( 5 , j ) ; if ( a + b == N ) { Console . Write ( i + " ▁ " + j ) ; return ; } } } Console . WriteLine ( " - 1" ) ; } public static void Main ( ) { int N = 129 ; findValX_Y ( N ) ; } }


"Find any K distinct odd integers such that their sum is equal to N | C # implementation to find k odd integers such that their sum is N ; Function to find K odd integers such that their sum is N ; Condition to check if there exist such K integers ; Loop to find first K - 1 distinct odd integers ; Final Kth odd number ; Driver code"
Solution in C#:

using System ; class GFG { static void oddints ( int n , int k ) { if ( n % 2 != k % 2 ) { Console . WriteLine ( " - 1" ) ; return ; } int sum = 0 ; int i = 1 ; int j = 1 ; while ( j < k ) { sum = sum + i ; Console . Write ( i + " ▁ " ) ; i = i + 2 ; j ++ ; } int finalOdd = n - sum ; Console . WriteLine ( finalOdd ) ; } public static void Main ( String [ ] args ) { int n = 10 ; int k = 2 ; oddints ( n , k ) ; } }


"Find any one of the multiple repeating elements in read only array | C # program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver code ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . Sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . Add ( arr [ i ] , 1 ) ; if ( m [ arr [ i ] ] == 1 ) return arr [ i ] ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; Console . WriteLine ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }


"Find any pair with given GCD and LCM | C # program to print any pair with a given gcd G and lcm L ; Function to print the pairs ; Driver Code"
Solution in C#:

using System ; class GFG { static void printPair ( int g , int l ) { Console . Write ( g + " ▁ " + l ) ; } public static void Main ( ) { int g = 3 , l = 12 ; printPair ( g , l ) ; } }


"Find any permutation of Binary String of given size not present in Array | C # Program for the above approach ; Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code"
Solution in C#:

using System ; class GFG { static string findString ( string [ ] arr , int N ) { string ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] [ i ] == '0' ? '1' : '0' ; } return ans ; } public static void Main ( String [ ] args ) { string [ ] arr = { "111" , "011" , "001" } ; int N = arr . Length ; Console . WriteLine ( findString ( arr , N ) ) ; } }


"Find any two pairs ( a , b ) and ( c , d ) such that a d | C # implementation to sort the array of points by its distance from the given point ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver code"
Solution in C#:

using System ; public class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPair ( pair [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int a = arr [ i ] . first , b = arr [ i ] . second ; for ( int j = i + 1 ; j < N ; j ++ ) { int c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { Console . WriteLine ( " ( " + a + " ▁ " + b + " ) , ▁ ( " + c + " ▁ " + d + " ) " ) ; return ; } } } Console . WriteLine ( " NO ▁ SUCH ▁ PAIR ▁ EXIST " ) ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findPair ( arr , 5 ) ; } }


"Find any two pairs ( a , b ) and ( c , d ) such that a d | C # program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code"
Solution in C#:

 using System ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static void findpair ( pair [ ] arr , int N ) { Array . Sort ( arr ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { Console . Write ( " ( " + arr [ i - 1 ] . first + " ▁ " + b + " ) , ▁ ( " + arr [ i ] . first + " ▁ " + d + " ) " ) ; return ; } } Console . Write ( " NO ▁ SUCH ▁ PAIR ▁ EXIST
 " ) ; } public static void Main ( String [ ] args ) { pair [ ] arr = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findpair ( arr , 5 ) ; } }


"Find area of parallelogram if vectors of two adjacent sides are given | C # code to calculate area of parallelogram if vectors of 2 adjacent sides are given ; Function to calculate area of parallelogram ; Driver code"
Solution in C#:

using System ; class GFG { static float area ( float x1 , float y1 , float z1 , float x2 , float y2 , float z2 ) { float area = ( float ) Math . Sqrt ( Math . Pow ( ( y1 * z2 - y2 * z1 ) , 2 ) + Math . Pow ( ( x1 * z2 - x2 * z1 ) , 2 ) + Math . Pow ( ( x1 * y2 - x2 * y1 ) , 2 ) ) ; return area ; } public static void Main ( ) { float x1 = 3 ; float y1 = 1 ; float z1 = - 2 ; float x2 = 1 ; float y2 = - 3 ; float z2 = 4 ; float a = area ( x1 , y1 , z1 , x2 , y2 , z2 ) ; Console . Write ( " Area ▁ = ▁ " + a ) ; } }


"Find area of the larger circle when radius of the smaller circle and difference in the area is given | C # implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver code"
Solution in C#:

using System ; public class GFG { static double PI = 3.14 ; static double find_area ( int r , int d ) { double R = d / PI ; R += Math . Pow ( r , 2 ) ; R = Math . Sqrt ( R ) ; double area = PI * Math . Pow ( R , 2 ) ; return area ; } static public void Main ( ) { int r = 4 , d = 5 ; Console . Write ( find_area ( r , d ) ) ; } }


"Find area of triangle if two vectors of two adjacent sides are given | C # program to calculate area of triangle if vectors of 2 adjacent sides are given ; function to calculate area of triangle ; Driver Code"
Solution in C#:

using System ; class GFG { static float area ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { double a = Math . Pow ( ( y1 * z2 - y2 * z1 ) , 2 ) + Math . Pow ( ( x1 * z2 - x2 * z1 ) , 2 ) + Math . Pow ( ( x1 * y2 - x2 * y1 ) , 2 ) ; float area = ( float ) Math . Sqrt ( a ) ; area = area / 2 ; return area ; } public static void Main ( ) { int x1 = - 2 ; int y1 = 0 ; int z1 = - 5 ; int x2 = 1 ; int y2 = - 2 ; int z2 = - 1 ; float a = area ( x1 , y1 , z1 , x2 , y2 , z2 ) ; Console . WriteLine ( " Area ▁ = ▁ " + a ) ; } }


"Find array elements with frequencies in range [ l , r ] | C # program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findElements ( int [ ] arr , int n , int l , int r ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int a = 0 ; if ( ! mp . ContainsKey ( arr [ i ] ) ) { a = 1 ; } else { a = mp [ arr [ i ] ] + 1 ; } if ( ! mp . ContainsKey ( arr [ i ] ) ) { mp . Add ( arr [ i ] , a ) ; } else { mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , a ) ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) && l <= mp [ arr [ i ] ] && ( mp [ arr [ i ] ] <= r ) ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = arr . Length ; int l = 2 , r = 3 ; findElements ( arr , n , l , r ) ; } }


"Find array elements with rightmost set bit at the position of the rightmost set bit in K | C # program for the above approach ; Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K"
Solution in C#:

using System ; public class GFG { static int findMask ( int K ) { int mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } public static void sameRightSetBitPos ( int [ ] arr , int N , int K ) { int mask = findMask ( K ) ; int pos = ( K & mask ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 3 , 4 , 6 , 7 , 9 , 12 , 15 } ; int N = arr . Length ; int K = 7 ; sameRightSetBitPos ( arr , N , K ) ; } }


"Find array sum after incrementing by K adjacent elements of every positive element M times | C # program for the above approach ; Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array [ ] arr ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code"
Solution in C#:

using System ; public class GFG { static void nearestLeft ( int [ ] arr , int N , int [ ] steps ) { int L = - N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } static void nearestRight ( int [ ] arr , int N , int [ ] steps ) { int R = 2 * N ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = Math . Min ( steps [ i ] , R - i ) ; } } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) sum += arr [ i ] ; return sum ; } static int findSum ( int [ ] arr , int N , int M , int K ) { int [ ] steps = new int [ N ] ; int sum = accumulate ( arr , 0 , N ) ; if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( int i = 0 ; i < N ; i ++ ) sum += 2 * K * Math . Max ( 0 , M - steps [ i ] ) ; return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 1 , 0 , 0 } ; int N = arr . Length ; int M = 2 ; int K = 1 ; Console . Write ( findSum ( arr , N , M , K ) ) ; } }


"Find array using different XORs of elements in groups of size 4 | C # implementation of the approach ; Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code"
Solution in C#:

using System ; class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void Main ( ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . Length ; findArray ( q , n ) ; } }


"Find array whose elements are XOR of adjacent elements in given array | C # program for the above approach ; Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print the array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to print arr [ ]"
Solution in C#:

using System ; class GFG { static int [ ] game_with_number ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } static void print ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 10 , 11 , 1 , 2 , 3 } ; int n = arr . Length ; int [ ] new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; } }


"Find array with k number of merge sort calls | C # program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code"
Solution in C#:

using System ; class GFG { static void unsort ( int l , int r , int [ ] a , int k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { Console . WriteLine ( " NO ▁ SOLUTION " ) ; return ; } int [ ] a = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }


"Find bitonic point in given bitonic sequence | C # program to find bitonic point in a bitonic array . ; Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program"
Solution in C#:

using System ; class GFG { static int binarySearch ( int [ ] arr , int left , int right ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 } ; int n = arr . Length ; int index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != - 1 ) Console . Write ( arr [ index ] ) ; } }


"Find bitwise AND ( & ) of all possible sub | C # program to find of all the sub - arrays ; function to return AND of sub - arrays ; Driver code ; size of the array ; print and of all subarrays"
Solution in C#:

using System ; public class GFG { static int AND ( int [ ] a , int n ) { int ans = a [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) ans &= a [ i ] ; return ans ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 3 } ; int n = a . Length ; Console . WriteLine ( AND ( a , n ) ) ; } }


"Find bitwise OR of all possible sub | C # program to find OR of all the sub - arrays ; function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays"
Solution in C#:

using System ; class GFG { static int OR ( int [ ] a , int n ) { int ans = a [ 0 ] ; int i ; for ( i = 1 ; i < n ; i ++ ) { ans |= a [ i ] ; } return ans ; } public static void Main ( ) { int [ ] a = { 1 , 4 , 6 } ; int n = a . Length ; Console . Write ( OR ( a , n ) ) ; } }


"Find ceil of a / b without using ceil ( ) function | C # program to find ceil ( a / b ) without using ceil ( ) function ; Driver Code ; taking input 1 ; example of perfect division taking input 2"
Solution in C#:

using System ; class GFG { static void Main ( ) { int a = 4 ; int b = 3 , val = 0 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; Console . WriteLine ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; Console . WriteLine ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }


using System ; class GFG { public static void Main ( ) { int a = 4 ; int b = 3 ; int val = ( a + b - 1 ) / b ; Console . WriteLine ( " The ▁ ceiling " + " ▁ value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; val = ( a + b - 1 ) / b ; Console . WriteLine ( " The ▁ ceiling " + " ▁ value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }


"Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | C # program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code"
Solution in C#:

using System ; class GFG { static char KthCharacter ( string s , string t , long k ) { long f = 1 ; long ss = 2 ; string tmp = " " ; int len = tmp . Length ; while ( len < k ) { long tf = f ; long ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . Length ; } char output = tmp [ ( int ) k - 1 ] ; return output ; } public static void Main ( string [ ] args ) { string S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; Console . Write ( ans ) ; } }


"Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | C # program to solve the above approach ; storing length ; variables for temporary strings of s and t ; readonly output variable ; if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k ; incrementing first by 2 , as said in problem statement ; if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k ; incrementing second by two as said in problem statement ; if k is smaller than y , then the character will be at position k % m . ; returning character at k index ; if k is smaller than x , then the character is at position k % n ; returning character at k index ; Driver code"
Solution in C#:

using System ; public class GFG { static char Kthchar ( String s , String t , long k ) { long n = s . Length ; long m = t . Length ; long first = 1 ; long second = 2 ; char output = ' ? ' ; while ( k > 0 ) { if ( k > first * n ) { long x = first * n ; k = k - x ; first = first + 2 ; if ( k > second * m ) { long y = second * m ; k = k - y ; second = second + 2 ; } else { long ind = k % m ; ind -- ; if ( ind < 0 ) ind = m - 1 ; output = t [ ( int ) ( ind ) ] ; break ; } } else { long ind = k % n ; ind -- ; if ( ind < 0 ) ind = n - 1 ; output = s [ ( int ) ( ind ) ] ; break ; } } return output ; } public static void Main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = Kthchar ( S1 , S2 , k ) ; Console . WriteLine ( ans ) ; } }


"Find closest integer with the same weight | C # implementation of the approach ; Function to return the number closest to x which has equal number of set bits as x ; Loop for each bit in x and compare with the next bit ; Driver code"
Solution in C#:

using System ; class GFG { static int NumUnsignBits = 64 ; static long findNum ( long x ) { for ( int i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } return long . MinValue ; } public static void Main ( String [ ] args ) { int n = 92 ; Console . WriteLine ( findNum ( n ) ) ; } }


"Find closest number in array | C # program to find element closet to given target . ; Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver code"
Solution in C#:

using System ; class GFG { public static int findClosest ( int [ ] arr , int target ) { int n = arr . Length ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } public static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 } ; int target = 11 ; Console . WriteLine ( findClosest ( arr , target ) ) ; } }


"Find combined mean and variance of two series | C # program to find combined mean and variance of two series . ; Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver code ; Function call to combined mean ."
Solution in C#:

using System ; class GFG { static float mean ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; float mean = ( float ) sum / n ; return mean ; } static float sd ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; float sdd = sum / n ; return sdd ; } static float combinedVariance ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { float mean1 = mean ( arr1 , n ) ; float mean2 = mean ( arr2 , m ) ; Console . Write ( " Mean1 : ▁ " + mean1 + " ▁ " ) ; Console . WriteLine ( " Mean2 : ▁ " + mean2 ) ; float sd1 = sd ( arr1 , n ) ; float sd2 = sd ( arr2 , m ) ; Console . Write ( " StandardDeviation1 : ▁ " + sd1 + " ▁ " ) ; Console . WriteLine ( " StandardDeviation2 : ▁ " + sd2 + " ▁ " ) ; float combinedMean = ( float ) ( n * mean1 + m * mean2 ) / ( n + m ) ; Console . WriteLine ( " Combined ▁ Mean : ▁ " + combinedMean + " ▁ " ) ; float d1_square = ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ; float d2_square = ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ; Console . Write ( " d1 ▁ square : ▁ " + d1_square + " ▁ " ) ; Console . WriteLine ( " d2 _ square : ▁ " + d2_square ) ; float combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; return combinedVar ; } public static void Main ( ) { int [ ] arr1 = { 23 , 45 , 34 , 78 , 12 , 76 , 34 } ; int [ ] arr2 = { 65 , 67 , 34 , 23 , 45 } ; int n = arr1 . Length ; int m = arr2 . Length ; Console . WriteLine ( " Combined ▁ Variance : ▁ " + combinedVariance ( arr1 , arr2 , n , m ) ) ; } }


"Find common elements in three sorted arrays | C # program to find common elements in three arrays ; This function prints common element s in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver code to test above"
Solution in C#:

using System ; class GFG { static void findCommon ( int [ ] ar1 , int [ ] ar2 , int [ ] ar3 ) { int i = 0 , j = 0 , k = 0 ; while ( i < ar1 . Length && j < ar2 . Length && k < ar3 . Length ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { Console . Write ( ar1 [ i ] + " ▁ " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } public static void Main ( ) { int [ ] ar1 = { 1 , 5 , 10 , 20 , 40 , 80 } ; int [ ] ar2 = { 6 , 7 , 20 , 80 , 100 } ; int [ ] ar3 = { 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 } ; Console . Write ( " Common ▁ elements ▁ are ▁ " ) ; findCommon ( ar1 , ar2 , ar3 ) ; } }


"Find consecutive 1 s of length >= n in binary representation of a number | C # implementation of above approach ; Function to count the number of leading zeros ; Function to find the string of n consecutive 1 's ; Initialize position to return . ; Skip leading 0 's ; Set position after leading 0 's ; Count first group of 1 's. ; If length of consecutive 1 's  is greater than or equal to n ; Not enough 1 's  skip over to next group ; Update the position ; if no string is found ; Driver code"
Solution in C#:

using System ; public class GFG { static int countLeadingZeros ( int x ) { int y ; int n ; n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } static int FindStringof1s ( int x , int n ) { int k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return - 1 ; } static public void Main ( ) { int x = 35 ; int n = 2 ; Console . WriteLine ( FindStringof1s ( x , n ) ) ; } }


"Find coordinates of the triangle given midpoint of each side | C # program to find coordinate of the triangle given midpoint of each side ; Return after solving the equations and finding the vertices coordinate . ; Finding sum of all three coordinate . ; Solving the equation . ; Finds vertices of a triangles from given middle vertices . ; Find X coordinates of vertices . ; Find Y coordinates of vertices . ; Output the solution . ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static final int N = 3 ; static ArrayList solve ( int [ ] v ) { ArrayList res = new ArrayList ( ) ; int all3 = v [ 0 ] + v [ 1 ] + v [ 2 ] ; res . Add ( all3 - v [ 1 ] * 2 ) ; res . Add ( all3 - v [ 2 ] * 2 ) ; res . Add ( all3 - v [ 0 ] * 2 ) ; return res ; } static void findVertex ( int [ ] xmid , int [ ] ymid ) { ArrayList V1 = solve ( xmid ) ; ArrayList V2 = solve ( ymid ) ; for ( int i = 0 ; i < 3 ; i ++ ) { Console . WriteLine ( V1 [ i ] + " ▁ " + V2 [ i ] ) ; } } public static void Main ( ) { int [ ] xmid = { 5 , 4 , 5 } ; int [ ] ymid = { 3 , 4 , 5 } ; findVertex ( xmid , ymid ) ; } }


"Find cost price from given selling price and profit or loss percentage | Csharp implementation to find Cost price ; Function to calculate cost price with profit ; required formula to calculate CP with profit ; Function to calculate cost price with loss ; required formula to calculate CP with loss ; Driver code"
Solution in C#:

using System ; class solution { static float CPwithProfit ( int sellingPrice , int profit ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 + profit ) ; return costPrice ; } static float CPwithLoss ( int sellingPrice , int loss ) { float costPrice ; costPrice = ( sellingPrice * 100 ) / ( 100 - loss ) ; return costPrice ; } public static void Main ( ) { int SP , profit , loss ; SP = 1020 ; profit = 20 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; SP = 900 ; loss = 10 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithLoss ( SP , loss ) ) ; SP = 42039 ; profit = 8 ; Console . WriteLine ( " Cost ▁ Price ▁ = ▁ " + CPwithProfit ( SP , profit ) ) ; }


"Find count of digits in a number that divide the number | C # program to count number of digits that divides the number . ; Return the number of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver method"
Solution in C#:

using System ; class GFG { static int countDigit ( int n ) { int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) count ++ ; } return count ; } public static void Main ( ) { int n = 1012 ; Console . Write ( countDigit ( n ) ) ; } }


"Find count of numbers from 0 to n which satisfies the given equation for a value K | C # implementation to find the total count of all the numbers from 0 to n which satisfies the given equation for a value K ; Function to find the values ; Calculate the LCM ; Calculate the multiples of lcm ; Find the values which satisfies the given condition ; Subtract the extra values ; Return the readonly result ; Driver code"
Solution in C#:

 using System ; class GFG { static int findAns ( int a , int b , int n ) { int lcm = ( a * b ) / __gcd ( a , b ) ; int multiples = ( n / lcm ) + 1 ; int answer = Math . Max ( a , b ) * multiples ; int lastvalue = lcm * ( n / lcm ) + Math . Max ( a , b ) ; if ( lastvalue > n ) { answer = answer - ( lastvalue - n - 1 ) ; } return answer ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int a = 1 , b = 13 , n = 500 ; Console . Write ( findAns ( a , b , n ) + "
 " ) ; } }


"Find cubic root of a number | C # program to find cubic root of a number using Binary Search ; Returns the absolute value of n - mid * mid * mid ; Returns cube root of a no . n ; Set start and end for binary search ; Set precision ; If error is less than e then mid is our answer so return mid ; If mid * mid * mid is greater than n set end = mid ; If mid * mid * mid is less than n set start = mid ; Driver Code"
Solution in C#:

using System ; class GFG { static double diff ( double n , double mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } static double cubicRoot ( double n ) { double start = 0 , end = n ; double e = 0.0000001 ; while ( true ) { double mid = ( start + end ) / 2 ; double error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } public static void Main ( ) { double n = 3 ; Console . Write ( " Cube ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + cubicRoot ( n ) ) ; } }


"Find depth of the deepest odd level leaf node | C # program to find depth of deepest odd level node ; A binary tree node ; A recursive function to find depth of the deepest odd level leaf ; Base Case ; If this node is a leaf and its level is odd , return its level ; If not leaf , return the maximum value from left and right subtrees ; Main function which calculates the depth of deepest odd level leaf . This function mainly uses depthOfOddLeafUtil ( ) ; Driver Code"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int depthOfOddLeafUtil ( Node node , int level ) { if ( node == null ) { return 0 ; } if ( node . left == null && node . right == null && ( level & 1 ) != 0 ) { return level ; } return Math . Max ( depthOfOddLeafUtil ( node . left , level + 1 ) , depthOfOddLeafUtil ( node . right , level + 1 ) ) ; } public virtual int depthOfOddLeaf ( Node node ) { int level = 1 , depth = 0 ; return depthOfOddLeafUtil ( node , level ) ; } public static void Main ( string [ ] args ) { int k = 45 ; BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . left . right = new Node ( 7 ) ; tree . root . right . right . right = new Node ( 8 ) ; tree . root . right . left . right . left = new Node ( 9 ) ; tree . root . right . right . right . right = new Node ( 10 ) ; tree . root . right . right . right . right . left = new Node ( 11 ) ; Console . WriteLine ( tree . depthOfOddLeaf ( tree . root ) + " ▁ is ▁ the ▁ required ▁ depth " ) ; } }


"Find depth of the deepest odd level leaf node | C # program to find depth of the deepest odd level leaf node of binary tree ; tree node ; returns a new tree Node ; return max odd number depth of leaf node ; create a queue for level order traversal ; traverse until the queue is empty ; traverse for complete level ; check if the node is leaf node and level is odd if level is odd , then update result ; check for left child ; check for right child ; Driver Code ; construct a tree"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { 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 int maxOddLevelDepth ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int result = int . MaxValue ; int level = 0 ; while ( q . Count != 0 ) { int size = q . Count ; level += 1 ; while ( size > 0 ) { Node temp = q . Peek ( ) ; q . Dequeue ( ) ; if ( temp . left == null && temp . right == null && ( level % 2 != 0 ) ) { result = level ; } if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; } size -= 1 ; } } return result ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; root . right . right . right . right . left = newNode ( 11 ) ; int result = maxOddLevelDepth ( root ) ; if ( result == int . MaxValue ) Console . WriteLine ( " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level " ) ; else { Console . Write ( result ) ; Console . WriteLine ( " ▁ is ▁ the ▁ required ▁ depth ▁ " ) ; } } }


"Find determinant of matrix generated by array rotation | C # program for finding determinant of generated matrix ; Function to calculate determinant ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static double calcDeterminant ( int [ ] arr ) { double determinant = 0 ; for ( int i = 0 ; i < N ; i ++ ) { determinant += Math . Pow ( arr [ i ] , 3 ) ; } determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] ; return determinant ; } static public void Main ( ) { int [ ] arr = { 4 , 5 , 3 } ; Console . WriteLine ( calcDeterminant ( arr ) ) ; } }


"Find difference between sums of two diagonals | C # Code for find difference between sums of two diagonals ; Initialize sums of diagonals ; Absolute difference of the sums across the diagonals ; Driver Code"
Solution in C#:

using System ; public class GFG { public static int difference ( int [ , ] arr , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += arr [ i , i ] ; d2 += arr [ i , n - i - 1 ] ; } return Math . Abs ( d1 - d2 ) ; } public static void Main ( ) { int n = 3 ; int [ , ] arr = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , - 12 } } ; Console . Write ( difference ( arr , n ) ) ; } }


"Find difference between sums of two diagonals | C # Code for find difference between sums of two diagonals ; Initialize sums of diagonals ; finding sum of primary diagonal ; finding sum of secondary diagonal ; Absolute difference of the sums across the diagonals ; Driver Code"
Solution in C#:

using System ; public class GFG { public static int difference ( int [ , ] arr , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) d1 += arr [ i , j ] ; if ( i == n - j - 1 ) d2 += arr [ i , j ] ; } } return Math . Abs ( d1 - d2 ) ; } public static void Main ( ) { int n = 3 ; int [ , ] arr = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , - 12 } } ; Console . Write ( difference ( arr , n ) ) ; } }


"Find distance between two nodes in the given Binary tree for Q queries | C # program to find distance between two nodes using LCA ; log2 ( MAX ) ; Array to store the level of each node ; List to store tree ; Pre - Processing to calculate values of lca [ , ] , dist [ , ] ; Using recursion formula to calculate the values of lca [ , ] ; Storing the level of each node ; Using recursion formula to calculate the values of lca [ , ] and dist [ , ] ; Function to find the distance between given nodes u and v ; The node which is present farthest from the root node is taken as v . If u is farther from root node then swap the two ; Finding the ancestor of v which is at same level as u ; Adding distance of node v till its 2 ^ i - th ancestor ; If u is the ancestor of v then u is the LCA of u and v ; Finding the node closest to the root which is not the common ancestor of u and v i . e . a node x such that x is not the common ancestor of u and v but lca [ x , 0 ] is ; Adding the distance of v and u to its 2 ^ i - th ancestor ; Adding the distance of u and v to its first ancestor ; Driver Code ; Number of nodes ; Add edges with their cost ; Initialising lca and dist values with - 1 and 0 respectively ; Perform DFS ; Query 1 : { 1 , 3 } ; Query 2 : { 2 , 3 } ; Query 3 : { 3 , 5 }"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 1000 ; static readonly int log = 10 ; static int [ ] level = new int [ MAX ] ; static int [ , ] lca = new int [ MAX , log ] ; static int [ , ] dist = new int [ MAX , log ] ; static List < List < int [ ] > > graph = new List < List < int [ ] > > ( ) ; static void addEdge ( int u , int v , int cost ) { graph [ u ] . Add ( new int [ ] { v , cost } ) ; graph [ v ] . Add ( new int [ ] { u , cost } ) ; } static void dfs ( int node , int parent , int h , int cost ) { lca [ node , 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { dist [ node , 0 ] = cost ; } for ( int i = 1 ; i < log ; i ++ ) { if ( lca [ node , i - 1 ] != - 1 ) { lca [ node , i ] = lca [ lca [ node , i - 1 ] , i - 1 ] ; dist [ node , i ] = dist [ node , i - 1 ] + dist [ lca [ node , i - 1 ] , i - 1 ] ; } } foreach ( int [ ] i in graph [ node ] ) { if ( i [ 0 ] == parent ) continue ; dfs ( i [ 0 ] , node , h + 1 , i [ 1 ] ) ; } } static void findDistance ( int u , int v ) { int ans = 0 ; if ( level [ u ] > level [ v ] ) { int temp = u ; u = v ; v = temp ; } for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v , i ] != - 1 && level [ lca [ v , i ] ] >= level [ u ] ) { ans += dist [ v , i ] ; v = lca [ v , i ] ; } } if ( v == u ) { Console . WriteLine ( ans ) ; } else { for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v , i ] != lca [ u , i ] ) { ans += dist [ u , i ] + dist [ v , i ] ; v = lca [ v , i ] ; u = lca [ u , i ] ; } } ans += dist [ u , 0 ] + dist [ v , 0 ] ; Console . WriteLine ( ans ) ; } } public static void Main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < MAX ; i ++ ) { graph . Add ( new List < int [ ] > ( ) ) ; } addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < log ; j ++ ) { lca [ i , j ] = - 1 ; dist [ i , j ] = 0 ; } } dfs ( 1 , - 1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ; } }


"Find distance from root to given node in a binary tree | C # program to find distance of a given node from root . ; A Binary Tree Node ; A utility function to create a new Binary Tree Node ; Returns - 1 if x doesn 't exist in tree. Else  returns distance of x from root ; Base case ; Initialize distance ; Check if x is present at root or in left subtree or right subtree . ; Driver code"
Solution in C#:

using System ; class GfG { class Node { public int data ; public Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int findDistance ( Node root , int x ) { if ( root == null ) return - 1 ; int dist = - 1 ; if ( ( root . data == x ) || ( dist = findDistance ( root . left , x ) ) >= 0 || ( dist = findDistance ( root . right , x ) ) >= 0 ) return dist + 1 ; return dist ; } public static void Main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 15 ) ; root . left . left = newNode ( 20 ) ; root . left . right = newNode ( 25 ) ; root . left . right . right = newNode ( 45 ) ; root . right . left = newNode ( 30 ) ; root . right . right = newNode ( 35 ) ; Console . WriteLine ( findDistance ( root , 45 ) ) ; } }


"Find distinct characters in distinct substrings of a string | C # implementation of the approach ; Function to return the count of distinct characters in all the distinct sub - strings of the given string ; To store all the sub - strings ; To store the current sub - string ; To store the characters of the current sub - string ; If current sub - string hasn 't  been stored before ; Insert it into the set ; Update the count of distinct characters ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class geeks { public static int countTotalDistinct ( String str ) { int cnt = 0 ; HashSet < String > items = new HashSet < String > ( ) ; for ( int i = 0 ; i < str . Length ; ++ i ) { String temp = " " ; HashSet < char > ans = new HashSet < char > ( ) ; for ( int j = i ; j < str . Length ; ++ j ) { temp = temp + str [ j ] ; ans . Add ( str [ j ] ) ; if ( ! items . Contains ( temp ) ) { items . Add ( temp ) ; cnt += ans . Count ; } } } return cnt ; } public static void Main ( String [ ] args ) { String str = " ABCA " ; Console . WriteLine ( countTotalDistinct ( str ) ) ; } }


"Find distinct integers for a triplet with given product | C # implementation of the approach ; Function to find the required triplets ; To store the factors ; Find factors in Math . Sqrt ( x ) time ; Choose a factor ; Choose another factor ; These conditions need to be met for a valid triplet ; Print the valid triplet ; Triplet found ; Triplet not found ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findTriplets ( int x ) { List < int > fact = new List < int > ( ) ; HashSet < int > factors = new HashSet < int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . Add ( i ) ; if ( x / i != i ) fact . Add ( x / i ) ; factors . Add ( i ) ; factors . Add ( x / i ) ; } } bool found = false ; int k = fact . Count ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact [ i ] ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact [ j ] ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { Console . Write ( a + " ▁ " + b + " ▁ " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int x = 105 ; findTriplets ( x ) ; } }


"Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | C # code to find the repeated elements in the array where every other is present once ; Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver Code"
Solution in C#:

using System ; class GFG { public static int findDuplicate ( int [ ] arr ) { int slow = arr [ 0 ] ; int fast = arr [ 0 ] ; do { slow = arr [ slow ] ; fast = arr [ arr [ fast ] ] ; } while ( slow != fast ) ; int ptr1 = arr [ 0 ] ; int ptr2 = slow ; while ( ptr1 != ptr2 ) { ptr1 = arr [ ptr1 ] ; ptr2 = arr [ ptr2 ] ; } return ptr1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 1 } ; Console . WriteLine ( " " + findDuplicate ( arr ) ) ; } }


"Find duplicates in a given array when elements are not limited to a range | C # implementation of the above approach ; Function to find the Duplicates , if duplicate occurs 2 times or more than 2 times in array so , it will print duplicate value only once at output ; Initialize ifPresent as false ; ArrayList to store the output ; Checking if element is present in the ArrayList or not if present then break ; If element is not present in the ArrayList then add it to ArrayList and make ifPresent at true ; If duplicates is present then print ArrayList ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findDuplicates ( int [ ] arr , int len ) { bool ifPresent = false ; List < int > al = new List < int > ( ) ; for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { if ( al . Contains ( arr [ i ] ) ) { break ; } else { al . Add ( arr [ i ] ) ; ifPresent = true ; } } } } if ( ifPresent == true ) { Console . Write ( " [ " + al [ 0 ] + " , ▁ " ) ; for ( int i = 1 ; i < al . Count - 1 ; i ++ ) { Console . Write ( al [ i ] + " , ▁ " ) ; } Console . Write ( al [ al . Count - 1 ] + " ] " ) ; } else { Console . Write ( " No ▁ duplicates ▁ present ▁ in ▁ arrays " ) ; } } static void Main ( ) { int [ ] arr = { 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 } ; int n = arr . Length ; findDuplicates ( arr , n ) ; } }


"Find element at given index after a number of rotations | C # code to rotate an array and answer the index query ; Function to compute the element at given index ; Range [ left ... right ] ; Rotation will not have any effect ; Returning new element ; Driver Code ; No . of rotations ; Ranges according to 0 - based indexing"
Solution in C#:

using System ; class GFG { static int findElement ( int [ ] arr , int [ , ] ranges , int rotations , int index ) { for ( int i = rotations - 1 ; i >= 0 ; i -- ) { int left = ranges [ i , 0 ] ; int right = ranges [ i , 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int rotations = 2 ; int [ , ] ranges = { { 0 , 2 } , { 0 , 3 } } ; int index = 1 ; Console . Write ( findElement ( arr , ranges , rotations , index ) ) ; } }


"Find element in a sorted array whose frequency is greater than or equal to n / 2. | C # code to find majority element in a sorted array ; Driver code"
Solution in C#:

using System ; public class GFG { public static int findMajority ( int [ ] arr , int n ) { return arr [ n / 2 ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( findMajority ( arr , n ) ) ; } }


"Find element in array that divides all array elements | C # program to find such number in the array that all array elements are divisible by it ; Returns GCD of two numbers ; Function to return the desired number if exists ; Find GCD of array ; Check if GCD is present in array ; Driver Code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findNumber ( int [ ] arr , int n ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) ans = gcd ( ans , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == ans ) return ans ; return - 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 4 } ; int n = arr . Length ; Console . WriteLine ( findNumber ( arr , n ) ) ; } }


"Find element using minimum segments in Seven Segment Display | C # program to find minimum number of segments required ; Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driver program"
Solution in C#:

using System ; class GFG { static int [ ] seg = new int [ 10 ] { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int computeSegment ( int x ) { if ( x == 0 ) return seg [ 0 ] ; int count = 0 ; while ( x > 0 ) { count += seg [ x % 10 ] ; x /= 10 ; } return count ; } static int elementMinSegment ( int [ ] arr , int n ) { int minseg = computeSegment ( arr [ 0 ] ) ; int minindex = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int temp = computeSegment ( arr [ i ] ) ; if ( temp < minseg ) { minseg = temp ; minindex = i ; } } return arr [ minindex ] ; } static public void Main ( ) { int [ ] arr = { 489 , 206 , 745 , 123 , 756 } ; int n = arr . Length ; Console . WriteLine ( elementMinSegment ( arr , n ) ) ; } }


"Find element with the maximum set bits in an array | C # implementation of the approach ; Function to return the element from the array which has the maximum set bits ; To store the required element and the maximum set bits so far ; Count of set bits in the current element ; Update the max ; Driver code"
Solution in C#:

using System ; class GFG { static int maxBitElement ( int [ ] arr , int n ) { int num = 0 , max = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = BitCount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } static int BitCount ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 } ; int n = arr . Length ; Console . Write ( maxBitElement ( arr , n ) ) ; } }