"Check if count of divisors is even or odd | C # program using Naive Solution to find if count of divisors is even or odd ; Function to count the divisors ; Initialize count of divisors ; Note that this loop runs till square root ; If divisors are equal increment count by one Otherwise increment count by 2 ; Driver code"
Solution in C#:

using System ; class GFG { static void countDivisors ( int n ) { int count = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) count += ( n / i == i ) ? 1 : 2 ; } if ( count % 2 == 0 ) Console . Write ( " Even " ) ; else Console . Write ( " Odd " ) ; } public static void Main ( ) { Console . Write ( " The ▁ count ▁ of ▁ divisor : ▁ " ) ; countDivisors ( 10 ) ; } }


"Check if count of even divisors of N is equal to count of odd divisors | C # code for the above program ; Function to check if count of even and odd divisors are equal ; If ( n - 2 ) % 4 is an integer , then return true else return false ; Driver code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static bool divisorsSame ( int n ) { return ( n - 2 ) % 4 == 0 ; } public static void Main ( ) { int N = 6 ; if ( divisorsSame ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Check if count of even divisors of N is equal to count of odd divisors | C # code for the above program ; Function to check if count of even and odd divisors are equal ; To store the count of even factors and odd factors ; Loop till [ 1 , sqrt ( N ) ] ; If divisors are equal add only one ; Check for even divisor ; Odd divisor ; Check for both divisor i . e . , i and N / i ; Check if i is odd or even ; Check if N / i is odd or even ; Return true if count of even_div and odd_div are equals ; Driver code ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static bool divisorsSame ( int n ) { int even_div = 0 , odd_div = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } else { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } if ( n / i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } } } return ( even_div == odd_div ) ; } public static void Main ( ) { int N = 6 ; if ( divisorsSame ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Check if count of substrings in S with string S1 as prefix and S2 as suffix is equal to that with S2 as prefix and S1 as suffix | C # program for the above approach ; Function to count number of substrings that starts with string S1 and ends with string S2 ; Stores the length of each string ; Stores the count of prefixes as S1 and suffixes as S2 ; Traverse string S ; Find the prefix at index i ; Find the suffix at index i ; If the prefix is S1 ; If the suffix is S2 ; Return the count of substrings ; Function to check if the number of substrings starts with S1 and ends with S2 and vice - versa is same or not ; Count the number of substrings ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubstrings ( string S , string S1 , string S2 ) { int N = S . Length ; int N1 = S1 . Length ; int N2 = S2 . Length ; int count = 0 , totalcount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String prefix = S . Substring ( i , ( i + N1 < N ) ? N1 : ( N - i ) ) ; String suffix = S . Substring ( i , ( i + N2 < N ) ? N2 : ( N - i ) ) ; if ( S1 . Equals ( prefix ) ) count ++ ; if ( S2 . Equals ( suffix ) ) totalcount += count ; } return totalcount ; } static void checkSubstrings ( string S , string S1 , string S2 ) { int x = countSubstrings ( S , S1 , S2 ) ; int y = countSubstrings ( S , S2 , S1 ) ; if ( x == y ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } static void Main ( ) { string S = " opencloseopencloseopen " ; string S1 = " open " ; string S2 = " close " ; checkSubstrings ( S , S1 , S2 ) ; } }


"Check if diagonal elements of a Matrix are Prime or not | C # program for the above approach ; Stores if a number is prime or not ; Function to generate and store primes using Sieve Of Eratosthenes ; Set all numbers as prime ; If p is a prime ; Set all its multiples as non - prime ; Function to check if all diagonal elements are prime or not ; Stores if all diagonal elements are prime or not ; Precompute primes ; Traverse the range [ 0 , N - 1 ] ; Check if numbers on the cross diagonal and main diagonal are primes or not ; If true , then print " Yes " ; Otherwise , print " No " ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static bool [ ] prime = new bool [ 1000005 ] ; static void SieveOfEratosthenes ( int N ) { Array . Fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= N ; i += p ) prime [ i ] = false ; } } } static void checkElementsOnDiagonal ( int [ , ] M , int N ) { int flag = 1 ; SieveOfEratosthenes ( 1000000 ) ; for ( int i = 0 ; i < N ; i ++ ) { bool flg = ( bool ) ( prime [ M [ i , i ] ] && prime [ M [ i , N - 1 - i ] ] ) ; int val = ( flg ) ? 1 : 0 ; flag &= val ; } if ( flag != 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int [ , ] M = { { 1 , 2 , 3 , 13 } , { 5 , 3 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 7 } } ; int N = M . GetLength ( 0 ) ; checkElementsOnDiagonal ( M , N ) ; } }


"Check if digit cube limit of an integer arrives at fixed point or a limit cycle | C # program for the above approach ; Define the limit ; Function to get the sum of cube of digits of a number ; Convert to String to get sum of the cubes of its digits ; return sum ; Function to check if the number arrives at a fixed point or a cycle ; Stores the values obtained ; Insert N to set s ; Get the next number using F ( N ) ; Check if the next number is repeated or not ; Function to check if digit cube limit of an integer arrives at fixed point or in a limit cycle ; N is a non negative integer ; Function Call ; If the value received is greater than limit ; If the value received is an Armstrong number ; Driver Code ; Function Call"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static readonly int limit = 1000000000 ; static int F ( int N ) { String str = String . Join ( " " , N ) ; int sum = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { int val = ( int ) ( str [ i ] - '0' ) ; sum += val * val * val ; } return sum ; } static int findDestination ( int N ) { HashSet < int > s = new HashSet < int > ( ) ; int prev = N , next = 0 ; s . Add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . Contains ( next ) ) { return next ; } prev = next ; s . Add ( prev ) ; N = next ; } return next ; } static void digitCubeLimit ( int N ) { if ( N < 0 ) Console . Write ( " N ▁ cannot ▁ be ▁ negative
 " ) ; else { int ans = findDestination ( N ) ; if ( ans > limit ) Console . Write ( " Limit ▁ exceeded
 " ) ; else if ( ans == F ( ans ) ) { Console . Write ( N ) ; Console . Write ( " ▁ reaches ▁ to ▁ a " + " ▁ fixed ▁ point : ▁ " ) ; Console . Write ( ans ) ; } else { Console . Write ( N ) ; Console . Write ( " ▁ reaches ▁ to ▁ a " + " ▁ limit ▁ cycle : ▁ " ) ; Console . Write ( ans ) ; } } } public static void Main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }


"Check if each element of an Array is the Sum of any two elements of another Array | C # program to implement the above approach ; Function to check if each element of [ ] B can be formed by adding two elements of array [ ] A ; Store each element of [ ] B ; Traverse all possible pairs of array ; If A [ i ] + A [ j ] is present in the set ; Remove A [ i ] + A [ j ] from the set ; If set is empty ; Otherwise ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String checkPossible ( int [ ] A , int [ ] B , int n ) { HashSet values = new HashSet ( ) ; for ( int i = 0 ; i < n ; i ++ ) { values . Add ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . Contains ( A [ i ] + A [ j ] ) ) { values . Remove ( A [ i ] + A [ j ] ) ; if ( values . Count == 0 ) break ; } } } if ( values . Count == 0 ) return " Yes " ; else return " No " ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] A = { 3 , 5 , 1 , 4 , 2 } ; int [ ] B = { 3 , 4 , 5 , 6 , 7 } ; Console . Write ( checkPossible ( A , B , N ) ) ; } }


"Check if each element of the given array is the product of exactly K prime numbers | C # program to check if each element of the given array is a product of exactly K prime factors ; initialise the global sieve array ; Function to generate Sieve ; NOTE : k value is necessarily more than 1 hence , 0 , 1 and any prime number cannot be represented as product of two or more prime numbers ; Function to check if each number of array satisfies the given condition ; Driver Code ; first construct the sieve"
Solution in C#:

using System ; class GFG { static int MAX = 1000000 ; static int [ ] Sieve = new int [ MAX + 1 ] ; static void constructSieve ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( int j = 2 * i ; j <= MAX ; j += i ) { int temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = temp / i ; } } } } } static void checkElements ( int [ ] A , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " No " ) ; } } } public static void Main ( ) { constructSieve ( ) ; int k = 3 ; int [ ] A = { 12 , 36 , 42 , 72 } ; int n = A . Length ; checkElements ( A , n , k ) ; } }


"Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; Driver code"
Solution in C#:

using System ; public class BinaryTree { bool hasOnlyOneChild ( int [ ] pre , int size ) { int nextDiff , lastDiff ; for ( int i = 0 ; i < size - 1 ; i ++ ) { nextDiff = pre [ i ] - pre [ i + 1 ] ; lastDiff = pre [ i ] - pre [ size - 1 ] ; if ( nextDiff * lastDiff < 0 ) { return false ; } ; } return true ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int [ ] pre = new int [ ] { 8 , 3 , 5 , 7 , 6 } ; int size = pre . Length ; if ( tree . hasOnlyOneChild ( pre , size ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; Initialize min and max using last two elements ; Every element must be either smaller than min or greater than max ; Driver code"
Solution in C#:

using System ; public class BinaryTree { bool hasOnlyOneChild ( int [ ] pre , int size ) { int min , max ; if ( pre [ size - 1 ] > pre [ size - 2 ] ) { max = pre [ size - 1 ] ; min = pre [ size - 2 ] ; } else { max = pre [ size - 2 ] ; min = pre [ size - 1 ] ; } for ( int i = size - 3 ; i >= 0 ; i -- ) { if ( pre [ i ] < min ) { min = pre [ i ] ; } else if ( pre [ i ] > max ) { max = pre [ i ] ; } else { return false ; } } return true ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int [ ] pre = new int [ ] { 8 , 3 , 5 , 7 , 6 } ; int size = pre . Length ; if ( tree . hasOnlyOneChild ( pre , size ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if edit distance between two strings is one | C # program to check if given two strings are at distance one . ; Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't  be at one distance ; Count of edits ; If current characters don 't match ; If length of one string is more , then only possible edit is to remove a character ; If lengths of both strings is same ; Increment count of edits ; If current characters match ; If last character is extra in any string ; Driver code"
Solution in C#:

using System ; class GFG { static bool isEditDistanceOne ( String s1 , String s2 ) { int m = s1 . Length , n = s2 . Length ; if ( Math . Abs ( m - n ) > 1 ) return false ; int count = 0 ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( s1 [ i ] != s2 [ j ] ) { if ( count == 1 ) return false ; if ( m > n ) i ++ ; else if ( m < n ) j ++ ; else { i ++ ; j ++ ; } count ++ ; } else { i ++ ; j ++ ; } } if ( i < m < n ) count ++ ; return count == 1 ; } public static void Main ( ) { String s1 = " gfg " ; String s2 = " gf " ; if ( isEditDistanceOne ( s1 , s2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if elements of a Binary Matrix can be made alternating | C # program for the above approach ; Function to check for the grid in one of the alternating ways ; Iterate over the range ; Iterate over the range ; ( i + j ) % 2 == 1 cells should be with the character check and the rest should be with the other character ; Function to fill the grid in a possible way ; Function to find if the grid can be made alternating ; If grid contains atleast one 1 or 0 ; Fill the grid in any possible way ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool PosCheck ( char [ , ] a , int n , int m , char check ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] == ' * ' ) { continue ; } else { if ( ( ( ( i + j ) & 1 ) == 1 ) && a [ i , j ] != check ) { return false ; } if ( ! ( ( ( i + j ) & 1 ) == 1 ) && a [ i , j ] == check ) { return false ; } } } } return true ; } public static void fill ( char [ , ] a , int n , int m , char odd , char even ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( ( i + j ) & 1 ) == 1 ) { a [ i , j ] = odd ; } else { a [ i , j ] = even ; } } } } public static void findPossibleGrid ( int n , int m , char [ , ] a ) { bool flag = true ; int k = - 1 , o = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i , j ] != ' * ' ) { k = i ; o = j ; break ; } } if ( k != - 1 ) { break ; } } if ( k != - 1 ) { flag = PosCheck ( a , n , m , '1' ) ; if ( flag ) { fill ( a , n , m , '1' , '0' ) ; } else { flag = PosCheck ( a , n , m , '0' ) ; if ( flag ) { fill ( a , n , m , '0' , '1' ) ; } } } else { char h = '1' ; for ( int i = 0 ; i < m ; i ++ ) { a [ 0 , i ] = h ; if ( h == '1' ) { h = '0' ; } else { h = '1' ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( i - 1 < 0 ) { continue ; } if ( a [ i - 1 , j ] == '1' ) { a [ i , j ] = '0' ; } else { a [ i , j ] = '1' ; } } } flag = true ; } if ( ! flag ) { Console . WriteLine ( " No " ) ; } else { Console . WriteLine ( " Yes " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( a [ i , j ] ) ; } Console . WriteLine ( ) ; } } } public static void Main ( ) { int n = 4 , m = 4 ; char [ , ] grid = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( n , m , grid ) ; } }


"Check if elements of a Binary Matrix can be made alternating | C # program for the above approach ; Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver code"
Solution in C#:

using System ; class GFG { public static void createGrid ( char [ , ] grid , bool is1 , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( is1 ) { grid [ i , j ] = '0' ; is1 = false ; } else { grid [ i , j ] = '1' ; is1 = true ; } } if ( M % 2 == 0 ) is1 = ! is1 ; } } public static bool testGrid ( char [ , ] testGrid , char [ , ] Grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( Grid [ i , j ] != ' * ' ) { if ( Grid [ i , j ] != testGrid [ i , j ] ) { return false ; } } } } return true ; } public static void printGrid ( char [ , ] grid , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( grid [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void findPossibleGrid ( int N , int M , char [ , ] grid ) { char [ , ] gridTest1 = new char [ N , 1001 ] ; char [ , ] gridTest2 = new char [ N , 1001 ] ; createGrid ( gridTest1 , true , N , M ) ; createGrid ( gridTest2 , false , N , M ) ; if ( testGrid ( gridTest1 , grid , N , M ) ) { Console . WriteLine ( " Yes " ) ; printGrid ( gridTest1 , N , M ) ; } else if ( testGrid ( gridTest2 , grid , N , M ) ) { Console . WriteLine ( " Yes " ) ; printGrid ( gridTest2 , N , M ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int N = 4 , M = 4 ; char [ , ] grid = { { ' * ' , ' * ' , '1' , '0' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , ' * ' , ' * ' } , { ' * ' , ' * ' , '0' , '1' } } ; findPossibleGrid ( N , M , grid ) ; } }


"Check if elements of an array can be arranged satisfying the given condition | C # implementation of the approach ; Function to return true if the elements can be arranged in the desired order ; If 2 * x is not found to pair ; Remove an occurrence of x and an occurrence of 2 * x ; Driver code ; Function call to print required answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static String canReorder ( int [ ] A , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( A [ i ] ) ) m [ A [ i ] ] = m [ A [ i ] ] + 1 ; else m . Add ( A [ i ] , 1 ) ; } Array . Sort ( A ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ A [ i ] ] == 0 ) continue ; if ( m . ContainsKey ( 2 * A [ i ] ) ) { count += 2 ; m [ A [ i ] ] = m [ A [ i ] ] - 1 ; if ( m . ContainsKey ( 2 * A [ i ] ) ) m [ 2 * A [ i ] ] = m [ 2 * A [ i ] ] - 1 ; else m . Add ( 2 * A [ i ] , m [ 2 * A [ i ] ] - 1 ) ; } } if ( count == n ) return " True " ; else return " False " ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , - 2 , 2 , - 4 } ; int n = A . Length ; Console . WriteLine ( canReorder ( A , n ) ) ; } }


"Check if elements of array can be arranged in AP , GP or HP | C # program to check if a given array form AP , GP or HP ; Returns true if arr [ 0. . n - 1 ] can form AP ; Base Case ; Sort array ; After sorting , difference between consecutive elements must be same . ; Traverse the given array and check if the difference between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form GP ; Base Case ; Sort array ; After sorting , common ratio between consecutive elements must be same . ; Traverse the given array and check if the common ratio between ith element and ( i - 1 ) th element is same or not ; Returns true if arr [ 0. . n - 1 ] can form HP ; Base Case ; Find reciprocal of [ ] arr ; After finding reciprocal , check if the reciprocal is in A . P . To check for A . P . ; Driver 's Code ; Function to check AP ; Function to check GP ; Function to check HP"
Solution in C#:

 using System ; class GFG { static bool checkIsAP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != d ) { return false ; } } return true ; } static bool checkIsGP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } static bool checkIsHP ( double [ ] arr , int n ) { if ( n == 1 ) { return true ; } double [ ] rec = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } if ( checkIsAP ( rec , n ) ) return true ; else return false ; } public static void Main ( String [ ] args ) { double [ ] arr = { 1.0 / 5.0 , 1.0 / 10.0 , 1.0 / 15.0 , 1.0 / 20.0 } ; int n = arr . Length ; int flag = 0 ; if ( checkIsAP ( arr , n ) ) { Console . Write ( " Yes , ▁ An ▁ AP ▁ can ▁ be ▁ formed " + "
 " ) ; flag = 1 ; } if ( checkIsGP ( arr , n ) ) { Console . Write ( " Yes , ▁ A ▁ GP ▁ can ▁ be ▁ formed " + "
 " ) ; flag = 1 ; } if ( checkIsHP ( arr , n ) ) { Console . Write ( " Yes , ▁ A ▁ HP ▁ can ▁ be ▁ formed " + "
 " ) ; flag = 1 ; } else if ( flag == 0 ) { Console . Write ( " No " ) ; } } }


"Check if end of a sorted Array can be reached by repeated jumps of one more , one less or same number of indices as previous jump | C # program for the above approach ; Utility function to check if it is possible to move from index 1 to N - 1 ; Successfully reached end index ; memo [ i ] [ j ] is already calculated ; Check if there is any index having value of A [ i ] + j - 1 , A [ i ] + j or A [ i ] + j + 1 ; If A [ k ] > A [ i ] + j + 1 , can 't make a move further ; It 's possible to move A[k] ; Check is it possible to move from index k having previously taken A [ k ] - A [ i ] steps ; If yes then break the loop ; Store value of flag in memo ; Return memo [ i ] [ j ] ; Function to check if it is possible to move from index 1 to N - 1 ; Stores the memoized state ; Initialize all values as - 1 ; Initially , starting index = 1 ; Function call ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int N = 8 ; static int check ( int [ , ] memo , int i , int j , int [ ] A ) { if ( i == N - 1 ) return 1 ; if ( memo [ i , j ] != - 1 ) return memo [ i , j ] ; int flag = 0 , k ; for ( k = i + 1 ; k < N ; k ++ ) { if ( A [ k ] - A [ i ] > j + 1 ) break ; if ( A [ k ] - A [ i ] >= j - 1 && A [ k ] - A [ i ] <= j + 1 ) flag = check ( memo , k , A [ k ] - A [ i ] , A ) ; if ( flag != 0 ) break ; } memo [ i , j ] = flag ; return memo [ i , j ] ; } static void checkEndReach ( int [ ] A , int K ) { int [ , ] memo = new int [ N , N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { memo [ i , j ] = - 1 ; } } int startIndex = 1 ; if ( check ( memo , startIndex , K , A ) != 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int [ ] A = { 0 , 1 , 3 , 5 , 6 , 8 , 12 , 17 } ; int K = 1 ; checkEndReach ( A , K ) ; } }


"Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | C # program for the above approach ; Function to check if there is a node in tree from where all other nodes are accessible or not ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver code ; Given number of nodes ; Given Directed Tree"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { public static void findNode ( Dictionary < int , int > map , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . ContainsKey ( i + 1 ) ) a [ i ] = map [ i + 1 ] ; else a [ i ] = 0 ; } int count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= Math . Floor ( ( ( double ) n ) / ( ( double ) 2 ) ) ) { Console . WriteLine ( " Yes " ) ; } else Console . WriteLine ( " No ▁ " ) ; } static public void Main ( ) { int N = 3 ; Dictionary < int , int > map = new Dictionary < int , int > ( ) ; map [ 1 ] = 0 ; map [ 2 ] = 2 ; map [ 3 ] = 0 ; findNode ( map , N ) ; } }


"Check if every vertex triplet in graph contains two vertices connected to third vertex | C # program of the above approach ; Function to add edge into the graph ; Finding the maximum and minimum values in each component ; Function for checking whether the given graph is valid or not ; Checking for intersecting intervals ; If intersection is found ; Graph is not valid ; Function for the DFS Traversal ; Traversing for every vertex ; Storing maximum and minimum values of each component ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void addEdge ( ArrayList adj , int u , int v ) { ( ( ArrayList ) adj [ u ] ) . Add ( v ) ; ( ( ArrayList ) adj [ v ] ) . Add ( u ) ; } static void DFSUtil ( int u , ArrayList adj , bool [ ] visited , int componentMin , int componentMax ) { visited [ u ] = true ; componentMax = Math . Max ( componentMax , u ) ; componentMin = Math . Min ( componentMin , u ) ; for ( int i = 0 ; i < ( ( ArrayList ) adj [ u ] ) . Count ; i ++ ) if ( visited [ ( int ) ( ( ArrayList ) adj [ u ] ) [ i ] ] == false ) DFSUtil ( ( int ) ( ( ArrayList ) adj [ u ] ) [ i ] , adj , visited , componentMin , componentMax ) ; } static bool isValid ( ArrayList v ) { int MAX = - 1 ; bool ans = false ; foreach ( pair i in v ) { if ( i . first <= MAX ) { ans = true ; } MAX = Math . Max ( MAX , i . second ) ; } return ( ans == false ? true : false ) ; } static void DFS ( ArrayList adj , int V ) { ArrayList v = new ArrayList ( ) ; bool [ ] visited = new bool [ V + 1 ] ; for ( int u = 1 ; u <= V ; u ++ ) { if ( visited [ u ] == false ) { int componentMax = u ; int componentMin = u ; DFSUtil ( u , adj , visited , componentMin , componentMax ) ; v . Add ( new pair ( componentMin , componentMax ) ) ; } } bool check = isValid ( v ) ; if ( check ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; return ; } public static void Main ( string [ ] args ) { int N = 4 ; ArrayList adj = new ArrayList ( ) ; for ( int i = 0 ; i <= N + 1 ; i ++ ) adj . Add ( new ArrayList ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; DFS ( adj , N ) ; } }


"Check if expression contains redundant bracket or not | Set 2 | C # program to check for redundant braces in the string ; Function to check for redundant braces ; count of no of signs ; Driver Code"
Solution in C#:

using System ; class GFG { static bool IsRedundantBraces ( string A ) { int a = 0 , b = 0 ; for ( int i = 0 ; i < A . Length ; i ++ ) { if ( A [ i ] == ' ( ' && A [ i + 2 ] == ' ) ' ) return true ; if ( A [ i ] == ' * ' A [ i ] == ' + ' A [ i ] == ' - ' A [ i ] == ' / ' ) a ++ ; if ( A [ i ] == ' ( ' ) b ++ ; } if ( b > a ) return true ; return false ; } public static void Main ( String [ ] args ) { String A = " ( ( ( a + b ) ▁ + ▁ c ) ▁ + ▁ d ) " ; if ( IsRedundantBraces ( A ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if factorial of N is divisible by the sum of squares of first N natural numbers | C # implementation of the approach ; Function to count number of times prime P divide factorial N ; Lengendre Formula ; Function to find count number of times all prime P divide summation ; Formula for summation of square after removing n and constant 6 ; Loop to traverse over all prime P which divide summation ; If Number itself is a Prime Number ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkfact ( int N , int countprime , int prime ) { int countfact = 0 ; if ( prime == 2 prime == 3 ) countfact ++ ; int divide = prime ; while ( N / divide != 0 ) { countfact += N / divide ; divide = divide * divide ; } if ( countfact >= countprime ) return true ; else return false ; } static bool check ( int N ) { int sumsquares = ( N + 1 ) * ( 2 * N + 1 ) ; int countprime = 0 ; for ( int i = 2 ; i <= Math . Sqrt ( sumsquares ) ; i ++ ) { int flag = 0 ; while ( sumsquares % i == 0 ) { flag = 1 ; countprime ++ ; sumsquares /= i ; } if ( flag == 1 ) { if ( ! checkfact ( N - 1 , countprime , i ) ) return false ; countprime = 0 ; } } if ( sumsquares != 1 ) if ( ! checkfact ( N - 1 , 1 , sumsquares ) ) return false ; return true ; } public static void Main ( ) { int N = 5 ; if ( check ( N ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if final remainder is present in original Array by reducing it based on given conditions | C # program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO"
Solution in C#:

using System ; using System . Collections . Generic ; class GFg { static int Reduced ( List < int > a , int n ) { List < int > original_array = new List < int > ( a ) ; while ( a . Count != 2 ) { int mid = a . Count / 2 ; int mid_ele = a [ mid ] ; int start = a [ 0 ] ; a . RemoveAt ( 0 ) ; int end = a [ a . Count - 1 ] ; a . RemoveAt ( a . Count - 1 ) ; int rmd = ( start * end ) % mid_ele ; a . Add ( rmd ) ; int remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( int i = 0 ; i < original_array . Count ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } int x = Reduced ( Arr , N ) ; if ( x > 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if frequency of all characters can become same by one removal | C # program to get same frequency character string by removal of at most one char ; Utility method to get index of character ch in lower alphabet characters ; Returns true if all non - zero elements values are same ; get first non - zero element ; check equality of each element with variable same ; Returns true if we can make all character frequencies same ; fill frequency array ; if all frequencies are same , then return true ; Try decreasing frequency of all character by one and then check all equality of all non - zero frequencies ; Check character only if it occurs in str ; Driver code"
Solution in C#:

using System ; class GFG { static int M = 26 ; static int getIdx ( char ch ) { return ( ch - ' a ' ) ; } static bool allSame ( int [ ] freq , int N ) { int same = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( freq [ i ] > 0 ) { same = freq [ i ] ; break ; } } for ( int j = i + 1 ; j < N ; j ++ ) if ( freq [ j ] > 0 && freq [ j ] != same ) return false ; return true ; } static bool possibleSameCharFreqByOneRemoval ( string str ) { int l = str . Length ; int [ ] freq = new int [ M ] ; for ( int i = 0 ; i < l ; i ++ ) freq [ getIdx ( str [ i ] ) ] ++ ; if ( allSame ( freq , M ) ) return true ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { int i = getIdx ( c ) ; if ( freq [ i ] > 0 ) { freq [ i ] -- ; if ( allSame ( freq , M ) ) return true ; freq [ i ] ++ ; } } return false ; } public static void Main ( ) { string str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( str ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if frequency of each character is equal to its position in English Alphabet | C # program for the above approach ; Initialise frequency array ; Traverse the String ; Update the frequency ; Check for valid String ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Driver Code ; Given String str"
Solution in C#:

using System ; class GFG { static bool checkValidString ( String str ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . Length ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } public static void Main ( String [ ] args ) { String str = " abbcccdddd " ; if ( checkValidString ( str ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Check if frequency of each digit is less than the digit | C # program to validate a number ; Function to validate number ( Check iffrequency of a digit is less than thedigit itself or not ) ; If current digit of temp is same as i ; if frequency is greater than digit value , return false ; Driver Code"
Solution in C#:

using System ; public class GFG { static bool validate ( long n ) { for ( int i = 0 ; i < 10 ; i ++ ) { long temp = n ; int count = 0 ; while ( temp > 0 ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } static public void Main ( String [ ] args ) { long n = 1552793 ; if ( validate ( n ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }


"Check if frequency of each element in given array is unique or not | C # code for the above approach ; Function to check whether the frequency of elements in array is unique or not . ; Freq map will store the frequency of each element of the array ; Store the frequency of each element from the array ; Check whether frequency of any two or more elements are same or not . If yes , return false ; Return true if each frequency is unique ; Driver Code ; Given array [ ] arr ; Function call ; Print the result"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static bool checkUniqueFrequency ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } } HashSet < int > uniqueFreq = new HashSet < int > ( ) ; foreach ( KeyValuePair < int , int > i in freq ) { if ( uniqueFreq . Contains ( i . Value ) ) return false ; else uniqueFreq . Add ( i . Value ) ; } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 5 , 5 } ; int n = arr . Length ; bool res = checkUniqueFrequency ( arr , n ) ; if ( res ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } }


"Check if given Binary string follows then given condition or not | C # program for the above approach ; Function to check if the String follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function call"
Solution in C#:

using System ; class GFG { static bool checkrules ( String s ) { if ( s . Length == 0 ) return true ; if ( s [ 0 ] != '1' ) return false ; if ( s . Length > 2 ) { if ( s [ 1 ] == '0' && s [ 2 ] == '0' ) return checkrules ( s . Substring ( 3 ) ) ; } return checkrules ( s . Substring ( 1 ) ) ; } public static void Main ( String [ ] args ) { String str = "1111" ; if ( checkrules ( str ) ) { Console . Write ( " Valid ▁ String " ) ; } else { Console . Write ( " Invalid ▁ String " ) ; } } }


"Check if given Parentheses expression is balanced or not | C # program for the above approach . ; Function to check if parentheses are balanced ; Initialising variables ; Traversing the expression ; It is a closing parenthesis ; This means there are more Closing parenthesis than opening ones ; If count is not zero , It means there are more opening parenthesis ; Driver code"
Solution in C#:

using System ; class GFG { public static bool isBalanced ( String exp ) { bool flag = true ; int count = 0 ; for ( int i = 0 ; i < exp . Length ; i ++ ) { if ( exp [ i ] == ' ( ' ) { count ++ ; } else { count -- ; } if ( count < 0 ) { flag = false ; break ; } } if ( count != 0 ) { flag = false ; } return flag ; } public static void Main ( String [ ] args ) { String exp1 = " ( ( ( ) ) ) ( ) ( ) " ; if ( isBalanced ( exp1 ) ) Console . WriteLine ( " Balanced " ) ; else Console . WriteLine ( " Not ▁ Balanced " ) ; String exp2 = " ( ) ) ( ( ( ) ) " ; if ( isBalanced ( exp2 ) ) Console . WriteLine ( " Balanced " ) ; else Console . WriteLine ( " Not ▁ Balanced " ) ; } }


"Check if given Preorder , Inorder and Postorder traversals are of same tree | C # program to check if all three given traversals are of the same tree ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; Recursive function to construct binary tree of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for  cases where inorder and preorder do not form a  tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; function to compare Postorder traversal on constructed tree and given Postorder ; first recur on left child ; now recur on right child ; Compare if data at current index in both Postorder traversals are same ; Driver code ; build tree from given Inorder and Preorder traversals ; compare postorder traversal on constructed tree with given Postorder traversal ; If both postorder traversals are same"
Solution in C#:

using System ; public class GfG { static int preIndex = 0 ; class Node { public int data ; public Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static int search ( int [ ] arr , int strt , int end , int value ) { for ( int i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return - 1 ; } static Node buildTree ( int [ ] In , int [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; Node tNode = newNode ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( In , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( In , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( In , pre , inIndex + 1 , inEnd ) ; return tNode ; } static int checkPostorder ( Node node , int [ ] postOrder , int index ) { if ( node == null ) return index ; index = checkPostorder ( node . left , postOrder , index ) ; index = checkPostorder ( node . right , postOrder , index ) ; if ( node . data == postOrder [ index ] ) index ++ ; else return - 1 ; return index ; } public static void Main ( ) { int [ ] inOrder = { 4 , 2 , 5 , 1 , 3 } ; int [ ] preOrder = { 1 , 2 , 4 , 5 , 3 } ; int [ ] postOrder = { 4 , 5 , 2 , 3 , 1 } ; int len = inOrder . Length ; Node root = buildTree ( inOrder , preOrder , 0 , len - 1 ) ; int index = checkPostorder ( root , postOrder , 0 ) ; if ( index == len ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if given String can be split only into subsequences ABC | C # program for the above approach ; Function to check if the given string can be splitted into subsequences " ABC " ; Stores the length of the string ; Stores the indices of ' A ' ; Stores the indices of ' B ' ; Stores the indices of ' C ' ; Traverse the string S ; If S [ i ] is equal to ' A ' ; Push the index i in A ; Else if S [ i ] is equal to ' B ' ; Push the index i in B ; Else if S [ i ] is equal to ' C ' ; Push the index i in C ; If N is not multiple of 3 or count of characters ' A ' , ' B ' and ' C ' are not equal ; Return " No " ; Iterate over the deque B ; If A is not empty and curr B is greater than A [ 0 ] ; Removes the front element of A ; Else return " No " ; Iterate over the deque B in reverse ; If C is not empty and last element of C is greater thab B [ i ] ; Removes the last element of C ; Else return " No " ; If none of the above cases satisfy return "Yes' ; Driver code ; Input ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; public static class GFG { public static IEnumerable < T > Reverse < T > ( this LinkedList < T > list ) { var el = list . Last ; while ( el != null ) { yield return el . Value ; el = el . Previous ; } } public static string check ( string S ) { int N = S . Length ; LinkedList < int > A = new LinkedList < int > ( ) ; LinkedList < int > B = new LinkedList < int > ( ) ; LinkedList < int > C = new LinkedList < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' A ' ) { A . AddLast ( i ) ; } else if ( S [ i ] == ' B ' ) { B . AddLast ( i ) ; } else { C . AddLast ( i ) ; } } if ( N % 3 > 0 A . Count != B . Count A . Count != C . Count ) { return " No " ; } foreach ( var itr in B ) { int b = itr ; if ( A . Count > 0 && b > A . First . Value ) { A . RemoveFirst ( ) ; } else { return " No " ; } } foreach ( var itr in B . Reverse ( ) ) { int b = itr ; if ( C . Count > 0 && b < C . Last . Value ) { C . RemoveLast ( ) ; } else { return " No " ; } } return " Yes " ; } static public void Main ( ) { string S = " ABABCC " ; Console . Write ( check ( S ) ) ; } }


"Check if given array can be made 0 with given operations performed any number of times | C # implementation of the approach ; Function to whether the array can be made zero or not ; Count for even elements ; Count for odd elements ; Traverse the array to count the even and odd ; If arr [ i ] is odd ; If arr [ i ] is even ; Check if count of even is zero or count of odd is zero ; Driver 's Code"
Solution in C#:

using System ; class GFG { static void check ( int [ ] arr , int N ) { int even = 0 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == N odd == N ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 3 } ; int N = arr . Length ; check ( arr , N ) ; } }


"Check if given array can be rearranged such that mean is equal to median | C # program for the above approach ; Function to return true or false if size of array is odd ; To prevent overflow ; If element is greater than mid , then it can only be present in right subarray ; If element is smaller than mid , then it can only be present in left subarray ; Else the element is present at the middle then return 1 ; when element is not present in array then return 0 ; Function to return true or false if size of array is even ; Calculating Candidate Median ; If Candidate Median if greater than Mean then decrement j ; If Candidate Median if less than Mean then increment i ; If Candidate Median if equal to Mean then return 1 ; when No candidate found for mean ; Function to return true if Mean can be equal to any candidate median otherwise return false ; Calculating Mean ; If N is Odd ; If N is even ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool binarySearch ( float [ ] arr , int size , float key ) { int low = 0 , high = size - 1 , mid ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( key > arr [ mid ] ) low = mid + 1 ; else if ( key < arr [ mid ] ) high = mid - 1 ; else return true ; } return false ; } static bool twoPointers ( float [ ] arr , int N , float mean ) { int i = 0 , j = N - 1 ; while ( i < j ) { float temp = ( arr [ i ] + arr [ j ] ) / 2 ; if ( temp > mean ) j -- ; else if ( temp < mean ) i ++ ; else return true ; } return false ; } static bool checkArray ( float [ ] arr , int N ) { float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; float mean = sum / N ; if ( ( N & 1 ) != 0 ) return binarySearch ( arr , N , mean ) ; else return twoPointers ( arr , N , mean ) ; } public static void Main ( ) { float [ ] arr = { 1.0f , 3.0f , 6.0f , 9.0f , 12.0f , 32.0f } ; int N = arr . Length ; if ( checkArray ( arr , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if given array is almost sorted ( elements are at | C # Code to check if given array is almost sorted or not ; function for checking almost sort ; One by one compare adjacents . ; Check whether resultant is sorted or not ; is resultant is sorted return true ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool almostSort ( int [ ] A , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { int temp = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = temp ; i ++ ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } public static void Main ( ) { int [ ] A = { 1 , 3 , 2 , 4 , 6 , 5 } ; int n = A . Length ; if ( almostSort ( A , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if given coins can be used to pay a value of S | C # implementation to check if it is possible to pay a value ; Function to check if it is possible to pay a value ; Loop to add the value of coin A ; Condition to check if it is possible to pay a value of S ; Driver Code"
Solution in C#:

 using System ; class GFG { static void knowPair ( int a , int b , int n , int s , int m ) { int i = 0 , rem = 0 ; int count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = rem / m ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { Console . Write ( " YES " + "
 " ) ; } else { Console . Write ( " NO " + "
 " ) ; } } public static void Main ( String [ ] args ) { int A = 1 ; int B = 2 ; int n = 3 ; int S = 4 ; int m = 2 ; knowPair ( A , B , n , S , m ) ; } }


"Check if given intervals can be made non | C # implementation to check if the intervals can be non - overlapping by by adding or subtracting X to each interval ; Function to check if two intervals overlap with each other ; Condition to check if the intervals overlap ; Function to check if there is a existing overlapping intervals ; Path compression ; Union of two intervals Returns True if there is a overlapping with the same another interval ; Both have same another overlapping interval ; Function to check if the intervals can be added by X to form non - overlapping intervals ; If the intervals overlaps we will union them ; There is no cycle ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool checkOverlapping ( List < int > a , List < int > b ) { if ( a [ 0 ] < b [ 0 ] ) { int temp = a [ 0 ] ; a [ 0 ] = b [ 0 ] ; b [ 0 ] = temp ; temp = a [ 1 ] ; a [ 1 ] = b [ 1 ] ; b [ 1 ] = temp ; } if ( b [ 0 ] <= a [ 0 ] && a [ 0 ] <= b [ 1 ] ) return true ; return false ; } static int find ( List < int > a , int i ) { if ( a [ i ] == i ) { return i ; } a [ i ] = find ( a , a [ i ] ) ; return a [ i ] ; } static bool union ( List < int > a , int x , int y ) { int xs = find ( a , x ) ; int ys = find ( a , y ) ; if ( xs == ys ) { return true ; } a [ ys ] = xs ; return false ; } static bool checkNonOverlapping ( List < List < int > > arr , int n ) { List < int > dsu = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dsu . Add ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( checkOverlapping ( arr [ i ] , arr [ j ] ) ) { if ( union ( dsu , i , j ) ) { return false ; } } } } return true ; } static void Main ( ) { List < List < int > > arr = new List < List < int > > ( ) ; arr . Add ( new List < int > { 1 , 4 } ) ; arr . Add ( new List < int > { 2 , 2 } ) ; arr . Add ( new List < int > { 2 , 3 } ) ; int n = arr . Count ; if ( checkNonOverlapping ( arr , n ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if given number contains a digit which is the average of all other digits | C # program to check whether a given number contains a digit which is the average of all other digits ; Function which checks if a digits exists in n which is the average of all other digits ; Calculate sum of digits in n ; Store the digits ; Increase the count of digits in n ; If average of all digits is an integer ; Otherwise ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void check ( int n ) { HashSet < int > digits = new HashSet < int > ( ) ; int temp = n ; int sum = 0 ; int count = 0 ; while ( temp > 0 ) { sum += temp % 10 ; digits . Add ( temp % 10 ) ; count ++ ; temp = temp / 10 ; } if ( sum % count == 0 && digits . Contains ( sum / count ) ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } public static void Main ( String [ ] args ) { int n = 42644 ; check ( n ) ; } }


"Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | C # Program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code"
Solution in C#:

using System ; class GFG { static int N = 200000 ; static long [ ] Ans = new long [ 200000 ] ; static int index = 0 ; static void populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; long x = 5 ; long inf = 1000000000001L ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } static void checkString ( int N ) { populateNumber ( ) ; for ( int i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { Console . WriteLine ( " YES " ) ; return ; } } Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { N = 5 ; checkString ( N ) ; } }


"Check if given number is a power of d where d is a power of 2 | C # program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code"
Solution in C#:

using System ; class GFG { static int Log2n ( int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } static bool isPowerOfd ( int n , int d ) { int count = 0 ; if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % ( Log2n ( d ) ) == 0 ) ; } return false ; } static void Main ( ) { int n = 64 , d = 8 ; if ( isPowerOfd ( n , d ) ) Console . WriteLine ( " { 0 } ▁ is ▁ a ▁ " + " power ▁ of ▁ { 1 } " , n , d ) ; else Console . WriteLine ( " { 0 } ▁ is ▁ not ▁ a " + " ▁ power ▁ of ▁ { 1 } " , n , d ) ; } }


"Check if given number is perfect square | C # program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver Code"
Solution in C#:

using System ; class GFG { static void checkperfectsquare ( int n ) { if ( Math . Ceiling ( ( double ) Math . Sqrt ( n ) ) == Math . Floor ( ( double ) Math . Sqrt ( n ) ) ) { Console . Write ( " perfect ▁ square " ) ; } else { Console . Write ( " not ▁ a ▁ perfect ▁ square " ) ; } } public static void Main ( ) { int n = 49 ; checkperfectsquare ( n ) ; } }


"Check if given number is perfect square | C # program to find if x is a perfect square . ; Find floating point value of square root of x . ; if product of square root is equal , then return T / F ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPerfectSquare ( double x ) { if ( x >= 0 ) { double sr = Math . Sqrt ( x ) ; return ( sr * sr == x ) ; } return false ; } public static void Main ( ) { double x = 2502 ; if ( isPerfectSquare ( x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | C # program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the Count is greater than 1 then it can 't be  represented in required order ; Driver code"
Solution in C#:

using System ; class GFG { static bool check_order ( int [ ] arr ) { int cnt = 0 ; for ( int i = 0 ; i < arr . Length - 1 ; i ++ ) { if ( Math . Abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . Abs ( arr [ 0 ] - arr [ arr . Length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 1 } ; if ( check_order ( arr ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if given point lies in range of any of the given towers | C # program to implement the above approach ; Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise"
Solution in C#:

using System ; class GFG { static bool checkPointRange ( int [ , ] arr , int X , int Y , int N ) { for ( int i = 0 ; i < N ; i ++ ) { double dist = Math . Sqrt ( ( arr [ i , 0 ] - X ) * ( arr [ i , 0 ] - X ) + ( arr [ i , 1 ] - Y ) * ( arr [ i , 1 ] - Y ) ) ; if ( dist <= arr [ i , 2 ] ) { return true ; } } return false ; } public static void Main ( ) { int [ , ] arr = { { 1 , 1 , 3 } , { 10 , 10 , 3 } , { 15 , 15 , 15 } } ; int X = 5 , Y = 5 ; int N = arr . Length ; if ( checkPointRange ( arr , X , Y , N ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }


"Check if given polygon is a convex polygon or not | C # program to implement the above approach ; Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code"
Solution in C#:

using System ; class GFG { static int CrossProduct ( int [ , ] A ) { int X1 = ( A [ 1 , 0 ] - A [ 0 , 0 ] ) ; int Y1 = ( A [ 1 , 1 ] - A [ 0 , 1 ] ) ; int X2 = ( A [ 2 , 0 ] - A [ 0 , 0 ] ) ; int Y2 = ( A [ 2 , 1 ] - A [ 0 , 1 ] ) ; return ( X1 * Y2 - Y1 * X2 ) ; } static bool isConvex ( int [ , ] points ) { int N = points . GetLength ( 0 ) ; int prev = 0 ; int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int [ ] temp1 = GetRow ( points , i ) ; int [ ] temp2 = GetRow ( points , ( i + 1 ) % N ) ; int [ ] temp3 = GetRow ( points , ( i + 2 ) % N ) ; int [ , ] temp = new int [ points . GetLength ( 0 ) , points . GetLength ( 1 ) ] ; temp = newTempIn ( points , temp1 , temp2 , temp3 ) ; curr = CrossProduct ( temp ) ; if ( curr != 0 ) { if ( curr * prev < 0 ) { return false ; } else { prev = curr ; } } } return true ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static int [ , ] newTempIn ( int [ , ] points , int [ ] row1 , int [ ] row2 , int [ ] row3 ) { int [ , ] temp = new int [ points . GetLength ( 0 ) , points . GetLength ( 1 ) ] ; for ( var i = 0 ; i < row1 . Length ; i ++ ) { temp [ 0 , i ] = row1 [ i ] ; temp [ 1 , i ] = row2 [ i ] ; temp [ 2 , i ] = row3 [ i ] ; } return temp ; } public static void Main ( String [ ] args ) { int [ , ] points = { { 0 , 0 } , { 0 , 1 } , { 1 , 1 } , { 1 , 0 } } ; if ( isConvex ( points ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if given sorted sub | C # program to find if given array exists as a subsequece in BST ; A binary Tree node ; structure of int class ; A utility function to create a new BST node with key as given num ; A utility function to insert a given key to BST ; function to check if given sorted sub - sequence exist in BST index - . iterator for given sorted sub - sequence seq [ ] - . given sorted sub - sequence ; We traverse left subtree first in Inorder ; If current node matches with se [ index ] then move forward in sub - sequence ; We traverse left subtree in the end in Inorder ; A wrapper over seqExistUtil . It returns true if seq [ 0. . n - 1 ] exists in tree . ; Initialize index in seq [ ] ; Do an inorder traversal and find if all elements of seq [ ] were present ; index would become n if all elements of seq [ ] were present ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node left , right ; } ; public class INT { public int a ; } static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void seqExistUtil ( Node ptr , int [ ] seq , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static bool seqExist ( Node root , int [ ] seq , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int [ ] seq = { 4 , 6 , 8 , 14 } ; int n = seq . Length ; if ( seqExist ( root , seq , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if given string can be formed by two other strings or their permutations | C # implementation of the above approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . Length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . Join ( " " , s ) ; } static Boolean isPossible ( List < String > v , String str ) { str = countingsort ( str . ToCharArray ( ) ) ; for ( int i = 0 ; i < v . Count - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . Count ; j ++ ) { String temp = v [ i ] + v [ j ] ; temp = countingsort ( temp . ToCharArray ( ) ) ; if ( temp . Equals ( str ) ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; List < String > v = new List < String > ( arr ) ; if ( isPossible ( v , str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if given string can be split into four distinct strings | C # program to check if we can break a string into four distinct strings . ; Return true if both strings are equal ; Return if the given string can be split or not . ; Brute Force ; Making 4 string from the given string ; Checking if they are distinct or not . ; Driver code"
Solution in C#:

using System ; class GFG { public static Boolean strcheck ( String s1 , String s2 ) { if ( s1 . CompareTo ( s2 ) != 0 ) return false ; return true ; } public static Boolean check ( String s ) { if ( s . Length >= 10 ) return true ; for ( int i = 1 ; i < s . Length ; i ++ ) { for ( int j = i + 1 ; j < s . Length ; j ++ ) { for ( int k = j + 1 ; k < s . Length ; k ++ ) { String s1 = " " , s2 = " " , s3 = " " , s4 = " " ; try { s1 = s . Substring ( 0 , i ) ; s2 = s . Substring ( i , j - i ) ; s3 = s . Substring ( j , k - j ) ; s4 = s . Substring ( k , s . Length - k ) ; } catch ( Exception e ) { } if ( strcheck ( s1 , s2 ) && strcheck ( s1 , s3 ) && strcheck ( s1 , s4 ) && strcheck ( s2 , s3 ) && strcheck ( s2 , s4 ) && strcheck ( s3 , s4 ) ) return true ; } } } return false ; } public static void Main ( String [ ] args ) { String str = " aaabb " ; if ( check ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if given string contains all the digits | C # implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if str contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the String ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in str ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 10 ; static bool isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static bool allDigits ( String str , int len ) { bool [ ] present = new bool [ MAX ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( isDigit ( str [ i ] ) ) { int digit = str [ i ] - '0' ; present [ digit ] = true ; } } for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } public static void Main ( String [ ] args ) { String str = " Geeks12345for69708" ; int len = str . Length ; if ( allDigits ( str , len ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if given string satisfies the following conditions | C # program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Driver code ; Given String ; Function call"
Solution in C#:

using System ; class GFG { static void isValid ( string s ) { int n = ( int ) Math . Sqrt ( s . Length ) ; char check = s [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s [ ( n * x ) + y ] != check || s [ ( n * y ) + x ] != check ) { Console . Write ( " No " ) ; return ; } x -- ; y ++ ; } } Console . Write ( " Yes " ) ; } public static void Main ( ) { string str = " abacdaeaafaghaia " ; isValid ( str ) ; } }


"Check if given strings can be made same by swapping two characters of same or different strings | C # program to implement the above approach ; Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] [ j ] ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static bool isEqualStrings ( string [ ] arr , int N ) { int M = arr [ 0 ] . Length ; int [ ] cntFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] [ j ] - ' a ' ] += 1 ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } public static void Main ( ) { string [ ] arr = { " aab " , " bbc " , " cca " } ; int N = arr . Length ; if ( isEqualStrings ( arr , N ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if given two straight lines are identical or not | C # program to check if given two straight lines are identical or not ; Function to check if they are identical ; Driver Code"
Solution in C#:

using System ; class GFG { static void idstrt ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { if ( ( a1 / a2 == b1 / b2 ) && ( a1 / a2 == c1 / c2 ) && ( b1 / b2 == c1 / c2 ) ) Console . WriteLine ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ identical " ) ; else Console . WriteLine ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ not ▁ identical " ) ; } public static void Main ( String [ ] args ) { double a1 = - 2 , b1 = 4 , c1 = 3 , a2 = - 6 , b2 = 12 , c2 = 9 ; idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }


"Check if it is possible to construct an Array of size N having sum as S and XOR value as X | C # program for the above approach ; Function to find if any sequence is possible or not . ; Since , S is greater than equal to X , and either both are odd or even There always exists a sequence ; Only one case possible is S == X or NOT ; ; Considering the above conditions true , check if XOR of S ^ ( S - X ) is X or not ; Driver code"
Solution in C#:

using System ; public class GFG { static void findIfPossible ( int N , int S , int X ) { if ( ( S >= X ) && ( S % 2 == X % 2 ) ) { if ( N >= 3 ) { Console . WriteLine ( " Yes " ) ; } if ( N == 1 ) { if ( S == X ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } if ( N == 2 ) { int C = ( S - X ) / 2 ; int A = C ; int B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { int N = 3 , S = 10 , X = 4 ; findIfPossible ( N , S , X ) ; } }


"Check if it is possible to create a palindrome string from given N | C # implementation of the above approach ; Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( String s ) { String s1 = " " ; int N = s . Length ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s [ i ] ; if ( s . Equals ( s1 ) ) return true ; return false ; } static bool createString ( int N ) { String str = " " ; String s = " " + N ; String letters = " abcdefghij " ; int sum = 0 ; String substr = " " ; for ( int i = 0 ; i < s . Length ; i ++ ) { int digit = s [ i ] - '0' ; substr += letters [ digit ] ; sum += digit ; } while ( str . Length <= sum ) { str += substr ; } str = str . Substring ( 0 , sum ) ; return isPalindrome ( str ) ; } public static void Main ( ) { int N = 61 ; bool flag = createString ( N ) ; if ( flag ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if it is possible to create a polygon with a given angle | C # implementation of above approach ; Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer"
Solution in C#:

using System ; class GFG { static void makePolygon ( double a ) { double n = 360 / ( 180 - a ) ; if ( n == ( int ) n ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } static void Main ( ) { double a = 90 ; makePolygon ( a ) ; } }


"Check if it is possible to create a polygon with given n sides | C # implementation of the approach ; Function that returns true if it is possible to form a polygon with the given sides ; Sum stores the sum of all the sides and maxS stores the length of the largest side ; If the length of the largest side is less than the sum of the other remaining sides ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int [ ] a , int n ) { int sum = 0 , maxS = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; maxS = Math . Max ( a [ i ] , maxS ) ; } if ( ( sum - maxS ) > maxS ) return true ; return false ; } static void Main ( ) { int [ ] a = { 2 , 3 , 4 } ; int n = a . Length ; if ( isPossible ( a , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if it is possible to draw a straight line with the given direction cosines | C # implementation of the approach ; Function that returns true if a straight line is possible ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( float x , float y , float z ) { float a = x * x + y * y + z * z ; if ( Math . Ceiling ( a ) == 1 && Math . Floor ( a ) == 1 ) return true ; return false ; } public static void Main ( ) { float l = 0.70710678f , m = 0.5f , n = 0.5f ; if ( isPossible ( l , m , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if it is possible to form string B from A under the given constraints | C # implementation of the approach ; Function that returns true if it is possible to form B from A satisfying the given conditions ; List to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isPossible ( string A , string B , int b , int m ) { List < List < int > > S = new List < List < int > > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { S . Add ( new List < int > ( ) ) ; } int [ ] box = new int [ A . Length ] ; for ( int i = 0 ; i < A . Length ; i ++ ) { S [ A [ i ] - ' a ' ] . Add ( i ) ; } int low = 0 ; for ( int i = 0 ; i < B . Length ; i ++ ) { List < int > indexes = S [ B [ i ] - ' a ' ] ; int it = lower_bound ( indexes , low ) ; if ( it == indexes . Count ) return false ; int count = indexes [ it ] / b ; box [ count ] = box [ count ] + 1 ; if ( box [ count ] >= m ) { count ++ ; low = ( count ) * b ; } else low = indexes [ it ] + 1 ; } return true ; } static int lower_bound ( List < int > indexes , int k ) { int low = 0 , high = indexes . Count - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( indexes [ mid ] < k ) low = mid + 1 ; else high = mid ; } return ( indexes [ low ] < k ) ? low + 1 : low ; } static void Main ( ) { string A = " abcbbcdefxyz " ; string B = " acdxyz " ; int b = 5 ; int m = 2 ; if ( isPossible ( A , B , b , m ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | C # program to check if we come back to zero or not in a clock ; Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; sum += a [ j ] ; if set then consider as ' + ' ; sum -= a [ j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver code"
Solution in C#:

using System ; class GfG { static bool checkCombinations ( int [ ] a , int n ) { int pow_set_size = ( int ) Math . Pow ( 2 , n ) ; int counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { int sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } public static void Main ( ) { int [ ] a = { 60 , 60 , 120 } ; int n = a . Length ; if ( checkCombinations ( a , n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if it is possible to get given sum by taking one element from each row | C # implementation to find whether is it possible to make sum equal to K ; Function that prints whether is it possible to make sum equal to K ; Base case ; Condition if we can make sum equal to current column by using above rows ; Iterate through current column and check whether we can make sum less than or equal to k ; Printing whether is it possible or not ; Driver code"
Solution in C#:

using System ; class GFG { static void PossibleSum ( int n , int m , int [ , ] v , int k ) { int [ , ] dp = new int [ n + 1 , k + 1 ] ; dp [ 0 , 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { if ( dp [ i , j ] == 1 ) { for ( int d = 0 ; d < m ; d ++ ) { if ( ( j + v [ i , d ] ) <= k ) { dp [ i + 1 , j + v [ i , d ] ] = 1 ; } } } } } if ( dp [ n , k ] == 1 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int N = 2 , M = 10 , K = 5 ; int [ , ] arr = new int [ , ] { { 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 } , { 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 } } ; PossibleSum ( N , M , arr , K ) ; } }


"Check if it is possible to make array increasing or decreasing by rotating the array | C # implementation of the approach ; Function that returns true if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum && the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index && minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index && minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int [ ] a , int n ) { if ( n <= 2 ) return true ; int flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] > a [ i + 1 ] && a [ i + 1 ] > a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] < a [ i + 1 ] && a [ i + 1 ] < a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; int val1 = int . MaxValue , mini = - 1 , val2 = int . MinValue , maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < val1 ) { mini = i ; val1 = a [ i ] ; } if ( a [ i ] > val2 ) { maxi = i ; val2 = a [ i ] ; } } flag = 1 ; for ( int i = 0 ; i < maxi ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi + 1 == mini ) { flag = 1 ; for ( int i = mini ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } flag = 1 ; for ( int i = 0 ; i < mini ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi - 1 == mini ) { flag = 1 ; for ( int i = maxi ; i < n - 1 ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } return false ; } public static void Main ( ) { int [ ] a = { 4 , 5 , 6 , 2 , 3 } ; int n = a . Length ; if ( isPossible ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | C # implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver Code"
Solution in C#:

using System ; using System . Collections ; class GfG { static string Check ( int [ , ] a , int [ , ] b , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i , j ] > b [ i , j ] ) { int tmp = a [ i , j ] ; a [ i , j ] = b [ i , j ] ; b [ i , j ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m - 1 ; j ++ ) if ( a [ i , j ] >= a [ i , j + 1 ] b [ i , j ] >= b [ i , j + 1 ] ) return " No " ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i , j ] >= a [ i + 1 , j ] b [ i , j ] >= b [ i + 1 , j ] ) return " No " ; return " Yes " ; } public static void Main ( string [ ] arg ) { int n = 2 , m = 2 ; int [ , ] a = { { 2 , 10 } , { 11 , 5 } } ; int [ , ] b = { { 9 , 4 } , { 3 , 12 } } ; Console . Write ( Check ( a , b , n , m ) ) ; } }


"Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | C # implementation of the approach ; Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int x , int y , int k ) { int minMoves = Math . Abs ( x ) + Math . Abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } public static void Main ( ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if it is possible to move from ( 0 , 0 ) to ( x , y ) in N steps | C # program to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Function to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Driver code"
Solution in C#:

using System ; class GFG { static bool Arrive ( int a , int b , int n ) { if ( n >= Math . Abs ( a ) + Math . Abs ( b ) && ( n - ( Math . Abs ( a ) + Math . Abs ( b ) ) ) % 2 == 0 ) return true ; return false ; } public static void Main ( ) { int a = 5 , b = 5 , n = 11 ; if ( Arrive ( a , b , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if it is possible to move from ( a , 0 ) to ( b , 0 ) with given jumps | C # program to move form ( a , 0 ) to ( b , 0 ) with given jumps ; Function to check if it is possible ; Driver code ; function call"
Solution in C#:

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


"Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | C # program for the above approach ; Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code"
Solution in C#:

using System ; class GFG { static void minimumMoves ( string s , int n , int k ) { if ( n % 2 == 1 ) { Console . WriteLine ( " No " ) ; return ; } int countOpen = 0 , countClose = 0 ; foreach ( char ch in s . ToCharArray ( ) ) if ( ch == ' ( ' ) countOpen ++ ; else if ( ch == ' ) ' ) countClose ++ ; if ( countOpen != countClose ) { Console . WriteLine ( " No " ) ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s [ i ] == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { string S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . Length , K ) ; } }


"Check if it is possible to perform the given Grid Division | C # implementation of the approach ; Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int [ ] arr , int p , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } public static void Main ( String [ ] args ) { int n = 3 , m = 4 ; int [ ] arr = { 6 , 3 , 2 , 1 } ; int p = arr . Length ; if ( isPossible ( arr , p , n , m ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C # program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code"
Solution in C#:

using System ; public class GFG { static bool canReach ( int X , int Y ) { int steps = 0 ; while ( X != 0 Y != 0 ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } public static void Main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | C # program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code"
Solution in C#:

 using System ; class GFG { static Boolean canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) Math . Pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) Math . Pow ( 3 , steps ) , steps + 1 ) ) ; } public static void Main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { Console . Write ( " YES " + "
 " ) ; } else Console . Write ( " NO " + "
 " ) ; } }


"Check if it is possible to reach ( x , y ) from origin in exactly Z steps using only plus movements | C # program for the above approach ; Function to check if it is possible to reach ( x , y ) from origin in exactly z steps ; Condition if we can 't reach in Z steps ; Driver Code ; Destination point coordinate ; Number of steps allowed ; Function Call"
Solution in C#:

 using System ; class GFG { static void possibleToReach ( int x , int y , int z ) { if ( z < Math . Abs ( x ) + Math . Abs ( y ) || ( z - Math . Abs ( x ) - Math . Abs ( y ) ) % 2 == 1 ) { Console . Write ( " Not ▁ Possible " + "
 " ) ; } else Console . Write ( " Possible " + "
 " ) ; } public static void Main ( String [ ] args ) { int x = 5 , y = 5 ; int z = 11 ; possibleToReach ( x , y , z ) ; } }


"Check if it is possible to reach M from 0 by given paths | C # program for the above approach ; Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code"
Solution in C#:

using System ; class GFG { static void canReach0toM ( int [ , ] a , int n , int m ) { int [ ] rightMost = new int [ m + 1 ] ; int [ ] dp = new int [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i , 0 ] ; int b1 = a [ i , 1 ] ; rightMost [ a1 ] = Math . Max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = Math . Min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ , ] arr = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = arr . GetLength ( 0 ) ; canReach0toM ( arr , N , M ) ; } }


"Check if it is possible to reach a number by making jumps of two given length | C # implementation of the approach ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code"
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 int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int minStepsNeeded ( int k , int d1 , int d2 , int x ) { int gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return - 1 ; Queue < pair > q = new Queue < pair > ( ) ; HashSet < int > visited = new HashSet < int > ( ) ; q . Enqueue ( new pair ( k , 0 ) ) ; visited . Add ( k ) ; while ( q . Count != 0 ) { int s = q . Peek ( ) . first ; int stp = q . Peek ( ) . second ; if ( s == x ) return stp ; q . Dequeue ( ) ; if ( ! visited . Contains ( s + d1 ) ) { q . Enqueue ( new pair ( s + d1 , stp + 1 ) ) ; visited . Add ( s + d1 ) ; } if ( ! visited . Contains ( s + d2 ) ) { q . Enqueue ( new pair ( s + d2 , stp + 1 ) ) ; visited . Add ( s + d2 ) ; } if ( ! visited . Contains ( s - d1 ) ) { q . Enqueue ( new pair ( s - d1 , stp + 1 ) ) ; visited . Add ( s - d1 ) ; } if ( ! visited . Contains ( s - d2 ) ) { q . Enqueue ( new pair ( s - d2 , stp + 1 ) ) ; visited . Add ( s - d2 ) ; } } return int . MinValue ; } public static void Main ( String [ ] args ) { int k = 10 , d1 = 4 , d2 = 6 , x = 8 ; Console . WriteLine ( minStepsNeeded ( k , d1 , d2 , x ) ) ; } }


"Check if it is possible to reach any point on the circumference of a given circle from origin | C # program for the above approach ; Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , ' R ' ; Stores the count of ' U ' , ' D ' ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static string isPossible ( string S , int R , int N ) { int cntl = 0 , cntr = 0 ; int cntu = 0 , cntd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] == ' L ' ) cntl ++ ; else if ( S [ i ] == ' R ' ) cntr ++ ; else if ( S [ i ] == ' U ' ) cntu ++ ; else cntd ++ ; } if ( Math . Max ( Math . Max ( cntl , cntr ) , Math . Max ( cntu , cntd ) ) >= R ) return " Yes " ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int r_square = R * R ; for ( int i = 1 ; i * i <= r_square ; i ++ ) { mp . Add ( i * i , i ) ; if ( mp . ContainsKey ( r_square - i * i ) ) { if ( Math . Max ( cntl , cntr ) >= mp [ r_square - i * i ] && Math . Max ( cntu , cntd ) >= i ) return " Yes " ; if ( Math . Max ( cntl , cntr ) >= i && Math . Max ( cntu , cntd ) >= mp [ r_square - i * i ] ) return " Yes " ; } } return " No " ; } static public void Main ( ) { string S = " RDLLDDLDU " ; int R = 5 ; int N = S . Length ; Console . WriteLine ( isPossible ( S , R , N ) ) ; } }


"Check if it is possible to reach destination in even number of steps in an Infinite Matrix | C # program for the above approach ; Function to check destination can be reached from source in even number of steps ; Coordinates differences ; Minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void IsEvenPath ( int [ ] Source , int [ ] Destination ) { int x_dif = Math . Abs ( Source [ 0 ] - Destination [ 0 ] ) ; int y_dif = Math . Abs ( Source [ 1 ] - Destination [ 1 ] ) ; int minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( string [ ] args ) { int [ ] Source = { 2 , 1 } ; int [ ] Destination = { 1 , 4 } ; IsEvenPath ( Source , Destination ) ; } }


"Check if it is possible to reach the point ( X , Y ) using distances given in an array | C # program for the above approach ; Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code"
Solution in C#:

using System ; class GFG { static int isPossibleToReach ( int [ ] A , int N , int X , int Y ) { double distance = Math . Sqrt ( ( X * X + Y * Y ) ) ; double mx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mx += ( A [ i ] ) ; } if ( mx < distance ) { Console . Write ( " NO " ) ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { Console . Write ( " YES " ) ; return 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( distance + mx < 2 * A [ i ] ) { Console . Write ( " No " ) ; return 0 ; } } Console . Write ( " Yes " ) ; return 0 ; } static public void Main ( ) { int [ ] A = { 2 , 5 } ; int X = 5 , Y = 4 ; int N = A . Length ; isPossibleToReach ( A , N , X , Y ) ; } }


"Check if it is possible to reach vector B by rotating vector A and adding vector C to it | C # program to Check if it is possible to reach vector B by Rotating vector A and adding vector C to it any number of times . ; function to check if vector B is possible from vector A ; if d = 0 , then you need to add nothing to vector A ; for all four quadrants ; Driver code ; initialize all three vector coordinates"
Solution in C#:

using System ; class GFG { static bool check ( long a , long b , long p , long q ) { long d = p * p + q * q ; if ( d == 0 ) return a == 0 && b == 0 ; else return ( a * p + b * q ) % d == 0 && ( b * p - a * q ) % d == 0 ; } static bool check ( int a , int b , int x , int y , int p , int q ) { if ( check ( a - x , b - y , p , q ) || check ( a + x , b + y , p , q ) || check ( a - y , b + x , p , q ) || check ( a + y , b - x , p , q ) ) return true ; else return false ; } public static void Main ( ) { int a = - 4 , b = - 2 ; int x = 0 , y = 0 ; int p = - 2 , q = - 1 ; if ( check ( a , b , x , y , p , q ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if it is possible to rearrange rectangles in a non | C # implementation of above approach ; Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; Driver code ; initialize the number of rectangles ; initialize n rectangles with length and breadth"
Solution in C#:

using System ; class GFG { static int rotateRec ( int n , int [ ] L , int [ ] B ) { int m = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . Max ( L [ i ] , B [ i ] ) <= m ) m = Math . Max ( L [ i ] , B [ i ] ) ; else if ( Math . Min ( L [ i ] , B [ i ] ) <= m ) m = Math . Min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } public static void Main ( ) { int n = 3 ; int [ ] L = { 5 , 5 , 6 } ; int [ ] B = { 6 , 7 , 8 } ; if ( rotateRec ( n , L , B ) == 1 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if it is possible to return to the starting position after moving in the given directions | C # implementation of above approach ; Main Method ; int n = 0 ; Count of North int s = 0 ; Count of South int e = 0 ; Count of East int w = 0 ; Count of West"
Solution in C#:

using System ; class GFG { public static void Main ( ) { string st = " NNNWEWESSS " ; int len = st . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( st [ i ] == ' N ' ) n += 1 ; if ( st [ i ] == ' S ' ) s += 1 ; if ( st [ i ] == ' W ' ) w += 1 ; if ( st [ i ] == ' E ' ) e += 1 ; } if ( n == s && w == e ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if it is possible to serve customer queue with different notes | C # implementation of the approach ; Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isSellingPossible ( int n , int [ ] a ) { int i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } public static void Main ( ) { int [ ] a = { 25 , 25 , 50 , 100 } ; int n = a . Length ; if ( isSellingPossible ( n , a ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if it is possible to sort an array with conditional swapping of adjacent allowed | C # program to check if we can sort an array with adjacent swaps allowed ; Returns true if it is possible to sort else false ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver function"
Solution in C#:

using System ; class GFG { static bool checkForSorting ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { else return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 2 } ; int n = arr . Length ; if ( checkForSorting ( arr , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if it is possible to sort an array with conditional swapping of elements at distance K | C # implementation to check if it is possible to sort an array with conditional swapping of elements at distance K ; Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool fun ( int [ ] arr , int n , int k ) { List < int > v = new List < int > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . Add ( arr [ j ] ) ; } v . Sort ( ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v [ x ] ; x ++ ; } v . Clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { return false ; } } return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = arr . Length ; if ( fun ( arr , n , K ) ) { Console . WriteLine ( " yes " ) ; } else { Console . WriteLine ( " no " ) ; } } }


"Check if it is possible to survive on Island | C # program to find the minimum days on which you need to buy food from the shop so that you can survive the next S days ; function to find the minimum days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code"
Solution in C#:

using System ; class GFG { static void survival ( int S , int N , int M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) Console . Write ( " No " ) ; else { int days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; Console . WriteLine ( " Yes ▁ " + days ) ; } } public static void Main ( ) { int S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ; } }


"Check if it is possible to transform one string to another | C # program to check if a string can be converted to another string by performing operations ; function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; Driver Code"
Solution in C#:

using System ; class GFG { static bool check ( string s1 , string s2 ) { int n = s1 . Length ; int m = s2 . Length ; bool [ , ] dp = new bool [ n + 1 , m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i , j ] = false ; } } dp [ 0 , 0 ] = true ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { for ( int j = 0 ; j <= s2 . Length ; j ++ ) { if ( dp [ i , j ] ) { if ( j < s2 . Length && ( Char . ToUpper ( s1 [ i ] ) == s2 [ j ] ) ) dp [ i + 1 , j + 1 ] = true ; if ( ! Char . IsUpper ( s1 [ i ] ) ) dp [ i + 1 , j ] = true ; } } } return ( dp [ n , m ] ) ; } public static void Main ( ) { string s1 = " daBcd " ; string s2 = " ABC " ; if ( check ( s1 , s2 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }