"Check given array of size n can represent BST of n levels or not | C # program to Check given array can represent BST or not ; structure for Binary Node ; To create a Tree with n levels . We always insert new node to left if it is less than previous value . ; Please refer below post for details of this function . www . geeksforgeeks . org / a - program - to - check - if - a - binary - tree - is - bst - or - not / https : ; Allow only distinct values ; Returns tree if given array of size n can represent a BST of n levels . ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int key ; public Node right , left ; } ; static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . key = num ; temp . left = null ; temp . right = null ; return temp ; } static Node createNLevelTree ( int [ ] arr , int n ) { Node root = newNode ( arr [ 0 ] ) ; Node temp = root ; for ( int i = 1 ; i < n ; i ++ ) { if ( temp . key > arr [ i ] ) { temp . left = newNode ( arr [ i ] ) ; temp = temp . left ; } else { temp . right = newNode ( arr [ i ] ) ; temp = temp . right ; } } return root ; } static bool isBST ( Node root , int min , int max ) { if ( root == null ) { return true ; } if ( root . key < min . key > max ) { return false ; } return ( isBST ( root . left , min , ( root . key ) - 1 ) && isBST ( root . right , ( root . key ) + 1 , max ) ) ; } static bool canRepresentNLevelBST ( int [ ] arr , int n ) { Node root = createNLevelTree ( arr , n ) ; return isBST ( root , int . MinValue , int . MaxValue ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 512 , 330 , 78 , 11 , 8 } ; int n = arr . Length ; if ( canRepresentNLevelBST ( arr , n ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check given matrix is magic square or not | C # program to check whether a given matrix is magic matrix or not ; Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int N = 3 ; static bool isMagicSquare ( int [ , ] mat ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i , i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i , N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) rowSum += mat [ i , j ] ; if ( rowSum != sum ) return false ; } for ( int i = 0 ; i < N ; i ++ ) { int colSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) colSum += mat [ j , i ] ; if ( sum != colSum ) return false ; } return true ; } public static void Main ( ) { int [ , ] mat = new int [ , ] { { 2 , 7 , 6 } , { 9 , 5 , 1 } , { 4 , 3 , 8 } } ; if ( isMagicSquare ( mat ) ) Console . WriteLine ( " Magic ▁ Square " ) ; else Console . WriteLine ( " Not ▁ a ▁ magic " + " ▁ Square " ) ; } }


"Check given string is oddly palindrome or not | C # program for the above approach ; Function to check if the String str is palindromic or not ; Iterate the String str from left and right pointers ; Keep comparing characters while they are same ; If they are not same then return false ; Return true if the String is palindromic ; Function to make String using odd indices of String str ; Functions checks if characters at odd index of the String forms palindrome or not ; Make odd indexed String ; Check for Palindrome ; Driver code ; Given String ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( string str ) { int l = 0 ; int h = str . Length - 1 ; while ( h > l ) { if ( str [ l ] != str [ h ] ) { return false ; } l ++ ; h -- ; } return true ; } static string makeOddString ( string str ) { string odd = " " ; for ( int i = 1 ; i < str . Length ; i += 2 ) { odd += str [ i ] ; } return odd ; } static void checkOddlyPalindrome ( string str ) { string odd = makeOddString ( str ) ; if ( isPalindrome ( odd ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void Main ( ) { string str = " ddwfefwde " ; checkOddlyPalindrome ( str ) ; } }


"Check given string is oddly palindrome or not | Set 2 | C # program for the above approach ; Function to check if String formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic String is not compared ; If stack is empty then return true ; Driver Code ; Given String"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static bool isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; bool lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < char > s = new Stack < char > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . Push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . Count > 0 ) { if ( s . Peek ( ) == str [ i ] ) s . Pop ( ) ; else break ; i = i + 2 ; } if ( s . Count == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } }


"Check given string is oddly palindrome or not | Set 2 | C # program for the above approach ; Functions checks if characters at odd index of the String forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call"
Solution in C#:

 using System ; class GFG { static bool isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str [ left ] != str [ right ] ) return false ; left += 2 ; right -= 2 ; } return true ; } public static void Main ( String [ ] args ) { int n = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } }


"Check if 2 * K + 1 non | C # program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code"
Solution in C#:

using System ; class GFG { static void checkString ( string s , int k ) { int n = s . Length ; if ( 2 * k + 1 > n ) { Console . Write ( " No " ) ; return ; } string a = s . Substring ( 0 , k ) ; string b = s . Substring ( n - k , k ) ; char [ ] arr = b . ToCharArray ( ) ; Array . Reverse ( arr ) ; b = new String ( arr ) ; if ( a == b ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { string S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; } }


"Check if A can be converted to B by reducing with a Prime number | C # implementation to find if it is possible to make a equal to b ; Function to find if it is possible to make A equal to B ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPossible ( int A , int B ) { return ( A - B > 1 ) ; } public static void Main ( ) { int A = 10 , B = 4 ; if ( isPossible ( A , B ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if Array forms an increasing | C # program for the above approach ; Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

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


"Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | C # program for the above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int convert ( List < int > p ) { int ans = 0 ; foreach ( int i in p ) { ans = ( ans << 1 ) | i ; } return ans ; } static int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } static void checkGoodMatrix ( int [ , ] mat ) { List < int > P = new List < int > ( ) ; List < int > S = new List < int > ( ) ; List < int > MR = new List < int > ( ) ; List < int > MC = new List < int > ( ) ; for ( int i = 0 ; i < mat . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < mat . GetLength ( 1 ) ; j ++ ) { if ( i == j ) P . Add ( mat [ i , j ] ) ; if ( i + j == mat . GetLength ( 0 ) - 1 ) S . Add ( mat [ i , j ] ) ; if ( i == Math . Floor ( ( mat . GetLength ( 0 ) - 1 ) / 2.0 ) ) MR . Add ( mat [ i , j ] ) ; if ( j == Math . Floor ( ( mat . GetLength ( 0 ) - 1 ) / 2.0 ) ) MC . Add ( mat [ i , j ] ) ; } } S . Reverse ( ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; } }


"Check if Euler Totient Function is same for a given number and twice of that number | C # program of the above approach ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Return if N is odd ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int sameEulerTotient ( int N ) { return ( N & 1 ) ; } public static void Main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if Euler Totient Function is same for a given number and twice of that number | C # program of the above approach ; Function to find the Euler 's Totient Function ; Initialize result as N ; Consider all prime factors of n and subtract their multiples from result ; Return the count ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Driver Code"
Solution in C#:

using System ; class GFG { static int phi ( int n ) { int result = 1 ; for ( int p = 2 ; p < n ; p ++ ) { if ( __gcd ( p , n ) == 1 ) { result ++ ; } } return result ; } static bool sameEulerTotient ( int n ) { return phi ( n ) == phi ( 2 * n ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | C # Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array [ ] a which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Recursive function to return gcd of a and b ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void ifgcdFibonacci ( int [ ] a , int n , int k ) { List < int > compositeset = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . Add ( a [ i ] ) ; } } int gcd = compositeset [ 0 ] ; for ( int i = 1 ; i < compositeset . Count ; i ++ ) { gcd = __gcd ( gcd , compositeset [ i ] ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { Console . Write ( " Yes " ) ; return ; } Console . Write ( " No " ) ; return ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 34 , 2 , 4 , 8 , 5 , 7 , 11 } ; int n = arr . Length ; int k = 2 ; ifgcdFibonacci ( arr , n , k ) ; } }


"Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | C # program for the above approach ; Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver Code"
Solution in C#:

using System ; class GFG { static void canPlace ( string s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s [ i ] == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s [ i + 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s [ i + 1 ] == '0' && s [ i - 1 ] == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { string S = "10001" ; int K = 1 ; int N = S . Length ; canPlace ( S , N , K ) ; } }


"Check if K can be obtained by performing arithmetic operations on any permutation of an Array | C # program for the above approach ; Function that finds the string of possible combination of operations ; If only one number left then check for result ; If resultant value is K ; Else return 0 ; Choose all combination of numbers and operators and operate them ; Choose the first two and operate it with ' + ' ; Place it to 0 th position ; Place ( n - 1 ) th element on 1 st position ; Evaluate the expression with current combination ; Now , we have N - 1 elements ; Try ' - ' operation ; Evaluate the expression with current combination ; Try reverse ' - ' ; Evaluate the expression with current combination ; Try ' * ' operation ; Evaluate the expression with current combination ; Try ' / ' ; Evaluate the expression with current combination ; Try reverse ' / ' ; Evaluate the expression with current combination ; Backtracking Step ; Return 0 if there doesnt exist any combination that gives K ; Function that finds the possible combination of operation to get the resultant value K ; Store the resultant operation ; Function call ; Driver Code ; Given array [ ] arr ; Resultant value K ; Function call"
Solution in C#:

using System ; class GFG { static bool find ( double [ ] v , int n , double dest , String s ) { if ( n == 1 ) { if ( Math . Abs ( v [ 0 ] - dest ) <= 0.0000001 ) { Console . WriteLine ( s + String . Join ( " " , ( int ) dest ) + " ▁ " ) ; return true ; } return false ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { double a = v [ i ] , b = v [ j ] ; String p = s ; v [ i ] = a + b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) a ) + ' + ' + String . Join ( " " , ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a - b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) a ) + ' - ' + String . Join ( " " , ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = b - a ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) b ) + ' - ' + String . Join ( " " , ( int ) a ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a * b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) a ) + ' * ' + String . Join ( " " , ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; if ( b != 0 ) { v [ i ] = a / b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) a ) + ' / ' + String . Join ( " " , ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; if ( a != 0 ) { v [ i ] = b / a ; v [ j ] = v [ n - 1 ] ; s = ( s + String . Join ( " " , ( int ) b ) + ' / ' + String . Join ( " " , ( int ) a ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; v [ i ] = a ; v [ j ] = b ; } } return false ; } static void checkPossibleOperation ( double [ ] arr , double K ) { String s = " " ; if ( ! find ( arr , arr . Length , K , s ) ) { Console . WriteLine ( " - 1" ) ; } } public static void Main ( String [ ] args ) { double [ ] arr = { 2 , 0 , 0 , 2 } ; double K = 4 ; checkPossibleOperation ( arr , K ) ; } }


"Check if K distinct array elements form an odd sum | C # program for the above approach ; Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool oddSum ( int [ ] A , int N , int K ) { HashSet < int > Odd = new HashSet < int > ( ) ; HashSet < int > Even = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . Add ( A [ i ] ) ; } else { Odd . Add ( A [ i ] ) ; } } if ( Odd . Count >= K ) return true ; bool flag = false ; for ( int i = 1 ; i < K ; i += 2 ) { int needed = K - i ; if ( needed <= Even . Count ) { return true ; } } return flag ; } public static void Main ( String [ ] args ) { int K = 5 ; int [ ] A = { 12 , 1 , 7 , 7 , 26 , 18 } ; int N = 3 ; if ( oddSum ( A , N , K ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if LCM of array elements is divisible by a prime number or not | C # program to find LCM of array of number is divisible by a prime number k or not ; Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver code"
Solution in C#:

using System ; class GFG { static bool func ( int [ ] a , int k ) { for ( int i = 0 ; i < a . Length ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } public static void Main ( ) { int [ ] a = { 14 , 27 , 38 , 76 , 84 } ; int k = 19 ; bool res = func ( a , k ) ; Console . Write ( res ) ; } }


"Check if Matrix sum is prime or not | C # implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code"
Solution in C#:

 using System ; class GFG { static int N = 4 , M = 5 ; static Boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int takeSum ( int [ ] [ ] a ) { int s = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) s += a [ i ] [ j ] ; return s ; } public static void Main ( String [ ] args ) { int [ ] [ ] a = new int [ ] [ ] { new int [ ] { 1 , 2 , 3 , 4 , 2 } , new int [ ] { 0 , 1 , 2 , 3 , 34 } , new int [ ] { 0 , 34 , 21 , 12 , 12 } , new int [ ] { 1 , 2 , 3 , 6 , 6 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) ) Console . Write ( " YES " + "
 " ) ; else Console . Write ( " NO " + "
 " ) ; } }


"Check if N and M can be made equal by increasing N by A and decreasing M by B | C # program to check if two numbers can be made equal by increasing the first by a and decreasing the second by b ; Function to whether the numbers can be made equal or not ; Check whether the numbers can reach an equal point or not ; M and N cannot be made equal by increasing M and decreasing N when M is already greater than N ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkEqualNo ( int m , int n , int a , int b ) { if ( m <= n ) { if ( ( n - m ) % ( a + b ) == 0 ) { return true ; } else { return false ; } } else { return false ; } } public static void Main ( String [ ] args ) { int M = 2 ; int N = 8 ; int A = 3 ; int B = 3 ; if ( checkEqualNo ( M , N , A , B ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if N can be converted to the form K power K by the given operation | C # implementation to check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static SortedSet < int > kPowKform = new SortedSet < int > ( ) ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . Contains ( n ) ) return 1 ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) == 1 ) { answer = 1 ; break ; } } x /= 10 ; } return answer ; } static void canBeConverted ( int n ) { if ( func ( n ) == 1 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( ) { int N = 90 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . Add ( val ) ; } canBeConverted ( N ) ; } }


"Check if N can be converted to the form K power K by the given operation | C # implementation to check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Fcuntion to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static HashSet < int > kPowKform = new HashSet < int > ( ) ; static int [ ] dp = new int [ 100005 ] ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . Contains ( n ) ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) != 0 ) { answer = 1 ; break ; } } x /= 10 ; } dp [ n ] = answer ; return answer ; } static void canBeConverted ( int n ) { Array . Fill ( dp , - 1 ) ; if ( func ( n ) != 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int N = 13 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . Add ( val ) ; } canBeConverted ( N ) ; } }


"Check if N can be expressed as product of 3 distinct numbers | C # program to find the three numbers ; function to find 3 distinct number with given product ; Declare a vector to store divisors ; store all divisors of number in array ; store all the occurence of divisors ; check if n is not equals to - 1 then n is also a prime factor ; Initialize the variables with 1 ; check for first number a ; check for second number b ; check for third number c ; check for all unwanted condition ; Driver function"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static void getnumbers ( int n ) { List < int > divisor = new List < int > ( ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { while ( n % i == 0 ) { divisor . Add ( i ) ; n /= i ; } } if ( n != 1 ) { divisor . Add ( n ) ; } int a , b , c , size ; a = b = c = 1 ; size = divisor . Count ; for ( int i = 0 ; i < size ; i ++ ) { if ( a == 1 ) { a = a * divisor [ i ] ; } else if ( b == 1 b == a ) { b = b * divisor [ i ] ; } else { c = c * divisor [ i ] ; } } if ( a == 1 b == 1 c == 1 a == b b == c a == c ) { Console . Write ( " - 1" + "
 " ) ; } else { Console . Write ( a + " ▁ " + b + " ▁ " + c + "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 64 ; getnumbers ( n ) ; } }


"Check if N can be represented as sum of distinct powers of 3 | C # program for the above approach ; Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; print ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool PermuteAndFind ( int [ ] power , int idx , int SumSoFar , int target ) { if ( idx == power . Length ) { if ( SumSoFar == target ) return true ; return false ; } bool select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; bool notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return ( select notselect ) ; } public static void DistinctPowersOf3 ( int N ) { int [ ] power = new int [ 16 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; bool found = PermuteAndFind ( power , 0 , 0 , N ) ; if ( found == true ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }


"Check if N can be represented as sum of integers chosen from set { A , B } | C # program to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver Code"
Solution in C#:

using System ; class GFG { static void checkIfPossibleRec ( int x , int a , int b , bool [ ] isPossible , int n ) { if ( x > n ) return ; if ( isPossible [ x ] ) return ; isPossible [ x ] = true ; checkIfPossibleRec ( x + a , a , b , isPossible , n ) ; checkIfPossibleRec ( x + b , a , b , isPossible , n ) ; } static bool checkPossible ( int n , int a , int b ) { bool [ ] isPossible = new bool [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) isPossible [ i ] = false ; checkIfPossibleRec ( 0 , a , b , isPossible , n ) ; return isPossible [ n ] ; } static public void Main ( ) { int a = 3 , b = 7 , n = 8 ; if ( checkPossible ( a , b , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if N can be represented as sum of positive integers containing digit D at least once | C # program for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code"
Solution in C#:

using System ; class GFG { static bool findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } static bool check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } public static void Main ( ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if N can be represented as sum of squares of two consecutive integers | C # implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of 2 consecutive numbers or not ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static bool isSumSquare ( int N ) { double n = ( 2 + Math . Sqrt ( 8 * N - 4 ) ) / 2 ; return ( n - ( int ) n ) == 0 ; } public static void Main ( String [ ] args ) { int i = 13 ; if ( isSumSquare ( i ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if N contains all digits as K in base B | C # implementation of above approach ; Function to print the number of digits ; Calculate log using base change property and then take its floor and then add 1 ; Return the output ; Function that returns true if n contains all one 's in base b ; Calculate the sum ; Driver code ; Given number N ; Given base B ; Given digit K ; Function call"
Solution in C#:

using System ; class GFG { static int findNumberOfDigits ( int n , int bas ) { int dig = ( ( int ) Math . Floor ( Math . Log ( n ) / Math . Log ( bas ) ) + 1 ) ; return dig ; } static bool isAllKs ( int n , int b , int k ) { int len = findNumberOfDigits ( n , b ) ; int sum = k * ( 1 - ( int ) Math . Pow ( b , len ) ) / ( 1 - b ) ; return sum == n ; } public static void Main ( ) { int N = 13 ; int B = 3 ; int K = 1 ; if ( isAllKs ( N , B , K ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if N is a Balanced Prime number or not | C # program to check if a given number is Balanced prime ; Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a Balanced prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a weak prime ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static bool isBalancedPrime ( int n ) { if ( ! isPrime ( n ) n == 2 ) return false ; int previous_prime = n - 1 ; int next_prime = n + 1 ; while ( ! isPrime ( next_prime ) ) next_prime ++ ; while ( ! isPrime ( previous_prime ) ) previous_prime -- ; int mean = ( previous_prime + next_prime ) / 2 ; if ( n == mean ) return true ; else return false ; } public static void Main ( ) { int n = 53 ; if ( isBalancedPrime ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if N is divisible by a number which is composed of the digits from the set { A , B } | C # program to find if number N is divisible by a number that contains only a and b as it 's digits ; Function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; Check for all numbers beginning with ' a ' or ' b ' ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isDivisibleRec ( int x , int a , int b , int n ) { if ( x > n ) return false ; if ( n % x == 0 ) return true ; return ( isDivisibleRec ( x * 10 + a , a , b , n ) || isDivisibleRec ( x * 10 + b , a , b , n ) ) ; } static bool isDivisible ( int a , int b , int n ) { return isDivisibleRec ( a , a , b , n ) || isDivisibleRec ( b , a , b , n ) ; } static public void Main ( ) { int a = 3 , b = 5 , n = 53 ; if ( isDivisible ( a , b , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if N leaves only distinct remainders on division by all values up to K | C # program to check if all remainders are distinct or not ; Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert into the set ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool is_distinct ( long n , long k ) { HashSet < long > s = new HashSet < long > ( ) ; for ( int i = 1 ; i <= k ; i ++ ) { long tmp = n % i ; if ( s . Contains ( tmp ) ) { return false ; } s . Add ( tmp ) ; } return true ; } public static void Main ( String [ ] args ) { long N = 5 , K = 3 ; if ( is_distinct ( N , K ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if N numbers with Even Sum can be selected from a given Array | C # efficient program to check if N numbers with Odd sum can be selected from the given array ; Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code"
Solution in C#:

 using System ; class GFG { static bool checkEvenSum ( int [ ] arr , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq % 2 == 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 } ; int size = arr . Length ; int N = 5 ; if ( checkEvenSum ( arr , N , size ) ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } }


"Check if Pascal 's Triangle is possible with a complete layer by using numbers upto N | C # program for the above approach ; Function to check if Pascaltriangle can be made by N integers ; Find X ; If x is integer ; Driver Code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static void checkPascaltriangle ( int N ) { double x = ( Math . Sqrt ( 8 * N + 1 ) - 1 ) / 2 ; if ( Math . Ceiling ( x ) - x == 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( String [ ] args ) { int N = 10 ; checkPascaltriangle ( N ) ; } }


"Check if String formed by first and last X characters of a String is a Palindrome | C # implementation for the above approach ; Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void isEqualSubstring ( string str , int x ) { int n = str . Length ; int i = 0 ; while ( i < n && i < x ) { if ( str [ i ] != str [ n - i - 1 ] ) { Console . Write ( " false " ) ; return ; } i ++ ; } Console . Write ( " true " ) ; } public static void Main ( ) { string str = " GeeksforGeeks " ; int x = 3 ; isEqualSubstring ( str , x ) ; } }


"Check if Sum and XOR of all elements of array is equal | C # Implementation to Check if Sum and XOR of all Elements of Array is equal ; Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal"
Solution in C#:

using System ; class GFG { static void equal_xor_sum ( int [ ] arr , int n ) { int Sum = 0 ; int Xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Sum = Sum + arr [ i ] ; Xor = Xor ^ arr [ i ] ; } if ( Sum == Xor ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 6 , 3 , 7 , 10 } ; int n = arr . Length ; equal_xor_sum ( arr , n ) ; } }


"Check if X and Y can be made zero by using given operation any number of times | C # implementation of the approach ; Function to check if X and Y can be made equal to zero by using given operation any number of times ; Check for the two conditions ; Driver code"
Solution in C#:

using System ; class GFG { static void ifPossible ( int X , int Y ) { if ( X > Y ) swap ( X , Y ) ; if ( ( X + Y ) % 5 == 0 && 3 * X >= 2 * Y ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void Main ( ) { int X = 33 , Y = 27 ; ifPossible ( X , Y ) ; } }


"Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | C # program for the above approach ; Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static string check ( int [ ] a , int [ ] b , int Na , int Nb , int k , int m ) { if ( Na < k < m ) return " No " ; Array . Sort ( a ) ; Array . Sort ( b ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " Yes " ; } return " No " ; } static public void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 3 , 4 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; int X = 2 , Y = 1 ; Console . WriteLine ( check ( arr1 , arr2 , N , M , X , Y ) ) ; } }


"Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | C # program for the above approach ; Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code"
Solution in C#:

using System ; class GFG { static void check ( int X , int Y ) { if ( X > 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 1 && Y == 1 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 2 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else if ( X == 3 && Y <= 3 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( string [ ] args ) { int X = 6 , Y = 8 ; check ( X , Y ) ; } }


"Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | C # program for the above approach ; Function to check the above problem condition ; Check for base cases ; Check if D - 1 is a divisor of X - T ; Driver Code"
Solution in C#:

using System ; public class GFG { static int possibleReachingSequence ( int X , int D , int T ) { if ( X < T ) { Console . WriteLine ( " NO " ) ; return 0 ; } if ( T * D < X ) { Console . WriteLine ( " NO " ) ; return 0 ; } if ( ( X - T ) % ( D - 1 ) == 0 ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } return 0 ; } public static void Main ( string [ ] args ) { int X = 10 , D = 3 , T = 6 ; possibleReachingSequence ( X , D , T ) ; } }


"Check if X can give change to every person in the Queue | C # program to check whether X can give change to every person in the Queue ; Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; Driver Code ; queue of customers with available notes . ; Calling function"
Solution in C#:

using System ; class GFG { static int isChangeable ( int [ ] notes , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } else { if ( fiveCount > 0 && tenCount > 0 ) { fiveCount -- ; tenCount -- ; } else if ( fiveCount >= 3 ) { fiveCount -= 3 ; } else return 0 ; } } return 1 ; } public static void Main ( ) { int [ ] a = { 5 , 5 , 5 , 10 , 20 } ; int n = a . Length ; if ( isChangeable ( a , n ) > 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | C # program for the above approach ; Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If true , print Yes ; Otherwise , print No ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections ; class GFG { static void canTransformStrings ( string A , string B ) { int n1 = A . Length ; int n2 = B . Length ; int count1A = 0 , count1B = 0 ; int odd1A = 0 , odd1B = 0 ; int even1A = 0 , even1B = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( A [ i ] == '1' ) count1A ++ ; else { if ( ( count1A & 1 ) == 1 ) odd1A ++ ; else even1A ++ ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( B [ i ] == '1' ) count1B ++ ; else { if ( ( count1B & 1 ) == 1 ) odd1B ++ ; else even1B ++ ; } } if ( count1A == count1B && odd1A == odd1B && even1A == even1B ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } static void Main ( ) { string A = "10011" , B = "11100" ; canTransformStrings ( A , B ) ; } }


"Check if a Binary String can be sorted in decreasing order by removing non | C # program for the above approach ; Function to sort the given string in decreasing order by removing the non adjacent characters ; Keeps the track whether the string can be sorted or not ; Traverse the given string S ; Check if S [ i ] and S [ i + 1 ] are both '1' ; Traverse the string S from the indices i to 0 ; If S [ j ] and S [ j + 1 ] is equal to 0 ; Mark flag false ; If flag is 0 , then it is not possible to sort the string ; Otherwise ; Driver code"
Solution in C#:

using System ; class GFG { static string canSortString ( string S , int N ) { int flag = 1 ; int i , j ; for ( i = N - 2 ; i >= 0 ; i -- ) { if ( S [ i ] == '1' && S [ i + 1 ] == '1' ) { break ; } } for ( j = i ; j >= 0 ; j -- ) { if ( S [ j ] == '0' && S [ j + 1 ] == '0' ) { flag = 0 ; break ; } } if ( flag == 0 ) { return " No " ; } else { return " Yes " ; } } public static void Main ( string [ ] args ) { string S = "10101011011" ; int N = S . Length ; Console . WriteLine ( canSortString ( S , N ) ) ; } }


"Check if a Binary String can be split into disjoint subsequences which are equal to "010" | C # program for the above approach ; Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver code ; Given string ; Function Call"
Solution in C#:

using System ; class GFG { static bool isPossible ( String s ) { int n = s . Length ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 ; count_1 = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s [ i ] == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } static public void Main ( ) { String s = "010100" ; if ( isPossible ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if a Binary Tree ( not BST ) has duplicate values | C # Program to check duplicates in Binary Tree ; Function that used HashSet to find presence of duplicate nodes ; If tree is empty , there are no duplicates . ; If current node 's data is already present.  ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; A binary tree Node has data , pointer to left child and a pointer to right child"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class CheckDuplicateValues { public static Boolean checkDupUtil ( Node root , HashSet < int > s ) { if ( root == null ) return false ; if ( s . Contains ( root . data ) ) return true ; s . Add ( root . data ) ; return checkDupUtil ( root . left , s ) || checkDupUtil ( root . right , s ) ; } public static Boolean checkDup ( Node root ) { HashSet < int > s = new HashSet < int > ( ) ; return checkDupUtil ( root , s ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; if ( checkDup ( root ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } } public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } ;


"Check if a Binary Tree consists of a pair of leaf nodes with sum K | C # program to implement the above approach ; Stores if a pair exists or not ; Struct binary tree node ; Creates a new node ; Function to check if a pair of leaf nodes exists with sum K ; Checks if root is null ; Checks if the current node is a leaf node ; Checks for a valid pair of leaf nodes ; Insert value of current node into the set ; Traverse left and right subtree ; Driver Code ; Construct binary tree ; Stores the leaf nodes"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool pairFound = false ; class Node { public int data ; public Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void pairSum ( Node root , int target , HashSet < int > S ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( S . Contains ( target - root . data ) ) { Console . Write ( target - root . data + " ▁ " + root . data ) ; pairFound = true ; return ; } else S . Add ( root . data ) ; } pairSum ( root . left , target , S ) ; pairSum ( root . right , target , S ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; HashSet < int > S = new HashSet < int > ( ) ; int K = 13 ; pairSum ( root , K , S ) ; if ( pairFound == false ) Console . Write ( " - 1" ) ; } }


"Check if a Binary Tree contains duplicate subtrees of size 2 or more | C # program to find if there is a duplicate sub - tree of size 2 or more . ; A binary tree Node has data , pointer to left child and a pointer to right child ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Function to find if the Binary Tree contains duplicate subtrees of size 2 or more ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static char MARKER = ' $ ' ; public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; } } ; public static String dupSubUtil ( Node root , HashSet < String > subtrees ) { String s = " " ; if ( root == null ) return s + MARKER ; String lStr = dupSubUtil ( root . left , subtrees ) ; if ( lStr . Equals ( s ) ) return s ; String rStr = dupSubUtil ( root . right , subtrees ) ; if ( rStr . Equals ( s ) ) return s ; s = s + root . data + lStr + rStr ; if ( s . Length > 3 && subtrees . Contains ( s ) ) return " " ; subtrees . Add ( s ) ; return s ; } public static String dupSub ( Node root ) { HashSet < String > subtrees = new HashSet < String > ( ) ; return dupSubUtil ( root , subtrees ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( ' A ' ) ; root . left = new Node ( ' B ' ) ; root . right = new Node ( ' C ' ) ; root . left . left = new Node ( ' D ' ) ; root . left . right = new Node ( ' E ' ) ; root . right . right = new Node ( ' B ' ) ; root . right . right . right = new Node ( ' E ' ) ; root . right . right . left = new Node ( ' D ' ) ; String str = dupSub ( root ) ; if ( str . Equals ( " " ) ) Console . Write ( " ▁ Yes ▁ " ) ; else Console . Write ( " ▁ No ▁ " ) ; } }


"Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | C # Program for the above approach ; Structure of Tree node ; Function to create new Tree node ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Driver Code ; Construct a Binary Tree ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int val ; public Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static bool checkEvenOddLevel ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; int level = 0 ; while ( q . Count != 0 ) { List < int > list = new List < int > ( ) ; int size = q . Count ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q . Dequeue ( ) ; list . Add ( node . val ) ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; } if ( level % 2 == 0 ) { for ( int i = 0 ; i < list . Count - 1 ; i ++ ) { if ( list [ i + 1 ] > list [ i ] ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( int i = 0 ; i < list . Count - 1 ; i ++ ) { if ( list [ i + 1 ] < list [ i ] ) continue ; return false ; } } level ++ ; } return true ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 2 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 7 ) ; root . right . right = newNode ( 11 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if a Float value is equivalent to an Integer value | C # program to implement the above approach ; Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; Driver code"
Solution in C#:

using System ; class GFG { static bool isint ( double N ) { int X = ( int ) N ; double temp2 = N - X ; if ( temp2 > 0 ) { return false ; } return true ; } public static void Main ( String [ ] args ) { double N = 1.5 ; if ( isint ( N ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if a HexaDecimal number is Even or Odd | C # code to check if a HexaDecimal number is Even or Odd ; Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver code"
Solution in C#:

using System ; public class GFG { static string even_or_odd ( string N ) { int len = N . Length ; if ( N [ len - 1 ] == '0' N [ len - 1 ] == '2' N [ len - 1 ] == '4' N [ len - 1 ] == '6' N [ len - 1 ] == '8' N [ len - 1 ] == ' A ' N [ len - 1 ] == ' C ' N [ len - 1 ] == ' E ' ) return ( " Even " ) ; else return ( " Odd " ) ; } static public void Main ( ) { string N = " AB3454D " ; Console . WriteLine ( even_or_odd ( N ) ) ; } }


"Check if a Linked List is Pairwise Sorted | C # program to check if linked list is pairwise sorted ; A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .5"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node start ; static Boolean isPairWiseSorted ( Node head ) { Boolean flag = true ; Node temp = head ; while ( temp != null && temp . next != null ) { if ( temp . data > temp . next . data ) { flag = false ; break ; } temp = temp . next . next ; } return flag ; } static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } public static void Main ( String [ ] args ) { start = null ; push ( start , 5 ) ; push ( start , 1 ) ; push ( start , 9 ) ; push ( start , 9 ) ; push ( start , 15 ) ; push ( start , 10 ) ; if ( isPairWiseSorted ( start ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if a M | C # program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code"
Solution in C#:

using System ; class GFG { static void check ( int n , int m ) { if ( n == 2 m == 2 n % m == 0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { int m = 3 , n = 9 ; check ( n , m ) ; } }


"Check if a Matrix is Bitonic or not | C # program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code"
Solution in C#:

using System ; class GFG { readonly static int N = 3 , M = 3 ; static bool checkBitonic ( int [ ] arr , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int [ , ] arr ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkBitonic ( GetRow ( arr , i ) , M ) ) { Console . WriteLine ( " NO " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int [ ] temp = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j , i ] ; } if ( ! checkBitonic ( temp , N ) ) { Console . WriteLine ( " NO " ) ; return ; } } Console . WriteLine ( " YES " ) ; } 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 void Main ( String [ ] args ) { int [ , ] m = { { 1 , 2 , 3 } , { 3 , 4 , 5 } , { 2 , 6 , 4 } } ; check ( m ) ; } }


"Check if a Matrix is Reverse Bitonic or Not | C # Program to check if a matrix is Reverse Bitonic or not ; Function to check if an array is Reverse Bitonic or not ; Check for decreasing sequence ; Check for increasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code"
Solution in C#:

 using System ; class GFG { static int N = 3 , M = 3 ; static bool checkReverseBitonic ( int [ ] arr , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int [ , ] arr ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkReverseBitonic ( GetRow ( arr , i ) , M ) ) { Console . Write ( " No " + "
 " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int [ ] temp = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j , i ] ; } if ( ! checkReverseBitonic ( temp , N ) ) { Console . Write ( " No " + "
 " ) ; return ; } } Console . Write ( " Yes " ) ; } 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 void Main ( String [ ] args ) { int [ , ] m = { { 2 , 3 , 4 } , { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; check ( m ) ; } }


"Check if a N base number is Even or Odd | C # code to check if a Octal number is Even or Odd ; To return value of a char . ; Function to convert a number from N base to decimal ; power of base ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 1 ] * base + str [ len - 1 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Returns true if n is even , else odd ; Driver code"
Solution in C#:

using System ; class Gfg { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( string str , int base_var ) { int len = str . Length ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str [ i ] ) >= base_var ) { Console . WriteLine ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str [ i ] ) * power ; power = power * base_var ; } return num ; } public static bool isEven ( string num , int N ) { int deci = toDeci ( num , N ) ; return ( deci % 2 == 0 ) ; } public static void Main ( string [ ] args ) { string num = "11A " ; int N = 16 ; if ( isEven ( num , N ) ) { Console . WriteLine ( " Even " ) ; } else { Console . WriteLine ( " Odd " ) ; } } }


"Check if a Number is Odd or Even using Bitwise Operators | C # program to check for even or odd using Bitwise XOR operator ; Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code"
Solution in C#:

using System ; class GFG { static bool isEven ( int n ) { if ( ( n ^ 1 ) == n + 1 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int n = 100 ; Console . Write ( isEven ( n ) == true ? " Even " : " Odd " ) ; } }


"Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | C # program to implement the above approach ; Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code"
Solution in C#:

using System ; class GFG { public static bool check ( String str1 , String str2 ) { bool [ ] mark = new bool [ 26 ] ; int n = str1 . Length , m = str2 . Length ; for ( int i = 0 ; i < n ; i ++ ) { mark [ str1 [ i ] - ' a ' ] = true ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mark [ str2 [ i ] - ' a ' ] ) return true ; } return false ; } public static void Main ( String [ ] args ) { String str1 = " abca " , str2 = " efad " ; if ( check ( str1 , str2 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a Regular Bracket Sequence can be formed with concatenation of given strings | C # program for the above approach ; Function to check possible RBS from the given Strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Make two pair vectors pos and neg ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static int checkRBS ( String [ ] S ) { int N = S . Length ; pair [ ] v = new pair [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { String s = S [ i ] ; int sum = 0 ; int pre = 0 ; foreach ( char c in s . ToCharArray ( ) ) { if ( c == ' ( ' ) { ++ sum ; } else { -- sum ; } pre = Math . Min ( sum , pre ) ; } v [ i ] = new pair ( sum , pre ) ; } List < pair > pos = new List < pair > ( ) ; List < pair > neg = new List < pair > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( v [ i ] . first >= 0 ) { pos . Add ( new pair ( - v [ i ] . second , v [ i ] . first ) ) ; } else { neg . Add ( new pair ( v [ i ] . first - v [ i ] . second , - v [ i ] . first ) ) ; } } pos . Sort ( ) ; int open = 0 ; foreach ( pair p in pos ) { if ( open - p . first >= 0 ) { open += p . second ; } else { Console . Write ( " No " + "
 " ) ; return 0 ; } } neg . Sort ( ) ; int negative = 0 ; foreach ( pair p in neg ) { if ( negative - p . first >= 0 ) { negative += p . second ; } else { Console . Write ( " No
 " ) ; return 0 ; } } if ( open != negative ) { Console . Write ( " No
 " ) ; return 0 ; } Console . Write ( " Yes
 " ) ; return 0 ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ) " , " ( ) ( " } ; checkRBS ( arr ) ; } }


"Check if a Rook can reach the given destination in a single move | C # program to implement the above approach ; Function to check if it is possible to reach destination in a single move by a rook ; Driver Code ; Given arrays"
Solution in C#:

using System ; class GFG { static string check ( int current_row , int current_col , int destination_row , int destination_col ) { if ( current_row == destination_row ) return " POSSIBLE " ; else if ( current_col == destination_col ) return " POSSIBLE " ; else return " NOT ▁ POSSIBLE " ; } public static void Main ( ) { int current_row = 8 ; int current_col = 8 ; int destination_row = 8 ; int destination_col = 4 ; string output = check ( current_row , current_col , destination_row , destination_col ) ; Console . WriteLine ( output ) ; } }


"Check if a Sequence is a concatenation of two permutations | C # program to check if a given sequence is a concatenation of two permutations or not ; Function to Check if a given sequence is a concatenation of two permutations or not ; Computing the sum of all the elements in the array ; Computing the prefix sum for all the elements in the array ; Iterating through the i from lengths 1 to n - 1 ; Sum of first i + 1 elements ; Sum of remaining n - i - 1 elements ; Lengths of the 2 permutations ; Checking if the sums satisfy the formula or not ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkPermutation ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int lsum = prefix [ i ] ; int rsum = sum - prefix [ i ] ; int l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = arr . Length ; if ( checkPermutation ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a binary string contains all permutations of length k | C # Program to Check If a String Contains All Binary Codes of Size K ; Unordered map of type string ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool hasAllcodes ( string s , int k ) { HashSet < string > us = new HashSet < string > ( ) ; for ( int i = 0 ; i + k <= s . Length ; i ++ ) { us . Add ( s . Substring ( i , k ) ) ; } return us . Count == 1 << k ; } static void Main ( ) { string s = "00110110" ; int k = 2 ; if ( hasAllcodes ( s , k ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }


"Check if a binary string contains consecutive same or not | C # implementation of the approach ; Function that returns true is str is valid ; Assuming the string is binary If any two consecutive characters are equal then the string is invalid ; If the string is alternating ; Driver code"
Solution in C#:

using System ; class gfg { static bool isValid ( string str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) return false ; } return true ; } public static void Main ( ) { string str = "0110" ; int len = str . Length ; if ( isValid ( str , len ) ) Console . Write ( " Valid " ) ; else Console . Write ( " Invalid " ) ; } }


"Check if a binary string has a 0 between 1 s or not | Set 1 ( General approach ) | C # program to check if a string is valid or not . ; Method returns 1 when string is valid else returns 0 ; Find first occurrence of 1 in s [ ] ; Find last occurrence of 1 in s [ ] ; Check if there is any 0 in range ; Driver method"
Solution in C#:

using System ; class GFG { static bool checkString ( String s ) { int len = s . Length ; int first = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == '1' ) { first = i ; break ; } } int last = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == '1' ) { last = i ; break ; } } for ( int i = first ; i <= last ; i ++ ) if ( s [ i ] == '0' ) return false ; return true ; } public static void Main ( ) { string s = "00011111111100000" ; Console . WriteLine ( checkString ( s ) ? " VALID " : " NOT ▁ VALID " ) ; } }


"Check if a binary string has two consecutive occurrences of one everywhere | C # implementation of the approach ; Function that returns 1 if str is valid ; Index of first appearance of ' b ' ; If str starts with ' b ' ; While ' b ' occurs in str ; If ' b ' doesn ' t ▁ appear ▁ after ▁ an ▁ ' a ' ; If ' b ' is not succeeded by another ' b ' ; If sub - string is of the type " abbb " ; If str ends with a single b ; Driver code"
Solution in C#:

using System ; class GFG { private static bool isValidString ( string str , int n ) { int index = str . IndexOf ( " b " ) ; if ( index == 0 ) return false ; while ( index != - 1 ) { if ( str [ index - 1 ] != ' a ' ) return false ; if ( index + 1 < n && str [ index + 1 ] != ' b ' ) return false ; if ( index + 2 < n && str [ index + 2 ] == ' b ' ) return false ; if ( index == n - 1 ) return false ; index = str . IndexOf ( " b " , index + 2 ) ; } return true ; } public static void Main ( ) { string str = " abbaaabbabba " ; int n = str . Length ; Console . WriteLine ( isValidString ( str , n ) ) ; } }


"Check if a binary tree is sorted level | C # program to determine whether binary tree is level sorted or not . ; Structure of a tree node . ; Function to create new tree node . ; Function to determine if given binary tree is level sorted or not . ; to store maximum value of previous level . ; to store minimum value of current level . ; to store maximum value of current level . ; to store number of nodes in current level . ; queue to perform level order traversal . ; find number of nodes in current level . ; traverse current level and find minimum and maximum value of this level . ; if minimum value of this level is not greater than maximum value of previous level then given tree is not level sorted . ; maximum value of this level is previous maximum value for next level . ; Driver Code ; 1 / 4 \ 6 / \ 8 9 / \ 12 10"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static int isSorted ( Node root ) { int prevMax = int . MinValue ; int minval ; int maxval ; int levelSize ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { levelSize = q . Count ; minval = int . MaxValue ; maxval = int . MinValue ; while ( levelSize > 0 ) { root = q . Peek ( ) ; q . Dequeue ( ) ; levelSize -- ; minval = Math . Min ( minval , root . key ) ; maxval = Math . Max ( maxval , root . key ) ; if ( root . left != null ) q . Enqueue ( root . left ) ; if ( root . right != null ) q . Enqueue ( root . right ) ; } if ( minval <= prevMax ) return 0 ; prevMax = maxval ; } return 1 ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 8 ) ; root . left . right . right = newNode ( 9 ) ; root . left . right . left . left = newNode ( 12 ) ; root . left . right . right . right = newNode ( 10 ) ; if ( isSorted ( root ) == 1 ) Console . WriteLine ( " Sorted " ) ; else Console . WriteLine ( " Not ▁ sorted " ) ; } }


"Check if a cell can be visited more than once in a String | C # program to check if any cell of the string can be visited more than once ; Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once , Return True ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkIfOverlap ( String str ) { int len = str . Length ; int [ ] visited = new int [ len + 1 ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' . ' ) continue ; for ( int j = Math . Max ( 0 , i - str [ i ] ) ; j <= Math . Min ( len , i + str [ i ] ) ; j ++ ) visited [ j ] ++ ; } for ( int i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } public static void Main ( ) { String str = " . 2 . . 2 . " ; if ( checkIfOverlap ( str ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if a decreasing Array can be sorted using Triple cyclic shift | C # program for the above approach ; If array is 3 2 1 can ' t ▁ ▁ be ▁ sorted ▁ because ▁ 2 ▁ is ▁ in ▁ ▁ its ▁ correct ▁ position , ▁ 1 ▁ ▁ and ▁ 3 ▁ can ' t shift right because cyclic right shift takes place between 3 elements ; Check if its possible to sort ; Number of swap is N / 2 always for this approach ; Printing index of the cyclic right shift ; Driver code"
Solution in C#:

using System ; class GFG { static void sortarray ( int [ ] arr , int N ) { if ( N == 3 ) Console . WriteLine ( " NO " ) ; else if ( N % 4 == 0 N % 4 == 1 ) { Console . WriteLine ( " YES " ) ; Console . WriteLine ( N / 2 ) ; int k = 1 ; for ( int l = 0 ; l < ( N / 4 ) ; l ++ ) { Console . WriteLine ( k + " ▁ " + ( k + 1 ) + " ▁ " + N ) ; Console . WriteLine ( k + 1 + " ▁ " + N + " ▁ " + ( N - 1 ) ) ; k = k + 2 ; N = N - 2 ; } } else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int N = 5 ; int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; sortarray ( arr , N ) ; } }


"Check if a destination is reachable from source with two movements allowed | Set 2 | C # program to implement the above approach ; Check if ( x2 , y2 ) can be reached from ( x1 , y1 ) ; Reduce x2 by y2 until it is less than or equal to x1 ; Reduce y2 by x2 until it is less than or equal to y1 ; If x2 is reduced to x1 ; Check if y2 can be reduced to y1 or not ; If y2 is reduced to y1 ; Check if x2 can be reduced to x1 or not ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isReachable ( long x1 , long y1 , long x2 , long y2 ) { while ( x2 > x1 && y2 > y1 ) { if ( x2 > y2 ) x2 %= y2 ; else y2 %= x2 ; } if ( x2 == x1 ) return ( y2 - y1 ) >= 0 && ( y2 - y1 ) % x1 == 0 ; else if ( y2 == y1 ) return ( x2 - x1 ) >= 0 && ( x2 - x1 ) % y1 == 0 ; else return false ; } public static void Main ( String [ ] args ) { long source_x = 2 , source_y = 10 ; long dest_x = 26 , dest_y = 12 ; if ( isReachable ( source_x , source_y , dest_x , dest_y ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if a doubly linked list of characters is palindrome or not | C # program to check if doubly linked list is palindrome or not ; Structure of node ; Given a reference ( pointer to pointer ) to the head of a list and an int , inserts a new node on the front of the list . ; Function to check if list is palindrome or not ; Find rightmost node ; Driver program"
Solution in C#:

using System ; class GFG { public class Node { public char data ; public Node next ; public Node prev ; } ; static Node push ( Node head_ref , char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; new_node . prev = null ; if ( head_ref != null ) head_ref . prev = new_node ; head_ref = new_node ; return head_ref ; } static bool isPalindrome ( Node left ) { if ( left == null ) return true ; Node right = left ; while ( right . next != null ) right = right . next ; while ( left != right ) { if ( left . data != right . data ) return false ; left = left . next ; right = right . prev ; } return true ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , ' l ' ) ; head = push ( head , ' e ' ) ; head = push ( head , ' v ' ) ; head = push ( head , ' e ' ) ; head = push ( head , ' l ' ) ; if ( isPalindrome ( head ) ) Console . Write ( " It ▁ is ▁ Palindrome " ) ; else Console . Write ( " Not ▁ Palindrome " ) ; } }


"Check if a given Binary Tree is height balanced like a Red | C # Program to check if a given Binary Tree is balanced like a Red - Black Tree ; utility that allocates a new Node with the given key ; Returns returns tree if the Binary tree is balanced like a Red - Black tree . This function also sets value in maxh and minh ( passed by reference ) . maxh and minh are set as maximum and minimum heights of root . ; Base case ; To store max and min heights of left subtree ; To store max and min heights of right subtree ; Check if left subtree is balanced , also set lmxh and lmnh ; Check if right subtree is balanced , also set rmxh and rmnh ; Set the max and min heights of this node for the parent call ; See if this node is balanced ; A wrapper over isBalancedUtil ( ) ; Driver code"
Solution in C#:

using System ; class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key ) { left = null ; right = null ; this . key = key ; } } public class INT { public int d ; public INT ( ) { d = 0 ; } } static bool isBalancedUtil ( Node root , INT maxh , INT minh ) { if ( root == null ) { maxh . d = minh . d = 0 ; return true ; } INT lmxh = new INT ( ) , lmnh = new INT ( ) ; INT rmxh = new INT ( ) , rmnh = new INT ( ) ; if ( isBalancedUtil ( root . left , lmxh , lmnh ) == false ) return false ; if ( isBalancedUtil ( root . right , rmxh , rmnh ) == false ) return false ; maxh . d = Math . Max ( lmxh . d , rmxh . d ) + 1 ; minh . d = Math . Min ( lmnh . d , rmnh . d ) + 1 ; if ( maxh . d <= 2 * minh . d ) return true ; return false ; } static bool isBalanced ( Node root ) { INT maxh = new INT ( ) , minh = new INT ( ) ; return isBalancedUtil ( root , maxh , minh ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 5 ) ; root . right = new Node ( 100 ) ; root . right . left = new Node ( 50 ) ; root . right . right = new Node ( 150 ) ; root . right . left . left = new Node ( 40 ) ; Console . WriteLine ( isBalanced ( root ) ? " Balanced " : " Not ▁ Balanced " ) ; } }


"Check if a given array contains duplicate elements within k distance from each other | C # program to Check if a given array contains duplicate elements within k distance from each other ; Creates an empty hashset ; Traverse the input array ; If already present n hash , then we found a duplicate within k distance ; Add this item to hashset ; Remove the k + 1 distant item ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool checkDuplicatesWithinK ( int [ ] arr , int k ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( set . Contains ( arr [ i ] ) ) return true ; set . Add ( arr [ i ] ) ; if ( i >= k ) set . Remove ( arr [ i - k ] ) ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; if ( checkDuplicatesWithinK ( arr , 3 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a given circle lies completely inside the ring formed by two concentric circles | C # code to check if a circle lies in the ring ; Function to check if circle lies in the ring ; distance between center of circle center of concentric circles ( origin ) using Pythagoras theorem ; Condition to check if circle is strictly inside the ring ; Driver Code ; Both circle with radius ' r ' and ' R ' have center ( 0 , 0 )"
Solution in C#:

using System ; class ring { public static bool checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = ( int ) Math . Sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; } public static void Main ( ) { int r = 8 , R = 4 , r1 = 2 , x1 = 6 , y1 = 0 ; if ( checkcircle ( r , R , r1 , x1 , y1 ) ) Console . WriteLine ( " yes " ) ; else Console . WriteLine ( " no " ) ; } }


"Check if a given integer is the product of K consecutive integers | C # program for the above approach ; Function to check if N can be expressed as the product of K consecutive integers ; Stores the K - th root of N ; Stores the product of K consecutive integers ; Traverse over the range [ 1 , K ] ; Update the product ; If product is N , then return " Yes " ; Otherwise , traverse over the range [ 2 , Kthroot ] ; Update the value of product ; If product is equal to N ; Otherwise , return " No " ; Driver Code"
Solution in C#:

using System ; class GFG { static string checkPro ( int n , int k ) { double exp = 1.0 / k ; int KthRoot = ( int ) Math . Pow ( n , exp ) ; int product = 1 ; for ( int i = 1 ; i < k + 1 ; i ++ ) { product = product * i ; } if ( product == n ) return " Yes " ; else { for ( int j = 2 ; j < KthRoot + 1 ; j ++ ) { product = product * ( j + k - 1 ) ; product = product / ( j - 1 ) ; if ( product == n ) return " Yes " ; } } return " No " ; } static public void Main ( ) { int N = 210 ; int K = 3 ; Console . WriteLine ( checkPro ( N , K ) ) ; } }


"Check if a given matrix is Hankel or not | C # Program to check if given matrix is Hankel Matrix or not . ; Function to check if given matrix is Hankel Matrix or not . ; for each row ; for each column ; checking if i + j is less than n ; checking if the element is equal to the corresponding diagonal constant ; checking if the element is equal to the corresponding diagonal constant ; Drivers code"
Solution in C#:

using System ; class GFG { static bool checkHankelMatrix ( int n , int [ , ] m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i , j ] != m [ i + j , 0 ] ) return false ; } else { if ( m [ i , j ] != m [ i + j - n + 1 , n - 1 ] ) return false ; } } } return true ; } public static void Main ( ) { int n = 4 ; int [ , ] m = { { 1 , 2 , 3 , 5 } , { 2 , 3 , 5 , 8 } , { 3 , 5 , 8 , 0 } , { 5 , 8 , 0 , 9 } } ; if ( checkHankelMatrix ( n , m ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if a given matrix is sparse or not | C # code to check if a matrix is sparse . ; Count number of zeros in the matrix ; Driver Function"
Solution in C#:

using System ; class GFG { static bool isSparse ( int [ , ] array , int m , int n ) { int counter = 0 ; for ( int i = 0 ; i < m ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) if ( array [ i , j ] == 0 ) ++ counter ; return ( counter > ( ( m * n ) / 2 ) ) ; } public static void Main ( ) { int [ , ] array = { { 1 , 0 , 3 } , { 0 , 0 , 4 } , { 6 , 0 , 0 } } ; int m = 3 , n = 3 ; if ( isSparse ( array , m , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a given number N has at least one odd divisor not exceeding N | C # program for the above approach ; Function to check whether N has at least one odd divisor not exceeding N - 1 or not ; Stores the value of N ; Reduce the given number N by dividing it by 2 ; If N is divisible by an odd divisor i ; Check if N is an odd divisor after reducing N by dividing it by 2 ; Otherwise ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static string oddDivisor ( int N ) { int X = N ; while ( N % 2 == 0 ) { N /= 2 ; } for ( int i = 3 ; i * i <= X ; i += 2 ) { if ( N % i == 0 ) { return " Yes " ; } } if ( N != X ) { return " Yes " ; } return " No " ; } static public void Main ( ) { int N = 10 ; Console . Write ( oddDivisor ( N ) ) ; } }


"Check if a given number can be expressed as pair | C # program of the above approach ; Function to check if the number is pair - sum of sum of first X natural numbers ; Check if the given number is sum of pair of special numbers ; X is the sum of first i natural numbers ; t = 2 * Y ; Condition to check if Y is a special number ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { static void checkSumOfNatural ( int n ) { int i = 1 ; bool flag = false ; while ( i * ( i + 1 ) < n * 2 ) { int X = i * ( i + 1 ) ; int t = n * 2 - X ; int k = ( int ) Math . Sqrt ( t ) ; if ( k * ( k + 1 ) == t ) { flag = true ; break ; } i += 1 ; } if ( flag ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( String [ ] args ) { int n = 25 ; checkSumOfNatural ( n ) ; } }


"Check if a given number can be represented in given a no . of digits in any base | C # program to check if a given number can be represented in given number of digits in any base ; Returns true if ' num ' can be represented usind ' dig ' digits in ' base ' ; Base case ; If there are more than 1 digits left and number is more than base , then remove last digit by doing num / base , reduce the number of digits and recur ; return true of num can be represented in ' dig ' digits in any base from 2 to 32 ; Check for all bases one by one ; Driver code"
Solution in C#:

using System ; class GFG { static bool checkUtil ( int num , int dig , int i ) { if ( dig == 1 && num < i ) return true ; if ( dig > 1 && num >= i ) return checkUtil ( ( num / i ) , -- dig , i ) ; return false ; } static bool check ( int num , int dig ) { for ( int i = 2 ; i <= 32 ; i ++ ) if ( checkUtil ( num , dig , i ) ) return true ; return false ; } public static void Main ( ) { int num = 8 ; int dig = 3 ; if ( check ( num , dig ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a given number divides the sum of the factorials of its digits | C # implementation of the approach ; Function that returns true if n divides the sum of the factorials of its digits ; To store factorials of digits ; To store sum of the factorials of the digits ; Store copy of the given number ; Store sum of the factorials of the digits ; If it is divisible ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPossible ( int n ) { int [ ] fac = new int [ 10 ] ; fac [ 0 ] = fac [ 1 ] = 1 ; for ( int i = 2 ; i < 10 ; i ++ ) fac [ i ] = fac [ i - 1 ] * i ; int sum = 0 ; int x = n ; while ( x != 0 ) { sum += fac [ x % 10 ] ; x /= 10 ; } if ( sum % n == 0 ) return true ; return false ; } public static void Main ( ) { int n = 19 ; if ( isPossible ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a given number is Fancy | C # program to find if a given number is fancy or not ; To store mappings of fancy pair characters . For example 6 is paired with 9 and 9 is paired with 6. ; Find number of digits in given number ; Traverse from both ends , and compare characters one by one ; If current characters at both ends are not fancy pairs ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isFancy ( String num ) { Dictionary < char , char > fp = new Dictionary < char , char > ( ) ; fp . Add ( '0' , '0' ) ; fp . Add ( '1' , '1' ) ; fp . Add ( '6' , '9' ) ; fp . Add ( '8' , '8' ) ; fp . Add ( '9' , '6' ) ; int n = num . Length ; int l = 0 , r = n - 1 ; while ( l <= r ) { if ( ! fp . ContainsKey ( num [ l ] ) fp [ num [ l ] ] != num [ r ] ) return false ; l ++ ; r -- ; } return true ; } public static void Main ( String [ ] args ) { String str = "9088806" ; if ( isFancy ( str ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if a given number is Pronic | Efficient Approach | C # program to check if a number is pronic or not ; Function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code"
Solution in C#:

using System ; class GFG { static bool pronic_check ( int n ) { int x = ( int ) ( Math . Sqrt ( n ) ) ; if ( x * ( x + 1 ) == n ) return true ; else return false ; } public static void Main ( ) { int n = 56 ; if ( pronic_check ( n ) == true ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if a given number is Pronic | Java program to check and Print Pronic Number upto 200 ; function to check Pronic Number ; Checking Pronic Number by multiplying consecutive numbers ; Driver Code ; Printing Pronic Numbers upto 200"
Solution in C#:

using System ; class GFG { static bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . Sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } public static void Main ( ) { for ( int i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) Console . Write ( i + " ▁ " ) ; } }


"Check if a given number is a Perfect square using Binary Search | C # program to check if a given number is Perfect square using Binary Search ; Function to check for perfect square number ; Find the mid value from start and last ; Check if we got the number which is square root of the perfect square number N ; If the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; If the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code"
Solution in C#:

using System ; class GFG { public static int checkPerfectSquare ( int N , int start , int last ) { int mid = ( start + last ) / 2 ; if ( start > last ) { return - 1 ; } if ( mid * mid == N ) { return mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } public static int Main ( ) { int N = 65 ; Console . Write ( checkPerfectSquare ( N , 1 , N ) ) ; return 0 ; } }


"Check if a given number is one less than twice its reverse | C # program of the above approach ; Iterative function to reverse digits of num ; Loop to extract all digits of the number ; Function to check if N satisfies given equation ; Driver Code"
Solution in C#:

using System ; class GFG { static int rev ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static bool check ( int n ) { return 2 * rev ( n ) == n + 1 ; } public static void Main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if a given number is one less than twice its reverse | C # program to implement the above approach ; Function to check y is a power of x ; logarithm function to calculate value ; Compare to the result1 or result2 both are equal ; Function to check if N satisfies the equation 2 * reverse ( n ) = n + 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isPower ( int x , int y ) { double res1 = Math . Log ( y ) / Math . Log ( x ) ; double res2 = Math . Log ( y ) / Math . Log ( x ) ; return ( res1 == res2 ) ; } static bool check ( int n ) { int x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } static public void Main ( ) { int n = 73 ; if ( check ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if a given pair of Numbers are Betrothed numbers or not | C # program for the above approach ; Function to check whether N is perfect square or not ; Find sqrt ; Function to check whether the given pairs of numbers is Betrothed numbers or not ; For finding the sum of all the divisors of first number n ; For finding the sum of all the divisors of second number m ; Driver Code ; Function Call"
Solution in C#:

 using System ; class GFG { static bool isPerfectSquare ( int N ) { double sr = Math . Sqrt ( N ) ; return ( sr - Math . Floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } for ( int i = 2 ; i <= Math . Sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : m / i ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { Console . Write ( " YES " + "
 " ) ; } else { Console . Write ( " NO " + "
 " ) ; } } public static void Main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }


"Check if a given pattern exists in a given string or not | C # program for the above approach ; Function to check if the pattern consisting of ' * ' , ' . ' and lowercase characters matches the text or not ; Base Case ; Stores length of text ; Stores length of pattern ; dp [ i ] [ j ] : Check if { text [ 0 ] , . . text [ i ] } matches { pattern [ 0 ] , ... pattern [ j ] } or not ; Base Case ; Iterate over the characters of the string pattern ; Update dp [ 0 ] [ i + 1 ] ; Iterate over the characters of both the strings ; If current character in the pattern is ' . ' ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in both the strings are equal ; Update dp [ i + 1 ] [ j + 1 ] ; If current character in the pattern is ' * ' ; Update dp [ i + 1 ] [ j + 1 ] ; Update dp [ i + 1 ] [ j + 1 ] ; Return dp [ M ] [ N ] ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isMatch ( string text , string pattern ) { if ( text == null pattern == null ) { return false ; } int N = text . Length ; int M = pattern . Length ; bool [ , ] dp = new bool [ N + 1 , M + 1 ] ; dp [ 0 , 0 ] = true ; for ( int i = 0 ; i < M ; i ++ ) { if ( pattern [ i ] == ' * ' && dp [ 0 , i - 1 ] ) { dp [ 0 , i + 1 ] = true ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( pattern [ j ] == ' . ' ) { dp [ i + 1 , j + 1 ] = dp [ i , j ] ; } if ( pattern [ j ] == text [ i ] ) { dp [ i + 1 , j + 1 ] = dp [ i , j ] ; } if ( pattern [ j ] == ' * ' ) { if ( pattern [ j - 1 ] != text [ i ] && pattern [ j - 1 ] != ' . ' ) { dp [ i + 1 , j + 1 ] = dp [ i + 1 , j - 1 ] ; } else { dp [ i + 1 , j + 1 ] = ( dp [ i + 1 , j ] dp [ i , j + 1 ] dp [ i + 1 , j - 1 ] ) ; } } } } return dp [ N , M ] ; } public static void Main ( ) { string text = " geeksforgeeks " ; string pattern = " ge * ksforgeeks " ; if ( isMatch ( text , pattern ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if a given string can be converted to a Balanced Bracket Sequence | C # program for the above approach ; Function to check if the String can be balanced by replacing the ' $ ' with opening or closing brackets ; If String can never be balanced ; Declare 2 stacks to check if all ) can be balanced with ( or $ and vice - versa ; Store the count the occurence of ( , ) and $ ; Traverse the String ; Increment closed bracket count by 1 ; If there are no opening bracket to match it then return false ; Otherwise , pop the character from the stack ; If current character is an opening bracket or $ , push it to the stack ; Increment symbol count by 1 ; Increment open bracket count by 1 ; Traverse the String from end and repeat the same process ; If there are no closing brackets to match it ; Otherwise , pop character from stack ; Store the extra ( or ) which are not balanced yet ; Check if $ is available to balance the extra brackets ; Count ramaining $ after balancing extra ( and ) ; Check if each pair of $ is convertable in ( ) ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool canBeBalanced ( String sequence ) { if ( sequence . Length % 2 == 1 ) return false ; Stack < char > stack_ = new Stack < char > ( ) ; Stack < char > stack2_ = new Stack < char > ( ) ; int countOpen = 0 , countClosed = 0 ; int countSymbol = 0 ; for ( int i = 0 ; i < sequence . Length ; i ++ ) { if ( sequence [ i ] == ' ) ' ) { countClosed ++ ; if ( stack_ . Count == 0 ) { return false ; } else { stack_ . Pop ( ) ; } } else { if ( sequence [ i ] == ' $ ' ) { countSymbol ++ ; } else { countOpen ++ ; } stack_ . Push ( sequence [ i ] ) ; } } for ( int i = sequence . Length - 1 ; i >= 0 ; i -- ) { if ( sequence [ i ] == ' ( ' ) { if ( stack2_ . Count == 0 ) { return false ; } else { stack2_ . Pop ( ) ; } } else { stack2_ . Push ( sequence [ i ] ) ; } } int extra = Math . Abs ( countClosed - countOpen ) ; if ( countSymbol < extra ) { return false ; } else { countSymbol -= extra ; if ( countSymbol % 2 == 0 ) { return true ; } } return false ; } public static void Main ( String [ ] args ) { String S = " ( ) ( $ " ; if ( canBeBalanced ( S ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }