"Find elements in a given range having at least one odd divisor | C # program to print all numbers with least one odd factor in the given range ; Function to prints all numbers with at least one odd divisor ; Check if the number is not a power of two ; Driver Code"
Solution in C#:

using System ; class GFG { static int printOddFactorNumber ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) { if ( ( i > 0 ) && ( ( i & ( i - 1 ) ) != 0 ) ) Console . Write ( i + " ▁ " ) ; } return 0 ; } public static void Main ( ) { int N = 2 , M = 10 ; printOddFactorNumber ( N , M ) ; } }


"Find elements larger than half of the elements in an array | C # program to find elements that are larger than half of the elements in array ; Prints elements larger than n / 2 element ; Sort the array in ascending order ; Print last ceil ( n / 2 ) elements ; Driver Code"
Solution in C#:

using System ; class GFG { static void findLarger ( int [ ] arr , int n ) { Array . Sort ( arr ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 6 , 1 , 0 , 9 } ; int n = arr . Length ; findLarger ( arr , n ) ; } }


"Find elements larger than half of the elements in an array | Set 2 | C # program for the above approach ; Function to find the element that are larger than half of elements of the array ; Find the value of mid ; Stores the maximum element ; Stores the frequency of each array element ; Traverse the array in the reverse order ; Decrement the value of count [ i ] and mid ; Print the current element ; Check if the value of mid is equal to 0 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findLarger ( int [ ] arr , int n ) { int mid = ( n + 1 ) / 2 ; int mx = Int32 . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > mx ) mx = arr [ i ] ; } int [ ] count = new int [ mx + 1 ] ; Array . Clear ( count , 0 , mx + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( int i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; Console . Write ( i + " ▁ " ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } public static void Main ( ) { int [ ] arr = { 10 , 4 , 2 , 8 , 9 } ; int N = arr . Length ; findLarger ( arr , N ) ; } }


"Find elements of array using XOR of consecutive elements | C # program to find the array elements using XOR of consecutive elements ; Function to find the array elements using XOR of consecutive elements ; array to store the original elements ; first element a i . e elements [ 0 ] = a ; To get the next elements we have to calculate xor of previous elements with given xor of 2 consecutive elements . e . g . if a ^ b = k1 so to get b xor a both side . b = k1 ^ a ; Printing the original array elements ; Driver Code"
Solution in C#:

using System ; class GFG { static void getElements ( int a , int [ ] arr , int n ) { int [ ] elements = new int [ n + 1 ] ; elements [ 0 ] = a ; for ( int i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( int i = 0 ; i < n + 1 ; i ++ ) Console . Write ( elements [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 13 , 2 , 6 , 1 } ; int n = arr . Length ; int a = 5 ; getElements ( a , arr , n ) ; } }


"Find elements which are present in first array and not in second | C # efficient program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Store all elements of second array in a hash table ; Print all elements of first array that are not present in hash table ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static void findMissing ( int [ ] a , int [ ] b , int n , int m ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < m ; i ++ ) s . Add ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! s . Contains ( a [ i ] ) ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 6 , 3 , 4 , 5 } ; int [ ] b = { 2 , 4 , 3 , 1 , 0 } ; int n = a . Length ; int m = b . Length ; findMissing ( a , b , n , m ) ; } }


"Find elements which are present in first array and not in second | C # simple program to find elements which are not present in second array ; Function for finding elements which are there in a [ ] but not in b [ ] . ; Driver code"
Solution in C#:

using System ; class GFG { static void findMissing ( int [ ] a , int [ ] b , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] a = { 1 , 2 , 6 , 3 , 4 , 5 } ; int [ ] b = { 2 , 4 , 3 , 1 , 0 } ; int n = a . Length ; int m = b . Length ; findMissing ( a , b , n , m ) ; } }


"Find even occurring elements in an array of limited range | C # Program to find the even occurring elements in given array ; Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code"
Solution in C#:

using System ; class GFG { static void printRepeatingEven ( int [ ] arr , int n ) { long _xor = 0L ; long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { Console . Write ( arr [ i ] + " ▁ " ) ; _xor ^= pos ; } } } public static void Main ( ) { int [ ] arr = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = arr . Length ; printRepeatingEven ( arr , n ) ; } }


"Find farthest node from each node in Tree | C # implementation to find the farthest node from each vertex of the tree ; Adjacency list to store edges ; Add edge between U and V in tree ; Edge from U to V ; Edge from V to U ; DFS to find the first End Node of diameter ; Calculating level of nodes ; Go in opposite direction of parent ; Function to clear the levels of the nodes ; Set all value of lvl [ ] to 0 for next dfs ; Set maximum with 0 ; DFS will calculate second end of the diameter ; Calculating level of nodes ; Store the node with maximum depth from end1 ; Go in opposite direction of parent ; Function to find the distance of the farthest distant node ; Storing distance from end1 to node u ; Function to find the distance of nodes from second end of diameter ; Storing distance from end2 to node u ; Joining Edge between two nodes of the tree ; Find the one end of the diameter of tree ; Find the other end of the diameter of tree ; Find the distance to each node from end1 ; Find the distance to each node from end2 ; Comparing distance between the two ends of diameter ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 10000 ; static List < int > [ ] adj = new List < int > [ N ] ; static int [ ] lvl = new int [ N ] , dist1 = new int [ N ] , dist2 = new int [ N ] ; static void AddEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static int end1 , end2 , maxi ; static void findFirstEnd ( int u , int p ) { lvl [ u ] = 1 + lvl [ p ] ; if ( lvl [ u ] > maxi ) { maxi = lvl [ u ] ; end1 = u ; } for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findFirstEnd ( adj [ u ] [ i ] , u ) ; } } } static void clear ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { lvl [ i ] = 0 ; } maxi = 0 ; dist1 [ 0 ] = dist2 [ 0 ] = - 1 ; } static void findSecondEnd ( int u , int p ) { lvl [ u ] = 1 + lvl [ p ] ; if ( lvl [ u ] > maxi ) { maxi = lvl [ u ] ; end2 = u ; } for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findSecondEnd ( adj [ u ] [ i ] , u ) ; } } } static void findDistancefromFirst ( int u , int p ) { dist1 [ u ] = 1 + dist1 [ p ] ; for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findDistancefromFirst ( adj [ u ] [ i ] , u ) ; } } } static void findDistancefromSecond ( int u , int p ) { dist2 [ u ] = 1 + dist2 [ p ] ; for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( adj [ u ] [ i ] != p ) { findDistancefromSecond ( adj [ u ] [ i ] , u ) ; } } } static void findNodes ( ) { int n = 5 ; AddEdge ( 1 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 3 , 4 ) ; AddEdge ( 3 , 5 ) ; findFirstEnd ( 1 , 0 ) ; clear ( n ) ; findSecondEnd ( end1 , 0 ) ; findDistancefromFirst ( end1 , 0 ) ; findDistancefromSecond ( end2 , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int x = dist1 [ i ] ; int y = dist2 [ i ] ; if ( x >= y ) { Console . Write ( end1 + " ▁ " ) ; } else { Console . Write ( end2 + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) { adj [ i ] = new List < int > ( ) ; } findNodes ( ) ; } }


"Find final value if we double after every successful search in array | C # program to find value if we double the value after every successful search ; Function to Find the value of k ; Search for k . After every successful search , double k . ; Driver Code"
Solution in C#:

using System ; class GFG { static int findValue ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == k ) k *= 2 ; return k ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 10 , 8 , 1 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findValue ( arr , n , k ) ) ; } }


"Find first and last digits of a number | C # Program to find first and last digits of a number ; Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; driver function"
Solution in C#:

using System ; public class GfG { public static int firstDigit ( int n ) { while ( n >= 10 ) n /= 10 ; return n ; } public static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void Main ( ) { int n = 98562 ; Console . WriteLine ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }


"Find first and last digits of a number | C # program to find first and last digits of a number ; Find the first digit ; Find total number of digits - 1 ; Find first digit ; Return first digit ; Find the last digit ; return the last digit ; Driver program"
Solution in C#:

using System ; class GFG { static int firstDigit ( int n ) { int digits = ( int ) ( Math . Log10 ( n ) ) ; n = ( int ) ( n / ( int ) ( Math . Pow ( 10 , digits ) ) ) ; return n ; } static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void Main ( ) { int n = 98562 ; Console . WriteLine ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }


"Find first k natural numbers missing in given array | C # code for the above approach ; Program to print first k missing number ; Creating a hashmap ; Iterate over array ; Iterate to find missing element ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static void printmissingk ( int [ ] arr , int n , int k ) { Dictionary < int , int > d = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { d . Add ( arr [ i ] , arr [ i ] ) ; } int cnt = 1 ; int fl = 0 ; for ( int i = 0 ; i < ( n + k ) ; i ++ ) { if ( ! d . ContainsKey ( cnt ) ) { fl += 1 ; Console . Write ( cnt + " ▁ " ) ; if ( fl == k ) break ; } cnt += 1 ; } } static public void Main ( ) { int [ ] arr = { 1 , 4 , 3 } ; int n = arr . Length ; int k = 3 ; printmissingk ( arr , n , k ) ; } }


"Find first k natural numbers missing in given array | C # program to find missing k numbers in an array . ; Prints first k natural numbers in arr [ 0. . n - 1 ] ; Find first positive number ; Now find missing numbers between array elements ; Find missing numbers after maximum . ; Driver code"
Solution in C#:

using System ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { Console . Write ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { Console . Write ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . Length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }


"Find first non matching leaves in two binary trees | C # program to find first leaves that are not same . ; Tree node ; Utility method to create a new node ; Prints the first non - matching leaf node in two trees if it exists , else prints nothing . ; If any of the tree is empty ; Create two stacks for preorder traversals ; If traversal of one tree is over and other tree still has nodes . ; Do iterative traversal of first tree and find first lead node in it as " temp1" ; pushing right childfirst so that left child comes first while popping . ; Do iterative traversal of second tree and find first lead node in it as " temp2" ; If we found leaves in both trees ; Driver code"
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 x ) { Node temp = new Node ( ) ; temp . data = x ; temp . left = null ; temp . right = null ; return temp ; } static bool isLeaf ( Node t ) { return ( ( t . left == null ) && ( t . right == null ) ) ; } static void findFirstUnmatch ( Node root1 , Node root2 ) { if ( root1 == null root2 == null ) return ; Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . Push ( root1 ) ; s2 . Push ( root2 ) ; while ( s1 . Count != 0 s2 . Count != 0 ) { if ( s1 . Count == 0 s2 . Count == 0 ) return ; Node temp1 = s1 . Peek ( ) ; s1 . Pop ( ) ; while ( temp1 != null && isLeaf ( temp1 ) != true ) { s1 . Push ( temp1 . right ) ; s1 . Push ( temp1 . left ) ; temp1 = s1 . Peek ( ) ; s1 . Pop ( ) ; } Node temp2 = s2 . Peek ( ) ; s2 . Pop ( ) ; while ( temp2 != null && isLeaf ( temp2 ) != true ) { s2 . Push ( temp2 . right ) ; s2 . Push ( temp2 . left ) ; temp2 = s2 . Peek ( ) ; s2 . Pop ( ) ; } if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) { Console . WriteLine ( temp1 . data + " ▁ " + temp2 . data ) ; return ; } } } } public static void Main ( String [ ] args ) { Node root1 = newNode ( 5 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 7 ) ; root1 . left . left = newNode ( 10 ) ; root1 . left . right = newNode ( 11 ) ; Node root2 = newNode ( 6 ) ; root2 . left = newNode ( 10 ) ; root2 . right = newNode ( 15 ) ; findFirstUnmatch ( root1 , root2 ) ; } }


"Find first non | C # implementation to find the first non - repeating element of the string using Linked List ; Function to find the first non - repeating element of the given string using Linked List ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class FirstNonRepeatingElement { static void firstNonRepElement ( string str ) { LinkedList < char > list = new LinkedList < char > ( ) ; list . AddLast ( str [ 0 ] ) ; for ( int i = 1 ; i < str . Length ; i ++ ) { if ( list . Contains ( str [ i ] ) ) list . Remove ( str [ i ] ) ; else list . AddLast ( str [ i ] ) ; } Console . Write ( list . First . Value ) ; } public static void Main ( string [ ] args ) { string str = " geeksforgeeks " ; firstNonRepElement ( str ) ; } }


"Find four elements a , b , c and d in an array such that a + b = c + d | C # Program to find four different elements a , b , c and d of array such that a + b = c + d ; Class to represent a pair ; function to find a , b , c , d such that ( a + b ) = ( c + d ) ; Create an empty Hash to store mapping from sum to pair indexes ; Traverse through all possible pairs of arr [ ] ; If sum of current pair is not in hash , then store it and continue to next pair ; Testing program"
Solution in C#:

using System ; using System . Collections . Generic ; public class ArrayElements { public class pair { private readonly ArrayElements outerInstance ; public int first , second ; public pair ( ArrayElements outerInstance , int f , int s ) { this . outerInstance = outerInstance ; first = f ; second = s ; } } public virtual bool findPairs ( int [ ] arr ) { Dictionary < int , pair > map = new Dictionary < int , pair > ( ) ; int n = arr . Length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . ContainsKey ( sum ) ) { map [ sum ] = new pair ( this , i , j ) ; } else { pair p = map [ sum ] ; Console . WriteLine ( " ( " + arr [ p . first ] + " , ▁ " + arr [ p . second ] + " ) ▁ and ▁ ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; return true ; } } } return false ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 3 , 4 , 7 , 1 , 2 , 9 , 8 } ; ArrayElements a = new ArrayElements ( ) ; a . findPairs ( arr ) ; } }


"Find four elements that sum to a given value | Set 1 ( n ^ 3 solution ) | C # program for naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; A naive solution to print all combination of 4 elements in A [ ] with sum equal to X ; Fix the first element and find other three ; Fix the second element and find other two ; Fix the third element and find the fourth ; find the fourth ; Driver program to test above functions"
Solution in C#:

using System ; class FindFourElements { void findFourElements ( int [ ] A , int n , int X ) { for ( int i = 0 ; i < n - 3 ; i ++ ) { for ( int j = i + 1 ; j < n - 2 ; j ++ ) { for ( int k = j + 1 ; k < n - 1 ; k ++ ) { for ( int l = k + 1 ; l < n ; l ++ ) { if ( A [ i ] + A [ j ] + A [ k ] + A [ l ] == X ) Console . Write ( A [ i ] + " ▁ " + A [ j ] + " ▁ " + A [ k ] + " ▁ " + A [ l ] ) ; } } } } } public static void Main ( ) { FindFourElements findfour = new FindFourElements ( ) ; int [ ] A = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = A . Length ; int X = 91 ; findfour . findFourElements ( A , n , X ) ; } }


"Find four elements that sum to a given value | Set 2 | A hashing based C # program to find if there are four elements with given sum . ; The function finds four elements with given sum X ; Store sums of all pairs in a hash table ; Traverse through all pairs and search for X - ( current pair sum ) . ; If X - sum is present in hash table , ; Making sure that all elements are distinct array elements and an element is not considered more than once . ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findFourElements ( int [ ] arr , int n , int X ) { Dictionary < int , pair > mp = new Dictionary < int , pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( mp . ContainsKey ( arr [ i ] + arr [ j ] ) ) mp [ arr [ i ] + arr [ j ] ] = new pair ( i , j ) ; else mp . Add ( arr [ i ] + arr [ j ] , new pair ( i , j ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( mp . ContainsKey ( X - sum ) ) { pair p = mp [ X - sum ] ; if ( p . first != i && p . first != j && p . second != i && p . second != j ) { Console . Write ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ p . first ] + " , ▁ " + arr [ p . second ] ) ; return ; } } } } } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . Length ; int X = 91 ; findFourElements ( arr , n , X ) ; } }


"Find four elements that sum to a given value | Set 2 | C # program to find four elements with the given sum ; Function to find 4 elements that add up to given sum ; Iterate from 0 to temp . length ; Iterate from 0 to arr . length ; Iterate from i + 1 to arr . length ; Store curr_sum = arr [ i ] + arr [ j ] ; Check if X - curr_sum if present in map ; Store pair having map value X - curr_sum ; Print the output ; Program for two Sum ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void fourSum ( int X , int [ ] arr , Dictionary < int , Tuple < int , int > > Map , int N ) { int [ ] temp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) temp [ i ] = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int curr_sum = arr [ i ] + arr [ j ] ; if ( Map . ContainsKey ( X - curr_sum ) ) { Tuple < int , int > p = Map [ X - curr_sum ] ; if ( p . Item1 != i && p . Item2 != i && p . Item1 != j && p . Item2 != j && temp [ p . Item1 ] == 0 && temp [ p . Item2 ] == 0 && temp [ i ] == 0 && temp [ j ] == 0 ) { Console . Write ( arr [ i ] + " , " + arr [ j ] + " , " + arr [ p . Item1 ] + " , " + arr [ p . Item2 ] ) ; temp [ p . Item2 ] = 1 ; temp [ i ] = 1 ; temp [ j ] = 1 ; break ; } } } } } static Dictionary < int , Tuple < int , int > > twoSum ( int [ ] nums , int N ) { Dictionary < int , Tuple < int , int > > Map = new Dictionary < int , Tuple < int , int > > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { Map [ nums [ i ] + nums [ j ] ] = new Tuple < int , int > ( i , j ) ; } } return Map ; } static void Main ( ) { int [ ] arr = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . Length ; int X = 91 ; Dictionary < int , Tuple < int , int > > Map = twoSum ( arr , n ) ; fourSum ( X , arr , Map , n ) ; } }


"Find four factors of N with maximum product and sum equal to N | Set 3 | C # implementation of above approach ; Function to find primes ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to find factors ; run a loop upto square root of that number ; if the n is perfect square ; otherwise push it 's two divisors ; sort the divisors ; Function to find max product ; To store factors of ' n ' ; find factors ; if it is divisible by 4. ; if it is prime ; otherwise answer will be possible ; include last third factor ; nested loop to find other two factors ; Driver code ; function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static List < List < int > > v = new List < List < int > > ( ) ; static void factors ( int N ) { for ( int i = 2 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( i / j == j ) v [ i ] . Add ( j ) ; else { v [ i ] . Add ( j ) ; v [ i ] . Add ( i / j ) ; } } } v [ i ] . Sort ( ) ; } } static int product ( int n ) { v . Clear ( ) ; for ( int i = 0 ; i < n + 100 ; i ++ ) v . Add ( new List < int > ( ) ) ; factors ( n + 100 ) ; if ( n % 4 == 0 ) { int x = n / 4 ; x *= x ; return x * x ; } else { if ( isPrime ( n ) ) return - 1 ; else { int ans = - 1 ; if ( v [ n ] . Count > 2 ) { int fac = v [ n ] [ v [ n ] . Count - 3 ] ; for ( int i = v [ n ] . Count - 1 ; i >= 0 ; i -- ) { for ( int j = v [ n ] . Count - 1 ; j >= 0 ; j -- ) { if ( ( fac * 2 ) + ( v [ n ] [ j ] + v [ n ] [ i ] ) == n ) ans = Math . Max ( ans , fac * fac * v [ n ] [ j ] * v [ n ] [ i ] ) ; } } return ans ; } } } return 0 ; } public static void Main ( String [ ] args ) { int n = 24 ; Console . WriteLine ( product ( n ) ) ; } }


"Find four factors of N with maximum product and sum equal to N | Set | C # program to find four factors of N with maximum product and sum equal to N ; Function to find factors and to print those four factors ; push all the factors in the container ; number of factors ; Initial maximum ; hash - array to mark the pairs ; form all the pair sums ; if the pair sum is less than n ; push in another container ; mark the sum with the elements formed ; mark in the map that v [ i ] + v [ j ] is present ; new size of all the pair sums ; iterate for all pair sum ; the required part ; if the required part is also present in pair sum ; find the elements with which the first pair is formed ; find the elements with which the second pair is formed ; check for previous maximum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findfactors ( int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; List < int > v = new List < int > ( ) , v1 = new List < int > ( ) ; for ( int i = 1 ; i <= ( int ) Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . Add ( i ) ; if ( i != ( n / i ) && i != 1 ) v . Add ( n / i ) ; } } int s = v . Count ; int maxi = - 1 ; int [ ] mp1_first = new int [ n + 5 ] ; int [ ] mp1_second = new int [ n + 5 ] ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = i ; j < s ; j ++ ) { if ( v [ i ] + v [ j ] < n ) { v1 . Add ( v [ i ] + v [ j ] ) ; mp1_first [ v [ i ] + v [ j ] ] = v [ i ] ; mp1_second [ v [ i ] + v [ j ] ] = v [ j ] ; mpp . Add ( v [ i ] + v [ j ] , 1 ) ; } } } s = v1 . Count ; for ( int i = 0 ; i < s ; i ++ ) { int el = n - ( v1 [ i ] ) ; if ( mpp . ContainsKey ( el ) ) { int a = mp1_first [ v1 [ i ] ] ; int b = mp1_second [ v1 [ i ] ] ; int c = mp1_first [ n - v1 [ i ] ] ; int d = mp1_second [ n - v1 [ i ] ] ; maxi = Math . Max ( a * b * c * d , maxi ) ; } } if ( maxi == - 1 ) Console . WriteLine ( " Not ▁ Possible " ) ; else { Console . WriteLine ( " The ▁ maximum ▁ product " + " ▁ is ▁ " + maxi ) ; } } public static void Main ( String [ ] args ) { int n = 50 ; findfactors ( n ) ; } }


"Find frequency of each element in a limited range array in less than O ( n ) time | C # program to count number of occurrences of each element in the array ; It prints number of occurrences of each element in the array . ; HashMap to store frequencies ; traverse the array ; update the frequency ; traverse the hashmap ; Driver function"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void findFrequency ( int [ ] arr , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . ContainsKey ( arr [ i ] ) ) mp [ arr [ i ] ] = 0 ; mp [ arr [ i ] ] ++ ; } foreach ( KeyValuePair < int , int > kvp in mp ) Console . WriteLine ( " Element ▁ " + kvp . Key + " ▁ occurs ▁ " + kvp . Value + " ▁ times " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . Length ; findFrequency ( arr , n ) ; } }


"Find frequency of each element in a limited range array in less than O ( n ) time | C # program to count number of occurrences of each element in the array in O ( n ) time and O ( 1 ) space ; check if the current element is equal to previous element . ; reset the frequency ; print the last element and its frequency ; Driver code"
Solution in C#:

using System ; class GFG { public static void findFrequencies ( int [ ] ele ) { int freq = 1 ; int idx = 1 ; int element = ele [ 0 ] ; while ( idx < ele . Length ) { if ( ele [ idx - 1 ] == ele [ idx ] ) { freq ++ ; idx ++ ; } else { Console . WriteLine ( element + " ▁ " + freq ) ; element = ele [ idx ] ; idx ++ ; freq = 1 ; } } Console . WriteLine ( element + " ▁ " + freq ) ; } public static void Main ( String [ ] args ) { Console . WriteLine ( " - - - frequencies ▁ in ▁ a ▁ sorted ▁ array - - - - " ) ; findFrequencies ( new int [ ] { 10 , 20 , 30 , 30 , 30 , 40 , 50 , 50 , 50 , 50 , 70 } ) ; } }


"Find frequency of each element in a limited range array in less than O ( n ) time | C # program to count number of occurrences of each element in the array in less than O ( n ) time ; A recursive function to count number of occurrences for each element in the array without traversing the whole array ; If element at index low is equal to element at index high in the array ; increment the frequency of the element by count of elements between high and low ; Find mid and recurse for left and right subarray ; A wrapper over recursive function findFrequencyUtil ( ) . It print number of occurrences of each element in the array . ; create a empty vector to store frequencies and initialize it by 0. Size of vector is maximum value ( which is last value in sorted array ) plus 1. ; Fill the vector with frequency ; Print the frequencies ; Driver Code"
Solution in C#:

using System ; class GFG { static void findFrequencyUtil ( int [ ] arr , int low , int high , int [ ] freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { int mid = ( low + high ) / 2 ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } static void findFrequency ( int [ ] arr , int n ) { int [ ] freq = new int [ arr [ n - 1 ] + 1 ] ; findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( int i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) Console . WriteLine ( " Element ▁ " + i + " ▁ occurs ▁ " + freq [ i ] + " ▁ times " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . Length ; findFrequency ( arr , n ) ; } }


"Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | C # program to find GCD of a ^ n and b . ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code"
Solution in C#:

using System ; class GFG { static long modPower ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long gcdPow ( long a , long n , long c ) { if ( a % c == 0 ) return c ; long modexpo = modPower ( a , n , c ) ; return gcd ( modexpo , c ) ; } public static void Main ( ) { long a = 10248585 , n = 1000000 , c = 12564 ; Console . Write ( gcdPow ( a , n , c ) ) ; } }


"Find height of a special binary tree whose leaf nodes are connected | C # implementation to calculate height of a special tree whose leaf nodes forms a circular doubly linked list ; User defined node class ; Constructor to create a new tree node ; function to check if given node is a leaf node or node ; If given node ' s ▁ left ' s right is pointing to given node and its right ' s ▁ left ▁ is ▁ pointing ▁ to ▁ the ▁ node ▁ ▁ itself ▁ then ▁ it ' s a leaf ; Compute the height of a tree -- the number of Nodes along the longest path from the root node down to the farthest leaf node . ; if node is NULL , return 0 ; if node is a leaf node , return 1 ; compute the depth of each subtree and take maximum ; Driver code ; Given tree contains 3 leaf nodes ; create circular doubly linked list out of leaf nodes of the tree set next pointer of linked list ; set prev pointer of linked list ; calculate height of the tree"
Solution in C#:

using System ; public class Node { public int data ; public Node left , right ; public Node ( int key ) { data = key ; left = right = null ; } } public class GFG { static bool isLeaf ( Node node ) { return ( node . left != null && node . left . right == node && node . right != null && node . right . left == node ) ; } static int maxDepth ( Node node ) { if ( node == null ) return 0 ; if ( isLeaf ( node ) ) return 1 ; return 1 + Math . Max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . left . left . left = new Node ( 6 ) ; Node L1 = root . left . left . left ; Node L2 = root . left . right ; Node L3 = root . right ; L1 . right = L2 ; L2 . right = L3 ; L3 . right = L1 ; L3 . left = L2 ; L2 . left = L1 ; L1 . left = L3 ; Console . WriteLine ( " Height ▁ of ▁ tree ▁ is ▁ " + maxDepth ( root ) ) ; } }


"Find i 'th Index character in a binary string obtained after n iterations | C # Program to find ith character in a binary string . ; Function to store binary Representation ; Function to find ith character ; Function to change decimal to binary ; Assign s1 string in s string ; Driver Code"
Solution in C#:

using System ; class GFG { static string ReverseString ( string s ) { char [ ] arr = s . ToCharArray ( ) ; Array . Reverse ( arr ) ; return new string ( arr ) ; } static void binary_conversion ( ref string s , int m ) { while ( m != 0 ) { int tmp = m % 2 ; s += tmp . ToString ( ) ; m = ( int ) ( m / 2 ) ; } s = ReverseString ( s ) ; } static int find_character ( int n , int m , int i ) { string s = " " ; binary_conversion ( ref s , m ) ; string s1 = " " ; for ( int x = 0 ; x < n ; x ++ ) { for ( int y = 0 ; y < s . Length ; y ++ ) { if ( s [ y ] == '1' ) s1 += "10" ; else s1 += "01" ; } s = s1 ; s1 = " " ; } return s [ i ] - '0' ; } static void Main ( ) { int m = 5 , n = 2 , i = 8 ; Console . Write ( find_character ( n , m , i ) ) ; } }


"Find if a 2 | C # program to Find a 2 - D array is completely traversed or not by following the cell values ; Cell class constructor ; function which tells all cells are visited or not ; starting cell values ; if we get null before the end of loop then returns false . Because it means we didn 't traverse all the cells ; If found cycle then return false ; Update startx and starty values to next cell values ; finally if we reach our goal then returns true ; Driver Code"
Solution in C#:

using System ; public class Cell { public int x ; public int y ; public Cell ( int x , int y ) { this . x = x ; this . y = y ; } } public class MoveCellPerCellValue { public Boolean isAllCellTraversed ( Cell [ , ] grid ) { Boolean [ , ] visited = new Boolean [ grid . GetLength ( 0 ) , grid . GetLength ( 1 ) ] ; int total = grid . GetLength ( 0 ) * grid . GetLength ( 1 ) ; int startx = grid [ 0 , 0 ] . x ; int starty = grid [ 0 , 0 ] . y ; for ( int i = 0 ; i < total - 2 ; i ++ ) { if ( grid [ startx , starty ] == null ) return false ; if ( visited [ startx , starty ] == true ) return false ; visited [ startx , starty ] = true ; int x = grid [ startx , starty ] . x ; int y = grid [ startx , starty ] . y ; startx = x ; starty = y ; } if ( grid [ startx , starty ] == null ) return true ; return false ; } public static void Main ( String [ ] args ) { Cell [ , ] cell = new Cell [ 3 , 2 ] ; cell [ 0 , 0 ] = new Cell ( 0 , 1 ) ; cell [ 0 , 1 ] = new Cell ( 2 , 0 ) ; cell [ 1 , 0 ] = null ; cell [ 1 , 1 ] = new Cell ( 1 , 0 ) ; cell [ 2 , 0 ] = new Cell ( 2 , 1 ) ; cell [ 2 , 1 ] = new Cell ( 1 , 1 ) ; MoveCellPerCellValue mcp = new MoveCellPerCellValue ( ) ; Console . WriteLine ( mcp . isAllCellTraversed ( cell ) ) ; } }


"Find if a binary matrix exists with given row and column sums | C # implementation of above approach ; Function to check if matrix exists ; Store sum of rowsums , max of row sum number of non zero row sums ; Store sum of column sums , max of column sum number of non zero column sums ; Check condition 1 , 2 , 3 ; Driver Code"
Solution in C#:

using System ; public class GFG { static bool matrix_exist ( int [ ] row , int [ ] column , int r , int c ) { int row_sum = 0 ; int column_sum = 0 ; int row_max = - 1 ; int column_max = - 1 ; int row_non_zero = 0 ; int column_non_zero = 0 ; for ( int i = 0 ; i < r ; i ++ ) { row_sum += row [ i ] ; row_max = Math . Max ( row_max , row [ i ] ) ; if ( row [ i ] > 0 ) { row_non_zero ++ ; } } for ( int i = 0 ; i < c ; i ++ ) { column_sum += column [ i ] ; column_max = Math . Max ( column_max , column [ i ] ) ; if ( column [ i ] > 0 ) { column_non_zero ++ ; } } if ( ( row_sum != column_sum ) || ( row_max > column_non_zero ) || ( column_max > row_non_zero ) ) { return false ; } return true ; } static public void Main ( ) { int [ ] row = { 2 , 2 , 2 , 2 , 2 } ; int [ ] column = { 5 , 5 , 0 , 0 } ; int r = row . Length ; int c = column . Length ; if ( matrix_exist ( row , column , r , c ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Find if a crest is present in the index range [ L , R ] of the given array | C # implementation of the approach ; Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code"
Solution in C#:

using System ; class GFG { static bool hasCrest ( int [ ] arr , int n , int L , int R ) { int [ ] present = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { present [ i ] = 0 ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( int i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 } ; int N = arr . Length ; int L = 2 ; int R = 6 ; if ( hasCrest ( arr , N , L , R ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if a degree sequence can form a simple graph | Havel | C # implementation of the approach ; Function that returns true if a simple graph exists ; Keep performing the operations until one of the stopping condition is met ; Sort the list in non - decreasing order ; Check if all the elements are equal to 0 ; Store the first element in a variable and delete it from the list ; Check if enough elements are present in the list ; Subtract first element from next v elements ; Check if negative element is encountered after subtraction ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GFG { static bool graphExists ( ArrayList a , int n ) { while ( true ) { a . Sort ( ) ; a . Reverse ( ) ; if ( ( int ) a [ 0 ] == 0 ) return true ; int v = ( int ) a [ 0 ] ; a . Remove ( a [ 0 ] ) ; if ( v > a . Count ) return false ; for ( int i = 0 ; i < v ; i ++ ) { a [ i ] = ( int ) a [ i ] - 1 ; if ( ( int ) a [ i ] < 0 ) return false ; } } } public static void Main ( string [ ] args ) { ArrayList a = new ArrayList ( ) { 3 , 3 , 3 , 3 } ; int n = a . Count ; if ( graphExists ( a , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " NO " ) ; } } }


"Find if a molecule can be formed from 3 atoms using their valence numbers | C # implementation of the above approach ; Function to check if it is possible ; Driver code"
Solution in C#:

using System ; class GFG { static void printPossible ( int a , int b , int c ) { if ( ( a + b + c ) % 2 != 0 a + b < c ) Console . Write ( " NO " ) ; else Console . Write ( " YES " ) ; } public static void Main ( ) { int a = 2 , b = 4 , c = 2 ; printPossible ( a , b , c ) ; } }


"Find if a number is divisible by every number in a list | C # program which check is a number divided with every element in list or no ; Function which check is a number divided with every element in list or not ; driver program"
Solution in C#:

using System ; class GFG { static bool findNoIsDivisibleOrNot ( int [ ] a , int n ) { for ( int i = 0 ; i < a . Length ; i ++ ) { if ( a [ i ] % n != 0 ) return false ; } return true ; } public static void Main ( ) { int [ ] a = { 14 , 12 , 4 , 18 } ; int n = 2 ; if ( findNoIsDivisibleOrNot ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if a number is part of AP whose first element and difference are given | C # program to check if x exist or not in the given AP . ; returns yes if exist else no . ; If difference is 0 , then x must be same as a . ; Else difference between x and a must be divisible by d . ; Driver code ."
Solution in C#:

using System ; class GFG { static bool isMember ( int a , int d , int x ) { if ( d == 0 ) return ( x == a ) ; return ( ( x - a ) % d == 0 && ( x - a ) / d >= 0 ) ; } public static void Main ( ) { int a = 1 , x = 7 , d = 3 ; if ( isMember ( a , d , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if a point lies inside , outside or on the circumcircle of three points A , B , C | C # program to find the points which lies inside , outside or on the circle ; Structure Pointer to store x and y coordinates ; Function to find the line given two points ; Function which converts the input line to its perpendicular bisector . It also inputs the points whose mid - point lies o on the bisector ; Find the mid point ; x coordinates ; y coordinates ; c = - bx + ay ; Assign the coefficient of a and b ; Returns the intersection point of two lines ; Find determinant ; Returns the intersection point of two lines ; Find determinant ; Function to find the point lies inside , outside or on the circle ; Store the coordinates radius of circumcircle ; Line PQ is represented as ax + by = c ; Line QR is represented as ex + fy = g ; Converting lines PQ and QR to perpendicular bisectors . After this , L = ax + by = c M = ex + fy = g ; The point of intersection of L and M gives r as the circumcenter ; Length of radius ; Distance between radius and the given point D ; Condition for point lies inside circumcircle ; Condition for point lies on circumcircle ; Condition for point lies outside circumcircle ; Driver Code ; Given Points ; Function call to find the point lies inside , outside or on the circle"
Solution in C#:

using System ; class GFG { class point { public double x , y ; public point ( ) { } public point ( double x , double y ) { this . x = x ; this . y = y ; } } ; static void lineFromPoints ( point P , point Q , double a , double b , double c ) { a = Q . y - P . y ; b = P . x - Q . x ; c = a * ( P . x ) + b * ( P . y ) ; } static void perpenBisectorFromLine ( point P , point Q , double a , double b , double c ) { point mid_point = new point ( ) ; mid_point . x = ( P . x + Q . x ) / 2 ; mid_point . y = ( P . y + Q . y ) / 2 ; c = - b * ( mid_point . x ) + a * ( mid_point . y ) ; double temp = a ; a = - b ; b = temp ; } static double LineInterX ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { double determ = a1 * b2 - a2 * b1 ; double x = ( b2 * c1 - b1 * c2 ) ; x /= determ ; return x ; } static double LineInterY ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { double determ = a1 * b2 - a2 * b1 ; double y = ( a1 * c2 - a2 * c1 ) ; y /= determ ; return y ; } static void findPosition ( point P , point Q , point R , point D ) { point r = new point ( ) ; double a = 0 , b = 0 , c = 0 ; lineFromPoints ( P , Q , a , b , c ) ; double e = 0 , f = 0 , g = 0 ; lineFromPoints ( Q , R , e , f , g ) ; perpenBisectorFromLine ( P , Q , a , b , c ) ; perpenBisectorFromLine ( Q , R , e , f , g ) ; r . x = LineInterX ( a , b , c , e , f , g ) ; r . y = LineInterY ( a , b , c , e , f , g ) ; double q = ( r . x - P . x ) * ( r . x - P . x ) + ( r . y - P . y ) * ( r . y - P . y ) ; double dis = ( r . x - D . x ) * ( r . x - D . x ) + ( r . y - D . y ) * ( r . y - D . y ) ; if ( dis < q ) { Console . Write ( " Point ▁ ( " + D . x + " , ▁ " + D . y + " ) ▁ is ▁ inside ▁ " + " the ▁ circumcircle " ) ; } else if ( dis == q ) { Console . Write ( " Point ▁ ( " + D . x + " , ▁ " + D . y + " ) ▁ lies ▁ on ▁ the ▁ " + " circumcircle " ) ; } else { Console . Write ( " Point ▁ ( " + D . x + " , ▁ " + D . y + " ) ▁ lies ▁ outside " + " ▁ the ▁ circumcircle " ) ; } } public static void Main (...


"Find if a string starts and ends with another given string | C # program to find if a given corner string is present at corners . ; If length of corner string is more , it cannot be present at corners . ; Return true if corner string is present at both corners of given string . ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isCornerPresent ( string str , string corner ) { int n = str . Length ; int cl = corner . Length ; if ( n < cl ) return false ; return ( str . Substring ( 0 , cl ) . Equals ( corner ) && str . Substring ( n - cl , cl ) . Equals ( corner ) ) ; } static void Main ( ) { string str = " geeksforgeeks " ; string corner = " geeks " ; if ( isCornerPresent ( str , corner ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if an array contains a string with one mismatch | C # program to find if given string is present with one mismatch . ; If the array is empty ; If sizes are same ; If first mismatch ; Second mismatch ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static bool check ( List < String > list , String s ) { int n = ( int ) list . Count ; if ( n == 0 ) { return false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( list [ i ] . Length != s . Length ) { continue ; } bool diff = false ; for ( int j = 0 ; j < ( int ) list [ i ] . Length ; j ++ ) { if ( list [ i ] [ j ] != s [ j ] ) { if ( ! diff ) { diff = true ; } else { diff = false ; break ; } } } if ( diff ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { List < String > s = new List < String > ( ) ; s . Add ( " bana " ) ; s . Add ( " apple " ) ; s . Add ( " banacb " ) ; s . Add ( " bonanza " ) ; s . Add ( " banamf " ) ; Console . WriteLine ( check ( s , " banana " ) == true ? 1 : 0 ) ; } }


"Find if an expression has duplicate parenthesis or not | C # program to find duplicate parenthesis in a balanced expression ; Function to find duplicate parenthesis in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; pop character from the stack ; stores the number of characters between a closing and opening parenthesis if this count is less than or equal to 1 then the brackets are redundant else not ; push open parenthesis ' ( ' , operators and operands to stack ; No duplicates found ; Driver code ; input balanced expression"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Boolean findDuplicateparenthesis ( String s ) { Stack < char > Stack = new Stack < char > ( ) ; char [ ] str = s . ToCharArray ( ) ; foreach ( char ch in str ) { if ( ch == ' ) ' ) { char top = Stack . Peek ( ) ; Stack . Pop ( ) ; int elementsInside = 0 ; while ( top != ' ( ' ) { elementsInside ++ ; top = Stack . Peek ( ) ; Stack . Pop ( ) ; } if ( elementsInside < 1 ) { return true ; } } else { Stack . Push ( ch ) ; } } return false ; } public static void Main ( String [ ] args ) { String str = " ( ( ( a + ( b ) ) + ( c + d ) ) ) " ; if ( findDuplicateparenthesis ( str ) ) { Console . WriteLine ( " Duplicate ▁ Found ▁ " ) ; } else { Console . WriteLine ( " No ▁ Duplicates ▁ Found ▁ " ) ; } } }


"Find if array can be sorted by swaps limited to multiples of k |  ; CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; If k = 1 then ( always possible to sort ) swapping can easily give sorted array ; Comparing sortarray with array ; Element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; If element is present then swapped ; If element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; If flag is 0 Not possible else Possible ; Driver code ; Size of step ; Array initialized ; Length of arr ; Calling function"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void CheckSort ( ArrayList arr , int k , int n ) { ArrayList sortarr = new ArrayList ( arr ) ; sortarr . Sort ( ) ; if ( k == 1 ) Console . Write ( " yes " ) ; else { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( int j = i ; j < n ; j += k ) { if ( ( int ) sortarr [ i ] == ( int ) arr [ j ] ) { int tmp = ( int ) arr [ i ] ; arr [ i ] = ( int ) arr [ j ] ; arr [ j ] = tmp ; flag = 1 ; break ; } if ( j + k >= n ) { break ; } } if ( flag == 0 ) { break ; } } if ( flag == 0 ) Console . Write ( " Not ▁ possible ▁ to ▁ sort " ) ; else Console . Write ( " Possible ▁ to ▁ sort " ) ; } } public static void Main ( string [ ] args ) { int k = 3 ; ArrayList arr = new ArrayList ( ) { 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 } ; int n = arr . Count ; CheckSort ( arr , k , n ) ; } }


"Find if given matrix is Toeplitz or not | C # program to check whether given matrix is a Toeplitz matrix or not ; Function to check if all elements present in descending diagonal starting from position ( i , j ) in the matrix are all same or not ; mismatch found ; we only reach here when all elements in given diagonal are same ; Function to check whether given matrix is a Toeplitz matrix or not ; do for each element in first row ; check descending diagonal starting from position ( 0 , j ) in the matrix ; do for each element in first column ; check descending diagonal starting from position ( i , 0 ) in the matrix ; we only reach here when each descending diagonal from left to right is same ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static int N = 5 ; public static int M = 4 ; static bool checkDiagonal ( int [ , ] mat , int i , int j ) { int res = mat [ i , j ] ; while ( ++ i < N && ++ j < M ) { if ( mat [ i , j ] != res ) return false ; } return true ; } static bool isToepliz ( int [ , ] mat ) { for ( int i = 0 ; i < M ; i ++ ) { if ( ! checkDiagonal ( mat , 0 , i ) ) return false ; } for ( int i = 1 ; i < N ; i ++ ) { if ( ! checkDiagonal ( mat , i , 0 ) ) return false ; } return true ; } public static void Main ( ) { int [ , ] mat = { { 6 , 7 , 8 , 9 } , { 4 , 6 , 7 , 8 } , { 1 , 4 , 6 , 7 } , { 0 , 1 , 4 , 6 } , { 2 , 0 , 1 , 4 } } ; if ( isToepliz ( mat ) ) Console . WriteLine ( " Matrix ▁ is ▁ a ▁ Toepliz ▁ " ) ; else Console . WriteLine ( " Matrix ▁ is ▁ not ▁ a ▁ Toepliz ▁ " ) ; } }


"Find if given matrix is Toeplitz or not | C # program to check whether given matrix is a Toeplitz matrix or not ; row = number of rows col = number of columns ; HashMap to store key , value pairs ; If key value exists in the hashmap , ; We check whether the current value stored in this key matches to element at current index or not . If not , return false ; Else we put key , value pair in hashmap ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isToeplitz ( int [ , ] matrix ) { int row = matrix . GetLength ( 0 ) ; int col = matrix . GetLength ( 1 ) ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { int key = i - j ; if ( map . ContainsKey ( key ) ) { if ( map [ key ] != matrix [ i , j ] ) return false ; } else { map . Add ( i - j , matrix [ i , j ] ) ; } } } return true ; } static void Main ( ) { int [ , ] matrix = { { 12 , 23 , - 32 } , { - 20 , 12 , 23 } , { 56 , - 20 , 12 } , { 38 , 56 , - 20 } } ; string result = ( isToeplitz ( matrix ) ) ? " Yes " : " No " ; Console . WriteLine ( result ) ; } }


"Find if given number is sum of first n natural numbers | C # program for above implementation ; Function to find no . of elements to be added from 1 to get n ; Start adding numbers from 1 ; If sum becomes equal to s return n ; Drivers code"
Solution in C#:

using System ; class GFG { static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n ; if ( sum == s ) return n ; } return - 1 ; } public static void Main ( ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }


"Find if given number is sum of first n natural numbers | C # program for finding s such that sum from 1 to s equals to n ; Function to find no . of elements to be added to get s ; Apply Binary search ; Find mid ; find sum of 1 to mid natural numbers using formula ; If sum is equal to n return mid ; If greater than n do r = mid - 1 ; else do l = mid + 1 ; If not possible , return - 1 ; Drivers code"
Solution in C#:

using System ; public class GFG { static int findS ( int s ) { int l = 1 , r = ( s / 2 ) + 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int sum = mid * ( mid + 1 ) / 2 ; if ( sum == s ) return mid ; else if ( sum > s ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } static public void Main ( ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }


"Find if given number is sum of first n natural numbers | C # program of the above approach ; Function to check if the s is the sum of first N natural number ; Solution of Quadratic Equation ; Condition to check if the solution is a integer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static int isvalid ( int s ) { double k = ( - 1.0 + Math . Sqrt ( 1 + 8 * s ) ) / 2 ; if ( Math . Ceiling ( k ) == Math . Floor ( k ) ) return ( int ) k ; else return - 1 ; } public static void Main ( string [ ] args ) { int s = 15 ; Console . Write ( isvalid ( s ) ) ; } }


"Find if it 's possible to rotate the page by an angle or not. | C # program to fill an array with frequencies . ; function to find if it 's possible  to rotate page or not ; Calculating distance b / w points ; If distance is not equal ; If the points are in same line ; Driver method ; Points a , b , and c"
Solution in C#:

using System ; class GFG { static void possibleOrNot ( long a1 , long a2 , long b1 , long b2 , long c1 , long c2 ) { long dis1 = ( long ) Math . Pow ( b1 - a1 , 2 ) + ( long ) Math . Pow ( b2 - a2 , 2 ) ; long dis2 = ( long ) Math . Pow ( c1 - b1 , 2 ) + ( long ) Math . Pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) Console . Write ( " No " ) ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b2 == ( ( a2 + c2 ) / 2.0 ) ) Console . Write ( " No " ) ; else Console . Write ( " Yes " ) ; } public static void Main ( ) { long a1 = 1 , a2 = 0 , b1 = 2 , b2 = 0 , c1 = 3 , c2 = 0 ; possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) ; } }


"Find if it is possible to choose subarray that it contains exactly K even integers | C # program to check if it is possible to choose a subarray that contains exactly K even integers ; Function to check if it is possible to choose a subarray that contains exactly K even integers ; Variable to store the count of even numbers ; If we have to select 0 even numbers but there is all odd numbers in the array ; If the count of even numbers is greater than or equal to K then we can select a subarray with exactly K even integers ; If the count of even numbers is less than K then we cannot select any subarray with exactly K even integers ; Driver Code"
Solution in C#:

using System ; class GFG { static void isPossible ( int [ ] A , int n , int k ) { int countOfTwo = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] % 2 == 0 ) { countOfTwo ++ ; } } if ( k == 0 && countOfTwo == n ) Console . Write ( " NO " ) ; else if ( countOfTwo >= k ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 } ; int K = 2 ; int n = arr . Length ; isPossible ( arr , n , K ) ; } }


"Find if it is possible to get a ratio from given ranges of costs and quantities | C # program to find if it is possible to get the ratio r ; Returns true if it is possible to get ratio r from given cost and quantity ranges . ; Calculating cost corresponding to value of i ; Driver Code"
Solution in C#:

using System ; class Ratio { static bool isRatioPossible ( int lowCost , int upCost , int lowQuant , int upQuant , int r ) { for ( int i = lowQuant ; i <= upQuant ; i ++ ) { int ans = i * r ; if ( lowCost <= ans && ans <= upCost ) return true ; } return false ; } public static void Main ( ) { int lowCost = 14 , upCost = 30 , lowQuant = 5 , upQuant = 12 , r = 9 ; if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if it is possible to make a binary string which contanins given number of "0" , "1" , "01" and "10" as sub sequences | C # implementation of the approach ; Function that returns true if it is possible to make a binary string consisting of l 0 ' s , ▁ m ▁ 1' s , x "01" sub - sequences and y "10" sub - sequences ; Driver code"
Solution in C#:

using System ; class sol { static Boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void Main ( String [ ] args ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if k bookings possible with given arrival and departure times | C # code implementation of the above approach ; Driver Code"
Solution in C#:

using System ; class GFG { static string areBookingsPossible ( int [ ] A , int [ ] B , int K ) { Array . Sort ( A ) ; Array . Sort ( B ) ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( i + K < A . Length && A [ i + K ] < B [ i ] ) { return " No " ; } } return " Yes " ; } static void Main ( ) { int [ ] arrival = { 1 , 2 , 3 } ; int [ ] departure = { 2 , 3 , 4 } ; int K = 1 ; Console . Write ( areBookingsPossible ( arrival , departure , K ) ) ; } }


"Find if n can be written as product of k numbers | C # program to find if it is possible to write a number n as product of exactly k positive numbers greater than 1. ; Prints k factors of n if n can be written as multiple of k numbers . Else prints - 1. ; A vector to store all prime factors of n ; Insert all 2 's in list ; n must be odd at this point So we skip one element ( i = i + 2 ) ; This is to handle when n > 2 and n is prime ; If size ( P ) < k , k factors are not possible ; printing first k - 1 factors ; calculating and printing product of rest of numbers ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static void kFactors ( int n , int k ) { ArrayList P = new ArrayList ( ) ; while ( n % 2 == 0 ) { P . Add ( 2 ) ; n /= 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; P . Add ( i ) ; } } if ( n > 2 ) P . Add ( n ) ; if ( P . Count < k ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) Console . Write ( P [ i ] + " , ▁ " ) ; int product = 1 ; for ( int i = k - 1 ; i < P . Count ; i ++ ) product = product * ( int ) P [ i ] ; Console . WriteLine ( product ) ; } static void Main ( ) { int n = 54 , k = 3 ; kFactors ( n , k ) ; } }


"Find if nCr is divisible by the given prime | C # Implementation of above approach ; Function to return the highest power of p that divides n ! implementing Legendre Formula ; Return the highest power of p which divides n ! ; Function to return N digits number which is divisible by D ; Find the highest powers of p that divide n ! , r ! and ( n - r ) ! ; If nCr is divisible by p ; Driver code"
Solution in C#:

using System ; class GFG { static int getfactor ( int n , int p ) { int pw = 0 ; while ( n != 0 ) { n /= p ; pw += n ; } return pw ; } static int isDivisible ( int n , int r , int p ) { int x1 = getfactor ( n , p ) ; int x2 = getfactor ( r , p ) ; int x3 = getfactor ( n - r , p ) ; if ( x1 > x2 + x3 ) return 1 ; return 0 ; } static public void Main ( ) { int n = 7 , r = 2 , p = 7 ; if ( isDivisible ( n , r , p ) == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if possible to visit every nodes in given Graph exactly once based on given conditions | C # program for above approach . ; Function to find print path ; If a [ 0 ] is 1 ; Printing path ; Seeking for a [ i ] = 0 and a [ i + 1 ] = 1 ; Printing path ; If a [ N - 1 ] = 0 ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void findpath ( int N , int [ ] a ) { if ( a [ 0 ] != 0 ) { Console . Write ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; return ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( a [ i ] == 0 && a [ i + 1 ] != 0 ) { for ( int j = 1 ; j <= i ; j ++ ) Console . Write ( j + " ▁ " ) ; Console . Write ( N + 1 + " ▁ " ) ; for ( int j = i + 1 ; j <= N ; j ++ ) Console . Write ( j + " ▁ " ) ; return ; } } for ( int i = 1 ; i <= N ; i ++ ) Console . Write ( i + " ▁ " ) ; Console . Write ( N + 1 + " ▁ " ) ; } public static void Main ( ) { int N = 3 ; int [ ] arr = { 0 , 1 , 0 } ; findpath ( N , arr ) ; } }


"Find if string is K | A Naive recursive C # program to find if given string is K - Palindrome or not ; find if given string is K - Palindrome or not ; If first string is empty , the only option is to remove all characters of second string ; If second string is empty , the only option is to remove all characters of first string ; If last characters of two strings are same , ignore last characters and get count for remaining strings . ; If last characters are not same , 1. Remove last char from str1 and recur for m - 1 and n 2. Remove last char from str2 and recur for m and n - 1 Take minimum of above two operations return 1 + Math . Min ( isKPalRec ( str1 , str2 , m - 1 , n ) , Remove from str1 isKPalRec ( str1 , str2 , m , n - 1 ) ) ; Remove from str2 ; Returns true if str is k palindrome . ; Swap values of left and right ; Driver code"
Solution in C#:

using System ; class GFG { static int isKPalRec ( String str1 , String str2 , int m , int n ) { if ( m == 0 ) { return n ; } if ( n == 0 ) { return m ; } if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) { return isKPalRec ( str1 , str2 , m - 1 , n - 1 ) ; } } static bool isKPal ( String str , int k ) { String revStr = str ; revStr = reverse ( revStr ) ; int len = str . Length ; return ( isKPalRec ( str , revStr , len , len ) <= k * 2 ) ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String str = " acdcb " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Find if string is K | C # program to find if String is K - Palindrome or not using all characters exactly once ; When size of String is less than k ; When size of String is equal to k ; When size of String is greater than k to store the frequencies of the characters ; To store the count of characters whose number of occurrences is odd . ; Iterating over the map ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void iskPalindromesPossible ( String s , int k ) { if ( s . Length < k ) { Console . Write ( " Not ▁ Possible " + "
 " ) ; return ; } if ( s . Length == k ) { Console . Write ( " Possible " + "
 " ) ; return ; } Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) if ( freq . ContainsKey ( s [ i ] ) ) { freq [ s [ i ] ] = freq [ s [ i ] ] + 1 ; } else { freq . Add ( s [ i ] , 1 ) ; } int count = 0 ; foreach ( KeyValuePair < int , int > it in freq ) { if ( it . Value % 2 == 1 ) count ++ ; } if ( count > k ) Console . Write ( " No " + "
 " ) ; else Console . Write ( " Yes " + "
 " ) ; } public static void Main ( String [ ] args ) { String str = " poor " ; int K = 3 ; iskPalindromesPossible ( str , K ) ; str = " geeksforgeeks " ; K = 10 ; iskPalindromesPossible ( str , K ) ; } }


"Find if string is K | C # program to find if given String is K - Palindrome or not ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 , n + 1 ] in bottom up fashion . Note that L [ i , j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m , n ] contains length of LCS for X and Y ; find if given String is K - Palindrome or not ; Find reverse of String ; find longest palindromic subsequence of given String ; If the difference between longest palindromic subsequence and the original String is less than equal to k , then the String is k - palindrome ; Swap values of left and right ; Driver code"
Solution in C#:

using System ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int [ , ] L = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i , j ] = 0 ; } else if ( X [ i - 1 ] == Y [ j - 1 ] ) { L [ i , j ] = L [ i - 1 , j - 1 ] + 1 ; } else { L [ i , j ] = Math . Max ( L [ i - 1 , j ] , L [ i , j - 1 ] ) ; } } } return L [ m , n ] ; } static bool isKPal ( String str , int k ) { int n = str . Length ; str = reverse ( str ) ; int lps = lcs ( str , str , n , n ) ; return ( n - lps <= k ) ; } static String reverse ( String input ) { char [ ] temparray = input . ToCharArray ( ) ; int left , right = 0 ; right = temparray . Length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . Join ( " " , temparray ) ; } public static void Main ( String [ ] args ) { String str = " abcdeca " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No " ) ; } }


"Find if string is K | C # program to find if given string is K - Palindrome or not ; find if given string is K - Palindrome or not ; Create a table to store results of subproblems ; Fill dp [ ] [ ] in bottom up manner ; If first string is empty , only option is to remove all characters of second string ; Min . operations = j ; If second string is empty , only option is to remove all characters of first string ; Min . operations = i ; If last characters are same , ignore last character and recur for remaining string ; If last character are different , remove it and find minimum ; Remove from str1 Remove from str2 ; Returns true if str is k palindrome . ;  ; Driver program"
Solution in C#:

using System ; class GFG { static int isKPalDP ( string str1 , string str2 , int m , int n ) { int [ , ] dp = new int [ m + 1 , n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) { dp [ i , j ] = j ; } else if ( j == 0 ) { dp [ i , j ] = i ; } else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) { dp [ i , j ] = dp [ i - 1 , j - 1 ] ; } else { dp [ i , j ] = 1 + Math . Min ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } } return dp [ m , n ] ; } static bool isKPal ( string str , int k ) { string revStr = str ; revStr = reverse ( revStr ) ; int len = str . Length ; return ( isKPalDP ( str , revStr , len , len ) <= k * 2 ) ; } static string reverse ( string str ) { char [ ] sb = str . ToCharArray ( ) ; Array . Reverse ( sb ) ; return new string ( sb ) ; } static void Main ( ) { string str = " acdcb " ; int k = 2 ; if ( isKPal ( str , k ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Find if the Vacation can be taken or not | C # program to find if the Vacation can be taken or not ; Function to find if the Vacation is possible or not ; Find the required number of hours of study ; find the hours of study that can be done if the vacation is taken ; check if the required hours are less than or equal to the hours of study that can be done if the vacation is taken ; Driver Code"
Solution in C#:

 using System ; class GFG { static int isPossible ( int N , int S , int C , int H , int L , int T ) { int total_time_required = S * C * H ; int available_time_after_vacation = ( N - L ) * T ; if ( available_time_after_vacation >= total_time_required ) return 1 ; return 0 ; } public static void Main ( String [ ] args ) { int N = 12 , S = 5 , C = 8 , H = 3 , L = 2 , T = 20 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; N = 1 ; S = 2 ; C = 3 ; H = 4 ; L = 5 ; T = 6 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } }


"Find if the given number is present in the infinite sequence or not | C # implementation of the approach ; Function that returns true if the sequence will contain B ; Driver code"
Solution in C#:

using System ; class GFG { static bool doesContainB ( int a , int b , int c ) { if ( a == b ) { return true ; } if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) { return true ; } return false ; } public static void Main ( ) { int a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Find if the glass will be empty or not when the rate of drinking is given | C # implementation of the approach ; Function to return the time when the glass will be empty ; Check the condition when the glass will never be empty ; Find the time ; Driver code"
Solution in C#:

using System ; class GFG { static double pie = 3.1415926535897 ; static double findsolution ( double d , double h , double m , double n ) { double k = ( 4 * m ) / ( pie * d * d ) ; if ( n > k ) return - 1 ; double ans = ( h / ( k - n ) ) ; return ans ; } public static void Main ( String [ ] args ) { double d = 1 , h = 1 , m = 1 , n = 1 ; Console . Write ( " { 0 : F5 } " , findsolution ( d , h , m , n ) ) ; } }


"Find if there exists a direction for ranges such that no two range intersect | C # implementation of the approach ; Structure to hold details of each interval ; Function that returns true if the assignment of directions is possible ; Sort the intervals based on velocity ; Test the condition for all intervals with same velocity ; If for any velocity , 3 or more intervals share a common point return false ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public class interval { public int l , r , v ; } public class sortHelper : IComparer < interval > { public int Compare ( interval a , interval b ) { return ( a . v < b . v ? 1 : 0 ) ; } } static int MAX = 100001 ; static bool isPossible ( int [ , ] range , int N ) { interval [ ] test = new interval [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { test [ i ] = new interval ( ) ; test [ i ] . l = range [ i , 0 ] ; test [ i ] . r = range [ i , 1 ] ; test [ i ] . v = range [ i , 2 ] ; } Array . Sort ( test , new sortHelper ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] count = new int [ MAX ] ; int current_velocity = test [ i ] . v ; int j = i ; while ( j < N && test [ j ] . v == current_velocity ) { for ( int k = test [ j ] . l ; k <= test [ j ] . r ; k ++ ) { count [ k ] ++ ; if ( count [ k ] >= 3 ) return false ; } j ++ ; } i = j - 1 ; } return true ; } public static void Main ( string [ ] args ) { int [ , ] range = { { 1 , 2 , 3 } , { 2 , 5 , 1 } , { 3 , 10 , 1 } , { 4 , 4 , 1 } , { 5 , 7 , 10 } } ; int n = range . GetLength ( 0 ) ; if ( isPossible ( range , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | C # implementation of the approach ; Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int w , int h , int x , int y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } static public void Main ( ) { int w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Find if there is a pair in root to a leaf path with sum equals to root 's data | C # program to find if there is a pair in any root to leaf path with sum equals to root 's key. ; A binary tree node has data , pointer to left child and a pointer to right child ; utility that allocates a new node with the given data and null left and right pointers . ; Function to print root to leaf path which satisfies the condition ; Base condition ; Check if current node makes a pair with any of the existing elements in set . ; Insert current node in set ; If result returned by either left or right child is true , return true . ; Remove current node from hash table ; A wrapper over printPathUtil ( ) ; create an empty hash table ; Recursively check in left and right subtrees . ; Driver code"
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 node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static bool printPathUtil ( Node node , HashSet < int > s , int root_data ) { if ( node == null ) return false ; int rem = root_data - node . data ; if ( s . Contains ( rem ) ) return true ; s . Add ( node . data ) ; bool res = printPathUtil ( node . left , s , root_data ) || printPathUtil ( node . right , s , root_data ) ; s . Remove ( node . data ) ; return res ; } static bool isPathSum ( Node root ) { HashSet < int > s = new HashSet < int > ( ) ; return printPathUtil ( root . left , s , root . data ) || printPathUtil ( root . right , s , root . data ) ; } public static void Main ( String [ ] args ) { Node root = newnode ( 8 ) ; root . left = newnode ( 5 ) ; root . right = newnode ( 4 ) ; root . left . left = newnode ( 9 ) ; root . left . right = newnode ( 7 ) ; root . left . right . left = newnode ( 1 ) ; root . left . right . right = newnode ( 12 ) ; root . left . right . right . right = newnode ( 2 ) ; root . right . right = newnode ( 11 ) ; root . right . right . left = newnode ( 3 ) ; Console . Write ( isPathSum ( root ) == true ? " Yes " : " No " ) ; } }


"Find if there is a path between two vertices in a directed graph | Set 2 | C # program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; mat matrix ; set mat [ i , j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code"
Solution in C#:

 using System ; class GFG { static readonly int X = 6 ; static readonly int Z = 2 ; static bool existPath ( int V , int [ , ] edges , int u , int v ) { bool [ , ] mat = new bool [ V , V ] ; for ( int i = 0 ; i < X ; i ++ ) mat [ edges [ i , 0 ] , edges [ i , 1 ] ] = true ; for ( int k = 0 ; k < V ; k ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { mat [ i , j ] = mat [ i , j ] || mat [ i , k ] && mat [ k , j ] ; } } } if ( u >= V v >= V ) { return false ; } if ( mat [ u , v ] ) return true ; return false ; } public static void Main ( String [ ] args ) { int V = 4 ; int [ , ] edges = { { 0 , 2 } , { 0 , 1 } , { 1 , 2 } , { 2 , 3 } , { 2 , 0 } , { 3 , 3 } } ; int u = 1 , v = 3 ; if ( existPath ( V , edges , u , v ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } }


"Find if two given Quadratic equations have common roots or not | C # Program to Find if two given quadratic equations have common roots or not ; Function to check if 2 quadratic equations have common roots or not . ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkSolution ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { return ( ( a1 / a2 ) == ( b1 / b2 ) && ( b1 / b2 ) == ( c1 / c2 ) ) ; } public static void Main ( string [ ] args ) { float a1 = 1 , b1 = - 5 , c1 = 6 ; float a2 = 2 , b2 = - 10 , c2 = 12 ; if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find if two people ever meet after same number of jumps | C # program to find any one of them can overtake the other ; function to find if any one of them can overtake the other ; Since starting points are always different , they will meet if following conditions are met . ( 1 ) Speeds are not same ( 2 ) Difference between speeds divide the total distance between initial points . ; Driver code"
Solution in C#:

using System ; class GFG { static bool sackRace ( int p1 , int s1 , int p2 , int s2 ) { return ( ( s1 > s2 && ( p2 - p1 ) % ( s1 - s2 ) == 0 ) || ( s2 > s1 && ( p1 - p2 ) % ( s2 - s1 ) == 0 ) ) ; } public static void Main ( ) { int p1 = 4 , s1 = 4 , p2 = 8 , s2 = 2 ; if ( sackRace ( p1 , s1 , p2 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C # implementation of the above approach ; Function that returns true if the sub - String starting from index i and ending at index j is a palindrome ; Function to get the required index ; Start comparing the two Strings from both ends . ; Break from the loop at first mismatch ; If it is possible to concatenate the Strings to form palindrome , return index ; If remaining part for s2 is palindrome ; If remaining part for s1 is palindrome ; If not possible , return - 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( string s , int i , int j ) { while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( string s1 , string s2 , int len ) { int i = 0 , j = len - 1 ; while ( i < j ) { if ( s1 [ i ] != s2 [ j ] ) { break ; } i ++ ; j -- ; } if ( i == j ) { return i - 1 ; } else if ( isPalindrome ( s2 , i , j ) ) return i - 1 ; else if ( isPalindrome ( s1 , i , j ) ) return j ; return - 1 ; } public static void Main ( ) { string s1 = " abcdf " , s2 = " sfgba " ; int len = s1 . Length ; Console . WriteLine ( getIndex ( s1 , s2 , len ) ) ; } }


"Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | C # implementation of the approach ; Function that returns true if s is palindrome ; Function to return the required index ; Copy the ith character in S ; Copy all the character of string s2 in Temp ; Check whether the string is palindrome ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( string s ) { int i = 0 ; int j = s . Length - 1 ; while ( i < j ) { if ( s [ i ] != s [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( string S1 , string S2 , int n ) { string S = " " ; for ( int i = 0 ; i < n ; i ++ ) { S = S + S1 [ i ] ; string Temp = " " ; for ( int j = i + 1 ; j < n ; j ++ ) Temp += S2 [ j ] ; if ( isPalindrome ( S + Temp ) ) { return i ; } } return - 1 ; } public static void Main ( ) { string S1 = " abcdf " , S2 = " sfgba " ; int n = S1 . Length ; Console . WriteLine ( getIndex ( S1 , S2 , n ) ) ; } }


"Find index of an extra element present in one sorted array | C # code for above approach ; Function to find Index ; Calculating extra element ; returns index of extra element ; function return sum of array elements ; function return index of given element ; Driver Code"
Solution in C#:

using System ; class GFG { static int find_extra_element_index ( int [ ] arrA , int [ ] arrB ) { int extra_element = sum ( arrA ) - sum ( arrB ) ; return indexOf ( arrA , extra_element ) ; } static int sum ( int [ ] arr ) { int sum = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int indexOf ( int [ ] arr , int element ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == element ) { return i ; } } return - 1 ; } public static void Main ( String [ ] args ) { int [ ] arrA = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arrB = { 2 , 4 , 6 , 8 , 10 , 12 } ; Console . WriteLine ( find_extra_element_index ( arrA , arrB ) ) ; } }


"Find index of an extra element present in one sorted array | C # program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Driver code ; Solve is passed both arrays"
Solution in C#:

using System ; class GfG { static int findExtra ( int [ ] arr1 , int [ ] arr2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } public static void Main ( ) { int [ ] arr1 = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arr2 = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . Length ; Console . Write ( findExtra ( arr1 , arr2 , n ) ) ; } }


"Find index of an extra element present in one sorted array | C # program to find an extra element present in arr1 [ ] ; Returns index of extra element in arr1 [ ] . n is size of arr2 [ ] . Size of arr1 [ ] is n - 1. ; Initialize result ; left and right are end points denoting the current range . ; If middle element is same of both arrays , it means that extra element is after mid so we update left to mid + 1 ; If middle element is different of the arrays , it means that the index we are searching for is either mid , or before mid . Hence we update right to mid - 1. ; when right is greater than left our search is complete . ; Driver Code ; Solve is passed both arrays"
Solution in C#:

using System ; class GFG { static int findExtra ( int [ ] arr1 , int [ ] arr2 , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } public static void Main ( ) { int [ ] arr1 = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int [ ] arr2 = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . Length ; Console . Write ( findExtra ( arr1 , arr2 , n ) ) ; } }


"Find index of closing bracket for a given opening bracket in an expression | C # program to find index of closing bracket for given opening bracket . ; Function to find index of closing bracket for given opening bracket . ; If index given is invalid and is not an opening bracket . ; Stack to store opening brackets . ; Traverse through string starting from given index . ; If current character is an opening bracket push it in stack . ; If current character is a closing bracket , pop from stack . If stack is empty , then this closing bracket is required bracket . ; If no matching closing bracket is found . ; Driver Code ; should be 8 ; should be 7 ; should be 12 ; No matching bracket"
Solution in C#:

 using System ; using System . Collections ; public class GFG { static void test ( String expression , int index ) { int i ; if ( expression [ index ] != ' [ ' ) { Console . Write ( expression + " , ▁ " + index + " : ▁ - 1
 " ) ; return ; } Stack st = new Stack ( ) ; for ( i = index ; i < expression . Length ; i ++ ) { if ( expression [ i ] == ' [ ' ) { st . Push ( ( int ) expression [ i ] ) ; } else if ( expression [ i ] == ' ] ' ) { st . Pop ( ) ; if ( st . Count == 0 ) { Console . Write ( expression + " , ▁ " + index + " : ▁ " + i + "
 " ) ; return ; } } } Console . Write ( expression + " , ▁ " + index + " : ▁ - 1
 " ) ; } public static void Main ( ) { test ( " [ ABC [ 23 ] ] [89 ] " , 0 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 4 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 9 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 1 ) ; } }


"Find index of first occurrence when an unsorted array is sorted | C # program to find index of first occurrence of x when array is sorted . ; Driver main"
Solution in C#:

using System ; public class GFG { static int findFirst ( int [ ] arr , int n , int x ) { int count = 0 ; bool isX = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { isX = true ; } else if ( arr [ i ] < x ) { count ++ ; } } return ( isX == false ) ? - 1 : count ; } public static void Main ( ) { int x = 20 ; int [ ] arr = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . Length ; Console . WriteLine ( findFirst ( arr , n , x ) ) ; } }


"Find index of first occurrence when an unsorted array is sorted | C # program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ;  ; Driver Code"
Solution in C#:

using System ; class GFG { static int findFirst ( int [ ] arr , int n , int x ) { Array . Sort ( arr ) ; int ptr = lowerBound ( arr , 0 , n , x ) ; return ( arr [ ptr ] != x ) ? - 1 : ( ptr ) ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static public void Main ( ) { int x = 20 ; int [ ] arr = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . Length ; Console . Write ( findFirst ( arr , n , x ) ) ; } }


"Find index of the element differing in parity with all other array elements | C # program for the above approach ; Function to print the array element which differs in parity with the remaining array elements ; Multimaps to store even and odd numbers along with their indices ; Traverse the array ; If array element is even ; Otherwise ; If only one even element is present in the array ; If only one odd element is present in the array ; Driver Code ; Given array ; Size of the array"
Solution in C#:

using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void OddOneOut ( int [ ] arr , int N ) { Dictionary < int , int > e = new Dictionary < int , int > ( ) ; Dictionary < int , int > o = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { e [ arr [ i ] ] = i ; } else { o [ arr [ i ] ] = i ; } } if ( e . Count == 1 ) { Console . Write ( e . First ( ) . Value ) ; } else { Console . Write ( o . First ( ) . Value ) ; } } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . Length ; OddOneOut ( arr , N ) ; } }


"Find index of the element differing in parity with all other array elements | C # program for the above approach ; Function to print the element which differs in parity ; Stores the count of odd and even array elements encountered ; Stores the indices of the last odd and even array elements encountered ; Traverse the array ; If array element is even ; Otherwise ; If only one odd element is present in the array ; If only one even element is present in the array ; Driver code ; Given array ; Size of the array"
Solution in C#:

using System ; class GFG { static void oddOneOut ( int [ ] arr , int N ) { int odd = 0 , even = 0 ; int lastOdd = 0 , lastEven = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { Console . WriteLine ( lastOdd ) ; } else { Console . WriteLine ( lastEven ) ; } } static void Main ( ) { int [ ] arr = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . Length ; oddOneOut ( arr , N ) ; } }


"Find indices of all local maxima and local minima in an Array | C # program for the above approach ; Function to find all the local maxima and minima in the given array arr [ ] ; Empty vector to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or none ; Iterating over all points to check local maxima and local minima ; Condition for local minima ; Condition for local maxima ; Checking whether the last point is local maxima or minima or none ; Print all the local maxima and local minima indexes stored ; Driver code ; Given array arr [ ] ; Function call"
Solution in C#:

 using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static void findLocalMaximaMinima ( int n , int [ ] arr ) { ArrayList mx = new ArrayList ( ) ; ArrayList mn = new ArrayList ( ) ; if ( arr [ 0 ] > arr [ 1 ] ) mx . Add ( 0 ) ; else if ( arr [ 0 ] < arr [ 1 ] ) mn . Add ( 0 ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] > arr [ i ] ) && ( arr [ i ] < arr [ i + 1 ] ) ) mn . Add ( i ) ; else if ( ( arr [ i - 1 ] < arr [ i ] ) && ( arr [ i ] > arr [ i + 1 ] ) ) mx . Add ( i ) ; } if ( arr [ n - 1 ] > arr [ n - 2 ] ) mx . Add ( n - 1 ) ; else if ( arr [ n - 1 ] < arr [ n - 2 ] ) mn . Add ( n - 1 ) ; if ( mx . Count > 0 ) { Console . Write ( " Points ▁ of ▁ Local ▁ " + " maxima ▁ are ▁ : ▁ " ) ; foreach ( int a in mx ) Console . Write ( a + " ▁ " ) ; Console . Write ( "
 " ) ; } else Console . Write ( " There ▁ are ▁ no ▁ points ▁ " + " of ▁ Local ▁ Maxima ▁ " ) ; if ( mn . Count > 0 ) { Console . Write ( " Points ▁ of ▁ Local ▁ " + " minima ▁ are ▁ : ▁ " ) ; foreach ( int a in mn ) Console . Write ( a + " ▁ " ) ; Console . Write ( "
 " ) ; } else Console . Write ( " There ▁ are ▁ no ▁ points ▁ of ▁ " + " Local ▁ Maxima ▁ " ) ; } public static void Main ( string [ ] args ) { int N = 9 ; int [ ] arr = { 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 } ; findLocalMaximaMinima ( N , arr ) ; } }


"Find indices of all occurrence of one string in other | C # program to find indices of all occurrences of one String in other . ; Driver code"
Solution in C#:

using System ; class GFG { static void printIndex ( String str , String s ) { bool flag = false ; for ( int i = 0 ; i < str . Length - s . Length + 1 ; i ++ ) { if ( str . Substring ( i , s . Length ) . Equals ( s ) ) { Console . Write ( i + " ▁ " ) ; flag = true ; } } if ( flag == false ) { Console . WriteLine ( " NONE " ) ; } } public static void Main ( String [ ] args ) { String str1 = " GeeksforGeeks " ; String str2 = " Geeks " ; printIndex ( str1 , str2 ) ; } }


"Find initial integral solution of Linear Diophantine equation if finite solution exists | C # program for the above approach ; Function to implement the extended euclid algorithm ; Base Case ; Recursively find the gcd ; Function to print the solutions of the given equations ax + by = c ; Condition for infinite solutions ; Condition for no solutions exist ; Condition for no solutions exist ; Print the solution ; Driver Code ; Given coefficients ; Function call"
Solution in C#:

 using System ; class GFG { static int x , y ; static int gcd_extend ( int a , int b ) { if ( b == 0 ) { x = 1 ; y = 0 ; return a ; } else { int g = gcd_extend ( b , a % b ) ; int x1 = x , y1 = y ; x = y1 ; y = x1 - ( a / b ) * y1 ; return g ; } } static void print_solution ( int a , int b , int c ) { if ( a == 0 && b == 0 ) { if ( c == 0 ) { Console . Write ( " Infinite ▁ Solutions ▁ " + " Exist " + "
 " ) ; } else { Console . Write ( " No ▁ Solution ▁ exists " + "
 " ) ; } } int gcd = gcd_extend ( a , b ) ; if ( c % gcd != 0 ) { Console . Write ( " No ▁ Solution ▁ exists " + "
 " ) ; } else { Console . Write ( " x ▁ = ▁ " + x * ( c / gcd ) + " , ▁ y ▁ = ▁ " + y * ( c / gcd ) + "
 " ) ; } } public static void Main ( String [ ] args ) { int a , b , c ; a = 4 ; b = 18 ; c = 10 ; print_solution ( a , b , c ) ; } }


"Find initial sequence that produces a given Array by cyclic increments upto index P | C # program to implement the above approach ; Function to generate and return the required initial arrangement ; Store the minimum element in the array ; Store the number of increments ; Subtract mi - 1 from every index ; Start from the last index which had been incremented ; Stores the index chosen to distribute its element ; Traverse the array cyclically and find the index whose element was distributed ; If any index has its value reduced to 0 ; Index whose element was distributed ; Store the number of increments at the starting index ; Print the original array ; Driver Code"
Solution in C#:

using System ; using System . Linq ; class GFG { static void findArray ( int [ ] a , int n , int P ) { int mi = a . Min ( ) ; int ctr = 0 ; mi = Math . Max ( 0 , mi - 1 ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] -= mi ; ctr += mi ; } i = P - 1 ; int start = - 1 ; while ( true ) { if ( a [ i ] == 0 ) { start = i ; break ; } a [ i ] -= 1 ; ctr += 1 ; i = ( i - 1 + n ) % n ; } a [ start ] = ctr ; for ( i = 0 ; i < n ; i ++ ) { Console . Write ( a [ i ] + " , ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 5 ; int P = 2 ; int [ ] arr = { 3 , 2 , 0 , 2 , 7 } ; findArray ( arr , N , P ) ; } }


"Find instances at end of time frame after auto scaling | C # program for above approach ; Function to find the number of instances after compintion ; Traverse the array , arr [ ] ; If current element is less than 25 ; Divide instances by 2 ; If the current element is greater than 60 ; Double the instances ; Print the instances at the end of the traversal ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static void finalInstances ( int instances , int [ ] arr ) { int i = 0 ; while ( i < arr . Length ) { if ( arr [ i ] < 25 && instances > 1 ) { instances = ( instances / 2 ) ; i = i + 10 ; } else if ( arr [ i ] > 60 && instances <= Math . Pow ( 10 , 8 ) ) { instances = instances * 2 ; i = i + 10 ; } i = i + 1 ; } Console . Write ( instances ) ; } static void Main ( ) { int instances = 2 ; int [ ] arr = { 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 } ; finalInstances ( instances , arr ) ; } }


"Find integers that divides maximum number of elements of the array | C # implementation of the approach ; Function to print the integers that divide the maximum number of elements from the array ; Initialize two lists to store rank and factors ; Start from 2 till the maximum element in arr ; Initialize a variable to count the number of elements it is a factor of ; Maximum rank in the rank list ; Print all the elements with rank m ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Linq ; class GFG { static void maximumFactor ( int [ ] arr ) { int [ ] rank = new int [ arr . Max ( ) + 1 ] ; int [ ] factors = new int [ arr . Max ( ) + 1 ] ; int g = 0 ; for ( int i = 2 ; i <= arr . Max ( ) ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . Length ; j ++ ) if ( arr [ j ] % i == 0 ) count += 1 ; rank [ g ] = count ; factors [ g ] = i ; g ++ ; } int m = rank . Max ( ) ; for ( int i = 0 ; i < rank . Length ; i ++ ) { if ( ( int ) rank [ i ] == m ) Console . Write ( factors [ i ] + " ▁ " ) ; } } static void Main ( ) { int [ ] arr = { 120 , 15 , 24 , 63 , 18 } ; maximumFactor ( arr ) ; } }


"Find integral points with minimum distance from given set of integers using BFS | C # implementation of above approach ; Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; List to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void minDistancePoints ( int [ ] A , int K , int n ) { Dictionary < int , Boolean > m = new Dictionary < int , Boolean > ( ) ; Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . Add ( A [ i ] , true ) ; q . Enqueue ( A [ i ] ) ; } List < int > ans = new List < int > ( ) ; while ( K > 0 ) { int x = q . Dequeue ( ) ; if ( ! m . ContainsKey ( x - 1 ) && K > 0 ) { m . Add ( x - 1 , true ) ; q . Enqueue ( x - 1 ) ; ans . Add ( x - 1 ) ; K -- ; } if ( ! m . ContainsKey ( x + 1 ) && K > 0 ) { m . Add ( x + 1 , true ) ; q . Enqueue ( x + 1 ) ; ans . Add ( x + 1 ) ; K -- ; } } foreach ( int i in ans ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = new int [ ] { - 1 , 4 , 6 } ; int K = 3 ; int n = A . Length ; minDistancePoints ( A , K , n ) ; } }


"Find interior angles for each side of a given Cyclic Quadrilateral | C # program for the above approach ; Function to find the interior angles of the cyclic quadrilateral ; Stores the numerator and the denominator to find angle A ; Stores the numerator and the denominator to find angle B ; Stores the numerator and the denominator to find angle C : ; Stores the numerator and the denominator to find angle D : ; Driver Code"
Solution in C#:

using System ; class GFG { static void findAngles ( double a , double b , double c , double d ) { double numerator = a * a + d * d - b * b - c * c ; double denominator = 2 * ( a * b + c * d ) ; double x = numerator / denominator ; Console . WriteLine ( " A : ▁ " + Math . Round ( ( ( Math . Acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = a * a + b * b - c * c - d * d ; x = numerator / denominator ; Console . WriteLine ( " B : ▁ " + Math . Round ( ( ( Math . Acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = c * c + b * b - a * a - d * d ; x = numerator / denominator ; Console . WriteLine ( " C : ▁ " + Math . Round ( ( ( Math . Acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = d * d + c * c - a * a - b * b ; x = numerator / denominator ; Console . WriteLine ( " D : ▁ " + Math . Round ( ( ( Math . Acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; } public static void Main ( string [ ] args ) { double A = 10 , B = 15 , C = 20 , D = 25 ; findAngles ( A , B , C , D ) ; } }


"Find iâ €™ th index character in a binary string obtained after n iterations | Set 2 | C # program to find ith Index character in a binary string obtained after n iterations ; Function to find the i - th character ; distance between two consecutive elements after N iterations ; binary representation of M ; kth digit will be derived from root for sure ; Check whether there is need to flip root or not ; Driver Code"
Solution in C#:

using System ; class GFG { static void KthCharacter ( int m , int n , int k ) { int distance = ( int ) Math . Pow ( 2 , n ) ; int Block_number = k / distance ; int remaining = k % distance ; int [ ] s = new int [ 32 ] ; int x = 0 ; for ( ; m > 0 ; x ++ ) { s [ x ] = m % 2 ; m = m / 2 ; } int root = s [ x - 1 - Block_number ] ; if ( remaining == 0 ) { Console . WriteLine ( root ) ; return ; } Boolean flip = true ; while ( remaining > 1 ) { if ( ( remaining & 1 ) > 0 ) { flip = ! flip ; } remaining = remaining >> 1 ; } if ( flip ) { Console . WriteLine ( ! ( root > 0 ) ) ; } else { Console . WriteLine ( root ) ; } } public static void Main ( ) { int m = 5 , k = 5 , n = 3 ; KthCharacter ( m , n , k ) ; } }