"Check if all the Nodes in a Binary Tree having common values are at least D distance apart | C # program to implement the above approach ; Structure of a Tree node ; Function to create a new node ; Function to count the frequency of node value present in the tree ; Function that returns the max distance between the nodes that have the same key ; If right and left subtree did not have node whose key is value ; Check if the current node is equal to value ; If the left subtree has no node whose key is equal to value ; If the right subtree has no node whose key is equal to value ; Function that finds if the distance between any same nodes is at most K ; Create the map to look for same value of nodes ; Counting the frequency of nodes ; If the returned value of distance is exceeds dist ; Print the result ; Driver Code"
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 = temp . right = null ; return ( temp ) ; } static void frequencyCounts ( Dictionary < int , int > map , Node root ) { if ( root == null ) return ; if ( map . ContainsKey ( root . key ) ) map [ root . key ] = map [ root . key ] + 1 ; else map [ root . key ] = 1 ; frequencyCounts ( map , root . left ) ; frequencyCounts ( map , root . right ) ; } static int computeDistance ( Node root , int value ) { if ( root == null ) { return - 1 ; } int left = computeDistance ( root . left , value ) ; int right = computeDistance ( root . right , value ) ; if ( left == - 1 && right == - 1 ) { if ( root . key == value ) { return 1 ; } else return - 1 ; } if ( left == - 1 ) { return right + 1 ; } if ( right == - 1 ) { return left + 1 ; } else { return 1 + Math . Max ( left , right ) ; } } static void solve ( Node root , int dist ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; frequencyCounts ( map , root ) ; int flag = 0 ; foreach ( KeyValuePair < int , int > it in map ) { if ( it . Value > 1 ) { int result = computeDistance ( root , it . Key ) ; if ( result > dist result == - 1 ) { flag = 1 ; break ; } } } if ( flag == 0 ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 4 ) ; root . right . left = newNode ( 4 ) ; int dist = 7 ; solve ( root , dist ) ; } }


"Check if all the digits of the given number are same | C # Program to implement the above approach ; Function to check if all the digits in the number N is the same or not ; Find the last digit ; Find the current last digit ; Update the value of N ; If there exists any distinct digit , then return No ; Otherwise , return Yes ; Driver Code"
Solution in C#:

using System ; class GFG { static string checkSameDigits ( int N ) { int digit = N % 10 ; while ( N != 0 ) { int current_digit = N % 10 ; N = N / 10 ; if ( current_digit != digit ) { return " No " ; } } return " Yes " ; } public static void Main ( ) { int N = 222 ; Console . Write ( checkSameDigits ( N ) ) ; } }


"Check if all the digits of the given number are same | C # program for the above approach ; Function to check if all the digits in the number N is the same or not ; Get the length of N ; Form the number M of the type K * 111. . . where K is the rightmost digit of N ; Check if the numbers are equal ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { public static String checkSameDigits ( int N ) { int length = ( ( int ) Math . Log10 ( N ) ) + 1 ; int M = ( ( int ) Math . Pow ( 10 , length ) - 1 ) / ( 10 - 1 ) ; M *= N % 10 ; if ( M == N ) return " Yes " ; return " No " ; } public static void Main ( ) { int N = 222 ; Console . WriteLine ( checkSameDigits ( N ) ) ; } }


"Check if all the elements can be made of same parity by inverting adjacent elements | C # implementation of the approach ; Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code"
Solution in C#:

using System ; class GFG { static bool flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = a . Length ; if ( flipsPossible ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if all the pairs of an array are coprime with each other | C # implementation of the above approach ; Function to check if all the pairs of the array are coprime with each other or not ; Check if GCD of the pair is not equal to 1 ; All pairs are non - coprime Return false ; Function return gcd of two number ; Driver Code"
Solution in C#:

using System ; class GFG { static bool allCoprime ( int [ ] A , int n ) { bool all_coprime = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( gcd ( A [ i ] , A [ j ] ) != 1 ) { all_coprime = false ; break ; } } } return all_coprime ; } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . Length ; if ( allCoprime ( A , arr_size ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if all the pairs of an array are coprime with each other | C # implementation of the above approach ; Function to store and check the factors ; Check if factors are equal ; Check if the factor is already present ; Insert the factor in set ; Check if the factor is already present ; Insert the factors in set ; Function to check if all the pairs of array elements are coprime with each other ; Check if factors of A [ i ] haven 't occurred previously ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool findFactor ( int value , HashSet < int > factors ) { factors . Add ( value ) ; for ( int i = 2 ; i * i <= value ; i ++ ) { if ( value % i == 0 ) { if ( value / i == i ) { if ( factors . Contains ( i ) ) { return true ; } else { factors . Add ( i ) ; } } else { if ( factors . Contains ( i ) || factors . Contains ( value / i ) ) { return true ; } else { factors . Add ( i ) ; factors . Add ( value / i ) ; } } } } return false ; } static bool allCoprime ( int [ ] A , int n ) { bool all_coprime = true ; HashSet < int > factors = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == 1 ) continue ; if ( findFactor ( A [ i ] , factors ) ) { all_coprime = false ; break ; } } return all_coprime ; } public static void Main ( String [ ] args ) { int [ ] A = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . Length ; if ( allCoprime ( A , arr_size ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }


"Check if all the palindromic sub | C # implementation of the approach ; Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code"
Solution in C#:

using System ; public class GFG { static bool checkPalindrome ( String s ) { for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] != s [ ( s . Length - i - 1 ) ] ) return false ; } return true ; } static bool CheckOdd ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { String x = " " ; for ( int j = i ; j < n ; j ++ ) { x += s [ j ] ; if ( x . Length % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } public static void Main ( ) { String s = " geeksforgeeks " ; if ( CheckOdd ( s ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if all the set bits of the binary representation of N are at least K places away | C # program to check if all the set bits of the binary representation of N are at least K places away . ; Initialize check and count with 0 ; The i - th bit is a set bit ; This is the first set bit so , start calculating all the distances between consecutive bits from here ; If count is less than K return false ; Adding the count as the number of zeroes increase between set bits ; Driver code"
Solution in C#:

using System ; public class GFG { static bool CheckBits ( int N , int K ) { int check = 0 ; int count = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( ( 1 << i ) & N ) > 0 ) { if ( check == 0 ) { check = 1 ; } else { if ( count < K ) { return false ; } } count = 0 ; } else { count ++ ; } } return true ; } static public void Main ( ) { int N = 5 ; int K = 1 ; if ( CheckBits ( N , K ) ) { Console . Write ( " YES " ) ; } else { Console . Write ( " NO " ) ; } } }


"Check if an Array can be Sorted by picking only the corner Array elements | C # program to implement the above approach ; Function to check if an array can be sorted using given operations ; If sequence becomes increasing after an already non - decreasing to non - increasing pattern ; If a decreasing pattern is observed ; Driver Code"
Solution in C#:

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


"Check if an Array is a permutation of numbers from 1 to N : Set 2 | C # program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code"
Solution in C#:

 using System ; class GFG { static string permutation ( int [ ] arr , int N ) { int [ ] hash = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 1 , 5 , 5 , 3 } ; int n = arr . Length ; Console . Write ( permutation ( arr , n ) + "
 " ) ; } }


"Check if an Array is a permutation of numbers from 1 to N | C # Program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Set to check the count of non - repeating elements ; Insert all elements in the set ; Calculating the max element ; Check if set size is equal to n ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool permutation ( int [ ] arr , int n ) { HashSet < int > hash = new HashSet < int > ( ) ; int maxEle = 0 ; for ( int i = 0 ; i < n ; i ++ ) { hash . Add ( arr [ i ] ) ; maxEle = Math . Max ( maxEle , arr [ i ] ) ; } if ( maxEle != n ) return false ; if ( hash . Count == n ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 2 } ; int n = arr . Length ; if ( permutation ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if an Array is made up of Subarrays of continuous repetitions of every distinct element | C # program to implement the above approach ; Function to check if the array is made up of subarrays of repetitions ; Base Case ; Stores the size of current subarray ; If a different element is encountered ; If the previous subarray was a single element ; Reset to new subarray ; Increase size of subarray ; If last element differed from the second last element ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean ContinuousElements ( int [ ] a , int n ) { if ( n == 1 ) return false ; int curr = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { if ( curr == 1 ) return false ; else curr = 0 ; } curr ++ ; } if ( curr == 1 ) return false ; return true ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 1 , 2 , 2 , 1 , 3 , 3 } ; int n = a . Length ; if ( ContinuousElements ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if an N | C # program for the above approach ; Function to check if the polygon exists or not ; Initialize a variable to store the sum of angles ; Loop through the array and calculate the sum of angles ; Check the condition for an N - side polygon ; Driver code ; Given array arr [ ] ; Function call"
Solution in C#:

using System ; class GFG { static void checkValidPolygon ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 180 * ( N - 2 ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int N = 3 ; int [ ] arr = { 60 , 60 , 60 } ; checkValidPolygon ( arr , N ) ; } }


"Check if an Octal number is Even or Odd | C # code to check if a Octal number is Even or Odd ; Check if the number is odd or even ; Check if the last digit is either '0' , '2' , '4' , or '6' ; 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' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void Main ( String [ ] args ) { String N = "735" ; Console . Write ( even_or_odd ( N ) ) ; } }


"Check if an array can be converted to another given array by swapping pairs of unequal elements | C # program for the above approach ; Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Driver Code ; Given input arrays ; Size of the array ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void canMakeEqual ( int [ ] arr1 , int [ ] arr2 , int N ) { int count = 0 ; int arr1_one = 0 , arr1_zero = 0 ; int arr2_one = 0 , arr2_zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { Console . WriteLine ( " No " ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { Console . WriteLine ( " No " ) ; return ; } } Console . WriteLine ( " Yes " ) ; } public static void Main ( ) { int [ ] arr1 = { 0 , 1 , 1 , 0 } ; int [ ] arr2 = { 0 , 0 , 1 , 1 } ; int N = arr1 . Length ; canMakeEqual ( arr1 , arr2 , N ) ; } }


"Check if an array can be divided into pairs whose sum is divisible by k | C # program to check if arr [ 0. . n - 1 ] can be divided in pairs such that every pair is divisible by k . ; Returns true if arr [ 0. . n - 1 ] can be divided into pairs with sum divisible by k . ; An odd length array cannot be divided into pairs ; Create a frequency array to count occurrences of all remainders when divided by k . ; Count occurrences of all remainders ; Traverse input array and use freq [ ] to decide if given array can be divided in pairs ; Remainder of current element ; If remainder with current element divides k into two halves . ; Then there must be even occurrences of such remainder ; If remainder is 0 , then there must be two elements with 0 remainder ; Then there must be even occurrences of such remainder ; Else number of occurrences of remainder must be equal to number of occurrences of k - remainder ; Driver code ; Function call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static bool canPairs ( int [ ] ar , int k ) { if ( ar . Length % 2 == 1 ) return false ; Dictionary < Double , int > hm = new Dictionary < Double , int > ( ) ; for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . ContainsKey ( rem ) ) { hm [ rem ] = 0 ; } hm [ rem ] ++ ; } for ( int i = 0 ; i < ar . Length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm [ rem ] % 2 == 1 ) return false ; } else { if ( hm [ k - rem ] != hm [ rem ] ) return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; bool ans = canPairs ( arr , k ) ; if ( ans ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }


"Check if an array can be reduced to at most length K by removal of distinct elements | C # program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void maxCount ( int [ ] arr , int N , int K ) { HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } if ( N - st . Count <= K ) { Console . Write ( " Yes " ) ; } else Console . Write ( " No " ) ; } static public void Main ( ) { int [ ] arr = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . Length ; maxCount ( arr , N , K ) ; } }


"Check if an array can be sorted by rearranging odd and even | Function to check if array can be sorted or not ; Function to check if given array can be sorted or not ; Traverse the array ; Traverse remaining elements at indices separated by 2 ; If current element is the minimum ; If any smaller minimum exists ; Swap with current element ; If array is sorted ; Otherwise ; Driver code ; Given array"
Solution in C#:

using System ; class GFG { public static bool isSorted ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } public static bool sortPoss ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int idx = - 1 ; int minVar = arr [ i ] ; int j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != - 1 ) { int t ; t = arr [ i ] ; arr [ i ] = arr [ idx ] ; arr [ idx ] = t ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } static public void Main ( ) { int [ ] arr = { 3 , 5 , 1 , 2 , 6 } ; int n = arr . Length ; if ( sortPoss ( arr , n ) ) Console . WriteLine ( " True " ) ; else Console . WriteLine ( " False " ) ; } }


"Check if an array can be split into K consecutive non | C # program for the above approach ; Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code"
Solution in C#:

using System ; public class GFG { static String checkPattern ( int [ ] arr , int m , int k , int n ) { int count = 1 , t = 0 ; for ( int i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " Yes " ; } } } else { t = 0 ; count = 1 ; } } return " No " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 1 , 3 , 3 , 3 , 3 } ; int M = 1 , K = 3 ; int N = arr . Length ; Console . Write ( checkPattern ( arr , M , K , N ) ) ; } }


"Check if an array can be split into subarrays with GCD exceeding K | C # program for the above approach ; Function to check if it is possible to split an array into subarrays having GCD at least K ; Iterate over the array [ ] arr ; If the current element is less than or equal to k ; If no array element is found to be less than or equal to k ; Driver code ; Given array [ ] arr ; Length of the array ; Given K ; Function call"
Solution in C#:

using System ; class GFG { static String canSplitArray ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) { return " No " ; } } return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 1 , 8 , 16 } ; int N = arr . Length ; int K = 3 ; Console . WriteLine ( canSplitArray ( arr , N , K ) ) ; } }


"Check if an array can be split into subsets of K consecutive elements | C # program to implement the above approach ; Function to check if a given array can be split into subsets of K consecutive elements ; Stores the frequencies of array elements ; Traverse the map ; Check if all its occurrences can be grouped into K subsets ; Traverse next K elements ; If the element is not present in the array ; If it cannot be split into required number of subsets ; Driver Code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool groupInKConsecutive ( int [ ] arr , int K ) { Dictionary < int , int > count = new Dictionary < int , int > ( ) ; foreach ( int h in arr ) { if ( count . ContainsKey ( h ) ) count [ h ] ++ ; else count [ h ] = 1 ; } foreach ( int c in count . Keys . ToList ( ) ) { int cur = c ; int n = count ; if ( n > 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( ! count . ContainsKey ( cur + i ) ) { return false ; } count [ cur + i ] -= n ; if ( count [ cur + i ] < 0 ) return false ; } } } return true ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 } ; int k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }


"Check if an array contains all elements of a given range | C # Code for Check if an array contains all elements of a given range ; Function to check the array for elements in given range ; Range is the no . of elements that are to be checked ; Traversing the array ; If an element is in range ; Checking whether elements in range 0 - range are negative ; Element from range is missing from array ; All range elements are present ; Driver Code ; Defining Array and size ; A is lower limit and B is the upper limit of range ; True denotes all elements were present ; False denotes any element was not present"
Solution in C#:

using System ; class GFG { public static bool check_elements ( int [ ] arr , int n , int A , int B ) { int range = B - A ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . Abs ( arr [ i ] ) >= A && Math . Abs ( arr [ i ] ) <= B ) { int z = Math . Abs ( arr [ i ] ) - A ; if ( arr [ z ] > 0 ) { arr [ z ] = arr [ z ] * - 1 ; } } } int count = 0 ; for ( int i = 0 ; i <= range && i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; else count ++ ; } if ( count != ( range + 1 ) ) return false ; return true ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 5 , 2 , 7 , 8 , 3 } ; int n = arr . Length ; int A = 2 , B = 5 ; if ( check_elements ( arr , n , A , B ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if an array contains only one distinct element | C # program for the above approach ; Function to find if the array contains only one distinct element ; Assume first element to be the unique element ; Traversing the array ; If current element is not equal to X then break the loop and print No ; Compare and print the result ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static void uniqueElement ( int [ ] arr ) { int x = arr [ 0 ] ; int flag = 1 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] != x ) { flag = 0 ; break ; } } if ( flag == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } static public void Main ( ) { int [ ] arr = { 9 , 9 , 9 , 9 , 9 , 9 , 9 } ; uniqueElement ( arr ) ; } }


"Check if an array contains only one distinct element | C # program for the above approach ; Function to find if the array contains only one distinct element ; Create a set ; Traversing the array ; Compare and print the result ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void uniqueElement ( int [ ] arr ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { set . Add ( arr [ i ] ) ; } if ( set . Count == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 9 , 9 , 9 , 9 , 9 , 9 } ; uniqueElement ( arr ) ; } }


"Check if an array element is concatenation of two elements from another array | C # program for the above approach ; Function to find elements present in the array b [ ] which are concatenation of any pair of elements in the array a [ ] ; Stores if there doesn 't any such  element in the array brr[] ; Stored the size of both the arrays ; Store the presence of an element of array a [ ] ; Traverse the array ; Traverse the array b [ ] ; Traverse over all possible concatenations of b [ i ] ; Update right and left parts ; Check if both left and right parts are present in a [ ] ; Driver code"
Solution in C#:

using System ; class GFG { static void findConcatenatedNumbers ( int [ ] a , int [ ] b ) { bool ans = true ; int n1 = a . Length ; int n2 = b . Length ; int [ ] cnt = new int [ 100000 ] ; for ( int i = 0 ; i < n1 ; i ++ ) { cnt [ a [ i ] ] = 1 ; } for ( int i = 0 ; i < n2 ; i ++ ) { int left = b [ i ] ; int right = 0 ; int mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left /= 10 ; mul *= 10 ; if ( cnt [ left ] == 1 && cnt [ right ] == 1 ) { ans = false ; Console . Write ( b [ i ] + " ▁ " ) ; } } } if ( ans ) Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 34 , 4 , 5 } ; int [ ] b = { 26 , 24 , 345 , 4 , 22 } ; findConcatenatedNumbers ( a , b ) ; } }


"Check if an array has a majority element | Hashing based C # program to find if there is a majority element in input array . ; Returns true if there is a majority element in a [ ] ; Insert all elements in a hash table ; Check if frequency of any element is n / 2 or more . ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static Boolean isMajority ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > x in mp ) if ( x . Value >= n / 2 ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 9 , 2 , 2 } ; int n = a . Length ; if ( isMajority ( a , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if an array is increasing or decreasing | C # implementation of the approach ; Function to check the type of the array ; If the first two and the last two elements of the array are in increasing order ; If the first two and the last two elements of the array are in decreasing order ; If the first two elements of the array are in increasing order and the last two elements of the array are in decreasing order ; If the first two elements of the array are in decreasing order and the last two elements of the array are in increasing order ; Driver code"
Solution in C#:

using System ; class GFG { public static void checkType ( int [ ] arr , int n ) { if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] <= arr [ n - 1 ] ) Console . Write ( " Increasing " ) ; else if ( arr [ 0 ] >= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) Console . Write ( " Decreasing " ) ; else if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) Console . Write ( " Increasing ▁ then ▁ decreasing " ) ; else Console . Write ( " Decreasing ▁ then ▁ increasing " ) ; } static public void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ; int n = arr . Length ; checkType ( arr , n ) ; } }


"Check if an array is sorted and rotated using Binary Search | C # implementation of the above approach ; Function to return the index of the pivot ; Base cases ; Check if element at ( mid - 1 ) is pivot Consider the cases like { 4 , 5 , 1 , 2 , 3 } ; Decide whether we need to go to the left half or the right half ; Function to check if a given array is sorted rotated or not ; To check if the elements to the left of the pivot are in descending or not ; To check if the elements to the right of the pivot are in ascending or not ; If any of the above if or else is true Then the array is sorted rotated ; Else the array is not sorted rotated ; Driver code"
Solution in C#:

using System ; class GFG { static int findPivot ( int [ ] arr , int low , int high ) { if ( high < low ) return - 1 ; if ( high == low ) return low ; int mid = ( low + high ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } public static bool isRotated ( int [ ] arr , int n ) { int l = 0 ; int r = n - 1 ; int pivot = - 1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; int temp = pivot ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } pivot = temp ; else { pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; Console . WriteLine ( isRotated ( arr , 5 ) ) ; } }


"Check if an array is sorted and rotated | C # program to check if an array is sorted and rotated clockwise ; Function to check if an array is sorted and rotated clockwise ; Find the minimum element and it 's index ; Check if all elements before minIndex are in increasing order ; Check if all elements after minIndex are in increasing order ; Check if last element of the array is smaller than the element just before the element at minIndex starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not circular array ; Driver code ; Function Call"
Solution in C#:

using System ; class GFG { static void checkIfSortRotated ( int [ ] arr , int n ) { int minEle = int . MaxValue ; int maxEle = int . MinValue ; int minIndex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < minEle ) { minEle = arr [ i ] ; minIndex = i ; } } bool flag1 = true ; for ( int i = 1 ; i < minIndex ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag1 = false ; break ; } } bool flag2 = true ; for ( int i = minIndex + 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag2 = false ; break ; } } if ( flag1 && flag2 && ( arr [ n - 1 ] < arr [ 0 ] ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; int n = arr . Length ; checkIfSortRotated ( arr , n ) ; } }


"Check if an array of 1 s and 2 s can be divided into 2 parts with equal sum | C # implementation of the above approach : ; Function to check if it is possible to split the array in two parts with equal sum ; Calculate sum of elements and count of 1 's ; If total sum is odd , return False ; If sum of each part is even , return True ; If sum of each part is even but there is atleast one 1 ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isSpiltPossible ( int n , int [ ] a ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } public static void Main ( ) { int n = 3 ; int [ ] a = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }


"Check if an array represents Inorder of Binary Search tree or not | C # program to check if a given array is sorted or not . ; Function that returns true if array is Inorder traversal of any Binary Search Tree or not . ; Array has one or no element ; Unsorted pair found ; No unsorted pair found ; Driver code"
Solution in C#:

using System ; class GFG { static bool isInorder ( int [ ] arr , int n ) { if ( n == 0 n == 1 ) { return true ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] > arr [ i ] ) { return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 19 , 23 , 25 , 30 , 45 } ; int n = arr . Length ; if ( isInorder ( arr , n ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " Non " ) ; } } }


"Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | C # program for the above approach ; Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver Code ; Given Input ; Function Call"
Solution in C#:

using System ; class GFG { static void findElement ( int [ ] A , int N , int X ) { int i = 0 ; int Comparisons = 0 ; int T = 1 ; Comparisons ++ ; if ( N % 2 == 1 ) { i = 1 ; T *= ( A [ 0 ] - X ) ; } for ( ; i < N ; i += 2 ) { Comparisons += 1 ; T *= ( A [ i ] - X ) ; T *= ( A [ i + 1 ] - X ) ; } Comparions += 1 ; if ( T == 0 ) { Console . Write ( " Yes ▁ " + Comparisons ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { int [ ] A = { - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 } ; int N = A . Length ; int X = 1 ; findElement ( A , N , X ) ; } }


"Check if an encoding represents a unique binary string | C # program to check if given encoding represents a single string . ; Return true if sum becomes k ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isUnique ( int [ ] a , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; sum += n - 1 ; return ( sum == k ) ; } static public void Main ( ) { int [ ] a = { 3 , 3 , 3 } ; int n = a . Length ; int k = 12 ; if ( isUnique ( a , n , k ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if an integer can be expressed as a sum of two semi | C # Code to check if an integer can be expressed as sum of two semi - primes ; Utility function to compute semi - primes in a range ; Increment count of prime numbers ; If number is greater than 1 , add it to the count variable as it indicates the number remain is prime number ; if count is equal to '2' then number is semi - prime ; Utility function to check if a number is sum of two semi - primes ; arr [ i ] is already a semi - prime if n - arr [ i ] is also a semi - prime then a number can be expressed as sum of two semi - primes ; Driver code"
Solution in C#:

using System . Collections . Generic ; class GFG { static int MAX = 1000000 ; static List < int > arr = new List < int > ( ) ; static bool [ ] sprime = new bool [ MAX ] ; static void computeSemiPrime ( ) { for ( int i = 0 ; i < MAX ; i ++ ) sprime [ i ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { int cnt = 0 ; int num = i ; for ( int j = 2 ; cnt < 2 && j * j <= num ; ++ j ) { while ( num % j == 0 ) { num /= j ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; if ( cnt == 2 ) { sprime [ i ] = true ; arr . Add ( i ) ; } } } static bool checkSemiPrime ( int n ) { int i = 0 ; while ( arr [ i ] <= n / 2 ) { if ( sprime [ n - arr [ i ] ] ) { return true ; } i ++ ; } return false ; } public static void Main ( ) { computeSemiPrime ( ) ; int n = 30 ; if ( checkSemiPrime ( n ) ) System . Console . WriteLine ( " YES " ) ; else System . Console . WriteLine ( " NO " ) ; } }


"Check if an integer is rotation of another given integer | C # implementation of the approach ; Function to check if the integer A is a rotation of the integer B ; Stores the count of digits in A ; Stores the count of digits in B ; If dig1 not equal to dig2 ; Stores position of first digit ; Stores the first digit ; Rotate the digits of the integer ; If A is equal to B ; If A is equal to the initial value of integer A ; Driver Code"
Solution in C#:

using System ; public class GFG { static int check ( int A , int B ) { if ( A == B ) { return 1 ; } int dig1 = ( int ) Math . Floor ( Math . Log10 ( A ) + 1 ) ; int dig2 = ( int ) Math . Floor ( Math . Log10 ( B ) + 1 ) ; if ( dig1 != dig2 ) { return 0 ; } int temp = A ; while ( true ) { int power = ( int ) Math . Pow ( 10 , dig1 - 1 ) ; int firstdigit = A / power ; A = A - firstdigit * power ; A = A * 10 + firstdigit ; if ( A == B ) { return 1 ; } if ( A == temp ) { return 0 ; } } } public static void Main ( String [ ] args ) { int A = 967 , B = 679 ; if ( check ( A , B ) == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if any King is unsafe on the Chessboard or not |  ; Function to check if any of the two kings is unsafe or not ; Find the position of both the kings ; Check for all pieces which can attack White King ; Check for Knight ; Check for Pawn ; Check for Rook ; Check for Bishop ; Check for Queen ; Check for King ; Check for all pieces which can attack Black King ; Check for Knight ; Check for Pawn ; Check for Rook ; Check for Bishop ; Check for Queen ; Check for King ; Store all possible moves of the king ; Incrementing index values ; Checking boundary conditions and character match ; Function to check if Queen can attack the King ; Queen 's moves are a combination  of both the Bishop and the Rook ; Function to check if bishop can attack the king ; Check the lower right diagonal ; Check the lower left diagonal ; Check the upper right diagonal ; Check the upper left diagonal ; Check if ; Check downwards ; Check upwards ; Check right ; Check left ; Check if the knight can attack the king ; All possible moves of the knight ; Incrementing index values ; Checking boundary conditions and character match ; Function to check if pawn can attack the king ; Check for white pawn ; Check for black pawn ; Check if the indices are within the matrix or not ; Checking boundary conditions ; Driver Code ; Chessboard instance"
Solution in C#:

using System ; class GFG { private static int checkBoard ( char [ , ] board ) { for ( int i = 0 ; i < 8 ; i ++ ) { for ( int j = 0 ; j < 8 ; j ++ ) { if ( board [ i , j ] == ' k ' ) { if ( lookForn ( board , ' N ' , i , j ) ) return 1 ; if ( lookForp ( board , ' P ' , i , j ) ) return 1 ; if ( lookForr ( board , ' R ' , i , j ) ) return 1 ; if ( lookForb ( board , ' B ' , i , j ) ) return 1 ; if ( lookForq ( board , ' Q ' , i , j ) ) return 1 ; if ( lookFork ( board , ' K ' , i , j ) ) return 1 ; } if ( board [ i , j ] == ' K ' ) { if ( lookForn ( board , ' n ' , i , j ) ) return 2 ; if ( lookForp ( board , ' p ' , i , j ) ) return 2 ; if ( lookForr ( board , ' r ' , i , j ) ) return 2 ; if ( lookForb ( board , ' b ' , i , j ) ) return 2 ; if ( lookForq ( board , ' q ' , i , j ) ) return 2 ; if ( lookFork ( board , ' k ' , i , j ) ) return 2 ; } } } return 0 ; } private static bool lookFork ( char [ , ] board , char c , int i , int j ) { int [ ] x = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; int [ ] y = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; for ( int k = 0 ; k < 8 ; k ++ ) { int m = i + x [ k ] ; int n = j + y [ k ] ; if ( inBounds ( m , n ) && board [ m , n ] == c ) return true ; } return false ; } private static bool lookForq ( char [ , ] board , char c , int i , int j ) { if ( lookForb ( board , c , i , j ) || lookForr ( board , c , i , j ) ) return true ; return false ; } private static bool lookForb ( char [ , ] board , char c , int i , int j ) { int k = 0 ; while ( inBounds ( i + ++ k , j + k ) ) { if ( board [ i + k , j + k ] == c ) return true ; if ( board [ i + k , j + k ] != ' - ' ) break ; } k = 0 ; while ( inBounds ( i + ++ k , j - k ) ) { if ( board [ i + k , j - k ] == c ) return true ; if ( board [ i + k , j - k ] != ' - ' ) break ; } k = 0 ; while ( inBounds ( i - ++ k , j + k ) ) { if ( board [ i - k , j + k ] == c ) return true ; if ( board [ i - k , j + k ] != ' - ' ) break ; } k = 0 ; while ( inBounds ( i - ++ k , j - k ) ) { if ( board [ i - k , j - k ] == c ) return true ; if ( board...


"Check if any large number is divisible by 17 or not | C # Program to validate the above logic ; Function to check if the number is divisible by 17 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 17 ; Driver code"
Solution in C#:

using System ; class GFG { static bool isDivisible ( long n ) { while ( n / 100 > 0 ) { long d = n % 10 ; n /= 10 ; n -= d * 5 ; } return ( n % 17 == 0 ) ; } public static void Main ( ) { long n = 19877658 ; if ( isDivisible ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if any large number is divisible by 19 or not | C # Program to validate the above logic ; Function to check if the number is divisible by 19 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 19 ; Driver code"
Solution in C#:

using System ; class GFG { static bool isDivisible ( long n ) { while ( n / 100 > 0 ) { long d = n % 10 ; n /= 10 ; n += d * 2 ; } return ( n % 19 == 0 ) ; } public static void Main ( ) { long n = 101156 ; if ( isDivisible ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | C # program for the above approach ; Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1' ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void rotateString ( int n , int m , string s ) { List < int > v = new List < int > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { v . Add ( i ) ; } } for ( int i = 1 ; i < v . Count ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( v . Count >= 2 && ( n - ( v [ v . Count - 1 ] - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { string S = "101001" ; int M = 1 ; int N = S . Length ; rotateString ( N , M , S ) ; } }


"Check if any pair of semicircles intersect or not | C # program for the above approach ; Function to check if any pairs of the semicircles intersects or not ; Stores the coordinates of all the semicircles ; x and y are coordinates ; Store the minimum and maximum value of the pair ; Push the pair in vector ; Compare one pair with other pairs ; Generating the second pair ; Extract all the second pairs one by one ; 1 st condition ; 2 nd condition ; If any one condition is true ; If any pair of semicircles doesn 't exists ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static bool checkIntersection ( int [ ] arr , int N ) { List < pair > vec = new List < pair > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int x = arr [ i ] , y = arr [ i + 1 ] ; int minn = Math . Min ( x , y ) ; int maxx = Math . Max ( x , y ) ; vec . Add ( new pair ( minn , maxx ) ) ; } for ( int i = 0 ; i < vec . Count ; i ++ ) { pair x = vec [ i ] ; for ( int j = 0 ; j < vec . Count ; j ++ ) { pair y = vec [ j ] ; bool cond1 = ( x . first < y . first && x . second < y . second && y . first < x . second ) ; bool cond2 = ( y . first < x . first && y . second < x . second && x . first < y . second ) ; if ( cond1 cond2 ) { return true ; } } } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 15 , 5 , 10 } ; int N = arr . Length ; if ( checkIntersection ( arr , N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if any permutation of N equals any power of K | C # implementation of above approach . ; Function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; Generate all power of K under 10 ^ 18 ; Check if any power of K is valid ; Driver code ; Function call to print required answer"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static bool isValid ( long N , int K ) { HashSet < long > m1 = new HashSet < long > ( ) ; HashSet < int > m2 = new HashSet < int > ( ) ; while ( N > 0 ) { m1 . Add ( N % 10 ) ; N /= 10 ; } while ( K > 0 ) { m2 . Add ( K % 10 ) ; K /= 10 ; } if ( m1 . Equals ( m2 ) ) { return true ; } return false ; } static String anyPermutation ( long N , int K ) { int [ ] powK = new int [ 100 + 1 ] ; int Limit = ( int ) Math . Pow ( 10 , 18 ) ; powK [ 0 ] = K ; int i = 1 ; while ( powK [ i - 1 ] * K < Limit && i < 100 ) { powK [ i ] = powK [ i - 1 ] * K ; i ++ ; } for ( int j = 0 ; j < i ; j ++ ) { if ( ! isValid ( N , powK [ j ] ) ) { return " True " ; } } return " False " ; } public static void Main ( String [ ] args ) { long N = 96889010407 ; int K = 7 ; Console . WriteLine ( anyPermutation ( N , K ) ) ; } }


"Check if any permutation of a large number is divisible by 8 | C # program to check if any permutation of a large number is divisible by 8 or not ; Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Utility reverse function ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static bool solve ( String n , int l ) { if ( l < 3 ) { if ( int . Parse ( n ) % 8 == 0 ) return true ; n = reverse ( n ) ; if ( int . Parse ( n ) % 8 == 0 ) return true ; return false ; } int [ ] hash = new int [ 10 ] ; for ( int i = 0 ; i < l ; i ++ ) hash [ n [ i ] - '0' ] ++ ; for ( int i = 104 ; i < 1000 ; i += 8 ) { int dup = i ; int [ ] freq = new int [ 10 ] ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = dup / 10 ; freq [ dup % 10 ] ++ ; dup = i ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; dup = dup / 10 ; if ( freq [ dup % 10 ] > hash [ dup % 10 ] ) continue ; return true ; } return false ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String number = "31462708" ; int l = number . Length ; if ( solve ( number , l ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if any permutation of a number is divisible by 3 and is Palindromic | C # implementation of the above approach ; Function to check if any permutation of a number is divisible by 3 and is Palindromic ; Hash array to store frequency of digits of n ; traverse the digits of integer and store their frequency ; Calculate the sum of digits simultaneously ; Check if number is not divisible by 3 ; If more than one digits have odd frequency , palindromic permutation not possible ; Driver Code"
Solution in C#:

using System ; class GFG { static bool isDivisiblePalindrome ( int n ) { int [ ] hash = new int [ 10 ] ; int digitSum = 0 ; while ( n != 0 ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n /= 10 ; } if ( digitSum % 3 != 0 ) return false ; int oddCount = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } static public void Main ( ) { int n = 34734 ; Console . WriteLine ( isDivisiblePalindrome ( n ) ) ; } }


"Check if any permutation of a number without any leading zeros is a power of 2 or not | C # program for the above approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i to n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagrams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Function to check if any permutation of a number is a power of 2 or not ; Iterate over all possible perfect power of 2 ; Print that number ; Driver Code ; Given number N ; Function call"
Solution in C#:

using System ; class GFG { static int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static bool areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } static bool isPowerOf2 ( int N ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( areAnagrams ( 1 << i , N ) ) { Console . Write ( ( 1 << i ) ) ; return true ; } } return false ; } public static void Main ( String [ ] args ) { int N = 46 ; if ( ! isPowerOf2 ( N ) ) { Console . Write ( " No " ) ; } } }


"Check if any permutation of array contains sum of every adjacent pair not divisible by 3 | C # program for the above approach ; Function to checks if any permutation of the array exists whose sum of adjacent pairs is not divisible by 3 ; Count remainder 0 ; Count remainder 1 ; Count remainder 2 ; Condition for valid arrangements ; Driver Code ; Given array [ ] arr ; Function Call"
Solution in C#:

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


"Check if any point exists in a plane whose Manhattan distance is at most K from N given points | C # program for the above approach ; Function to check if there exists any point with at most K distance from N given points ; Traverse the given N points ; Stores the count of pairs of coordinates having Manhattan distance <= K ; For the same coordinate ; Calculate Manhattan distance ; If Manhattan distance <= K ; If all coordinates can meet ; If all coordinates can 't meet ; Driver Code"
Solution in C#:

using System ; class GFG { public static String find ( int [ ] a , int [ ] b , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { continue ; } long dis = Math . Abs ( a [ i ] - a [ j ] ) + Math . Abs ( b [ i ] - b [ j ] ) ; if ( dis <= K ) { count ++ ; } if ( count == N - 1 ) { return " Yes " ; } } } return " No " ; } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] A = { 1 , 0 , 2 , 1 , 1 } ; int [ ] B = { 1 , 1 , 1 , 0 , 2 } ; int K = 1 ; Console . WriteLine ( find ( A , B , N , K ) ) ; } }


"Check if any point overlaps the given Circle and Rectangle | C # implementation to check if any point overlaps the given circle and rectangle ; Function to check if any point overlaps the given circle and rectangle ; Find the nearest point on the rectangle to the center of the circle ; Find the distance between the nearest point and the center of the circle Distance between 2 points , ( x1 , y1 ) & ( x2 , y2 ) in 2D Euclidean space is ( ( x1 - x2 ) * * 2 + ( y1 - y2 ) * * 2 ) * * 0.5 ; Driver code"
Solution in C#:

 using System ; class GFG { static bool checkOverlap ( int R , int Xc , int Yc , int X1 , int Y1 , int X2 , int Y2 ) { int Xn = Math . Max ( X1 , Math . Min ( Xc , X2 ) ) ; int Yn = Math . Max ( Y1 , Math . Min ( Yc , Y2 ) ) ; int Dx = Xn - Xc ; int Dy = Yn - Yc ; return ( Dx * Dx + Dy * Dy ) <= R * R ; } public static void Main ( ) { int R = 1 ; int Xc = 0 , Yc = 0 ; int X1 = 1 , Y1 = - 1 ; int X2 = 3 , Y2 = 1 ; if ( checkOverlap ( R , Xc , Yc , X1 , Y1 , X2 , Y2 ) ) { Console . Write ( " True " + "
 " ) ; } else { Console . Write ( " False " ) ; } } }


"Check if any square ( with one colored cell ) can be divided into two equal parts | C # program to illustrate the above problem ; function to check if it 's possible to divide the square in two equal parts ; if the painted square is linked anyway to the center of the square then it 's not possible ; else yes it 's possible ; Driver code ; initialize the size of the square ; initialize the dimension of the painted square"
Solution in C#:

using System ; class GFG { static void halfsquare ( int n , int x , int y ) { int half = n / 2 ; if ( ( half == x half == x - 1 ) && ( half == y half == y - 1 ) ) Console . WriteLine ( " NO " ) ; else Console . WriteLine ( " YES " ) ; } public static void Main ( ) { int n = 100 ; int x = 51 , y = 100 ; halfsquare ( n , x , y ) ; } }


"Check if any subarray of length M repeats at least K times consecutively or not | C # program for the above approach ; Function to check if any subarray of length M repeats at least K times consecutively or not ; Stores the required count of repeated subarrays ; Check if the next continuous subarray has equal elements ; Check if K continuous subarray of length M are found or not ; If no subarrays are found ; Driver Code"
Solution in C#:

using System ; class GFG { public static bool checkExists ( int [ ] arr , int N , int M , int K ) { int count = 0 ; for ( int i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } public static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int N = arr . Length ; int M = 2 , K = 2 ; if ( checkExists ( arr , N , M , K ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if any subarray of length M repeats at least K times consecutively or not | C # program for the above approach ; Function to check if there exists any subarray of length M repeating at least K times consecutively ; Iterate from i equal 0 to M ; Iterate from j equals 1 to K ; If elements at pos + i and pos + i + j * M are not equal ; Function to check if a subarray repeats at least K times consecutively or not ; Iterate from ind equal 0 to M ; Check if subarray arr [ i , i + M ] repeats atleast K times or not ; Otherwise , return false ; Driver code"
Solution in C#:

using System ; class GFG { static bool check ( int [ ] arr , int M , int K , int ind ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < K ; j ++ ) { if ( arr [ ind + i ] != arr [ ind + i + j * M ] ) { return false ; } } } return true ; } static bool SubarrayRepeatsKorMore ( int [ ] arr , int N , int M , int K ) { for ( int ind = 0 ; ind <= N - M * K ; ind ++ ) { if ( check ( arr , M , K , ind ) ) { return true ; } } return false ; } static void Main ( ) { int [ ] arr = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = arr . Length ; if ( SubarrayRepeatsKorMore ( arr , N , M , K ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if any two intervals intersects among a given set of intervals | C # program to check if any two intervals overlap ; An interval has start time and end time ; Function to check if any two intervals overlap ; Find the overall maximum element ; Initialize an array of size max_ele ; starting point of the interval ; end point of the interval ; Calculating the prefix Sum ; Overlap ; If we reach here , then no Overlap ; Driver Code"
Solution in C#:

 using System ; class GFG { class Interval { public int start ; public int end ; public Interval ( int start , int end ) { this . start = start ; this . end = end ; } } ; static bool isIntersect ( Interval [ ] arr , int n ) { int max_ele = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_ele < arr [ i ] . end ) max_ele = arr [ i ] . end ; } int [ ] aux = new int [ max_ele + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] . start ; int y = arr [ i ] . end ; aux [ x ] ++ ; aux [ y ] -- ; } for ( int i = 1 ; i <= max_ele ; i ++ ) { aux [ i ] += aux [ i - 1 ] ; if ( aux [ i ] > 1 ) return true ; } return false ; } public static void Main ( String [ ] args ) { Interval [ ] arr1 = { new Interval ( 1 , 3 ) , new Interval ( 7 , 9 ) , new Interval ( 4 , 6 ) , new Interval ( 10 , 13 ) } ; int n1 = arr1 . Length ; if ( isIntersect ( arr1 , n1 ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; Interval [ ] arr2 = { new Interval ( 6 , 8 ) , new Interval ( 1 , 3 ) , new Interval ( 2 , 4 ) , new Interval ( 4 , 7 ) } ; int n2 = arr2 . Length ; if ( isIntersect ( arr2 , n2 ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } }


"Check if any valid sequence is divisible by M |  ; Base case ; Check if sum is divisible by M ; 1. Try placing ' + ' ; 2. Try placing ' - '"
Solution in C#:

static bool isPossible ( int index , int sum ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } bool placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; bool placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; if ( placeAdd placeMinus ) return true ; return false ; }


"Check if any valid sequence is divisible by M | C # implementation of above approach ; Calculate modulo for this call ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code ; MAX is the Maximum value M can take"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int isPossible ( int n , int index , int modulo , int M , int [ ] arr , int [ , ] dp ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) { return 1 ; } return 0 ; } if ( dp [ index , modulo ] != - 1 ) { return dp [ index , modulo ] ; } int placeAdd = isPossible ( n , index + 1 , modulo + arr [ index ] , M , arr , dp ) ; int placeMinus = isPossible ( n , index + 1 , modulo - arr [ index ] , M , arr , dp ) ; int res = placeAdd ; dp [ index , modulo ] = res ; return res ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 6 } ; int n = arr . Length ; int M = 4 ; int [ , ] dp = new int [ n + 1 , MAX ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } bool res ; if ( isPossible ( n , 1 , arr [ 0 ] , M , arr , dp ) == 1 ) { res = true ; } else { res = false ; } Console . WriteLine ( res ? " True " : " False " ) ; } }


"Check if any valid sequence is divisible by M | C # program for the above approach ; Function to check if any valid sequence is divisible by M ; Declare mod array ; Calculate the mod array ; Check if sum is divisible by M ; Check if sum is not divisible by 2 ; Remove the first element from the ModArray since it is not possible to place minus on the first element ; Decrease the size of array ; Sort the array ; Loop until the pointer cross each other ; Check if sum becomes equal ; Increase and decrease the pointer accordingly ; Driver code ; Function call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static void func ( int n , int m , int [ ] A ) { List < int > ModArray = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ModArray . Add ( 0 ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ModArray [ i ] = ( A [ i ] % m ) ; sum += ( ( int ) ModArray [ i ] ) ; } sum = sum % m ; if ( sum % m == 0 ) { Console . WriteLine ( " True " ) ; return ; } if ( sum % 2 != 0 ) { Console . WriteLine ( " False " ) ; } else { ModArray . Remove ( 0 ) ; int i = 0 ; int j = ModArray . Count - 1 ; ModArray . Sort ( ) ; sum = sum / 2 ; int i1 , i2 ; while ( i <= j ) { int s = ( int ) ModArray [ i ] + ( int ) ModArray [ j ] ; if ( s == sum ) { i1 = i ; i2 = j ; Console . WriteLine ( " True " ) ; break ; } else if ( s > sum ) j -- ; else i ++ ; } } } public static void Main ( ) { int m = 2 ; int [ ] a = { 1 , 3 , 9 } ; int n = a . Length ; func ( n , m , a ) ; } }


"Check if any valid sequence is divisible by M | C # program to check if any valid sequence is divisible by M ; Base case ; check if sum is divisible by M ; check if the current state is already computed ; 1. Try placing ' + ' ; 2. Try placing ' - ' ; calculate value of res for recursive case ; store the value for res for current states and return for parent call ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 1000 ; static Boolean isPossible ( int n , int index , int sum , int M , int [ ] arr , int [ , ] dp ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } else if ( sum < 0 sum >= MAX ) return false ; if ( dp [ index , sum ] != - 1 ) { if ( dp [ index , sum ] == 0 ) return false ; return true ; } Boolean placeAdd = isPossible ( n , index + 1 , sum + arr [ index ] , M , arr , dp ) ; Boolean placeMinus = isPossible ( n , index + 1 , sum - arr [ index ] , M , arr , dp ) ; Boolean res = ( placeAdd placeMinus ) ; dp [ index , sum ] = ( res ) ? 1 : 0 ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 6 } ; int n = arr . Length ; int M = 4 ; int [ , ] dp = new int [ n + 1 , MAX ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i , j ] = - 1 ; Boolean res ; res = isPossible ( n , 0 , 0 , M , arr , dp ) ; Console . WriteLine ( ( res ? " True " : " False " ) ) ; } }


"Check if array can be converted into strictly decreasing sequence | C # implementation to check that array can be converted into a strictly decreasing sequence ; Function to check that array can be converted into a strictly decreasing sequence ; Loop to check that each element is greater than the ( N - index ) ; If element is less than ( N - index ) ; If array can be converted ; Driver Code ; Function calling"
Solution in C#:

 using System ; class GFG { static bool check ( int [ ] arr , int n ) { bool flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < n - i ) { flag = false ; } } if ( flag ) { return true ; } else { return false ; } } static public void Main ( String [ ] args ) { int [ ] arr1 = { 11 , 11 , 11 , 11 } ; int n1 = arr1 . Length ; if ( check ( arr1 , n1 ) ) { Console . Write ( " Yes " + "
 " ) ; } else { Console . Write ( " No " + "
 " ) ; } } }


"Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array | C # implementation of the above approach ; Function to check if it is possible to sort array or not ; Store the smallest element ; Copy the original array ; Iterate over the given array ; Update smallest element ; Copy elements of [ ] arr to array [ ] B ; Sort original array ; Iterate over the given array ; If the i - th element is not in its sorted place ; Not possible to swap ; Driver Code ; Given array ; Function Call"
Solution in C#:

using System ; class GFG { static void isPossible ( int [ ] arr , int N ) { int mn = int . MaxValue ; int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { mn = Math . Min ( mn , arr [ i ] ) ; B [ i ] = arr [ i ] ; } Array . Sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != B [ i ] ) { if ( B [ i ] % mn != 0 ) { Console . Write ( " No " ) ; return ; } } } Console . Write ( " Yes " ) ; return ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 4 , 3 , 6 , 6 , 2 , 9 } ; isPossible ( arr , N ) ; } }


"Check if array can be sorted with one swap | A linear C # program to check if array becomes sorted after one swap ; Find counts and positions of elements that are out of order . ; If there are more than two elements are out of order . ; If all elements are sorted already ; Cases like { 1 , 5 , 3 , 4 , 2 } We swap 5 and 2. ; Cases like { 1 , 2 , 4 , 3 , 5 } ; Now check if array becomes sorted for cases like { 4 , 1 , 2 , 3 } ; Driver Code"
Solution in C#:

using System ; class GFG { static bool checkSorted ( int n , int [ ] arr ) { int first = 0 , second = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { count ++ ; if ( first == 0 ) first = i ; else second = i ; } } if ( count > 2 ) return false ; if ( count == 0 ) return true ; if ( count == 2 ) swap ( arr , first - 1 , second ) ; else if ( count == 1 ) swap ( arr , first - 1 , first ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] < arr [ i - 1 ] ) return false ; return true ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 3 , 2 } ; int n = arr . Length ; if ( checkSorted ( n , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if array can be sorted with one swap | C # program to check if an array can be sorted with at - most one swap ; Create a sorted copy of original array ; Check if 0 or 1 swap required to get the sorted array ; Driver Code"
Solution in C#:

using System ; class GFG { static Boolean checkSorted ( int n , int [ ] arr ) { int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = arr [ i ] ; Array . Sort ( b , 0 , n ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != b [ i ] ) ct ++ ; if ( ct == 0 ct == 2 ) return true ; else return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . Length ; if ( checkSorted ( n , arr ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if array elements are consecutive | Added Method 3 |  ; The function checks if the array elements are consecutive If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) max - min + 1 is equal to n , then only check all elements ; Create a temp array to hold visited flag of all elements . Note that , calloc is used here so that all values are initialized as false ; If we see an element again , then return false ; If visited first time , then mark the element as visited ; If all elements occur once , then return true ; if ( max - min + 1 != n ) ; UTILITY FUNCTIONS ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static bool areConsecutive ( int [ ] arr , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { bool [ ] visited = new bool [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i ] - min ] != false ) return false ; visited [ arr [ i ] - min ] = true ; } return true ; } return false ; } static int getMin ( int [ ] arr , int n ) { int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } static int getMax ( int [ ] arr , int n ) { int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void Main ( ) { int [ ] arr = { 5 , 4 , 2 , 3 , 1 , 6 } ; int n = arr . Length ; if ( areConsecutive ( arr , n ) == true ) Console . Write ( " Array ▁ elements ▁ are " + " ▁ consecutive " ) ; else Console . Write ( " Array ▁ elements ▁ are " + " ▁ not ▁ consecutive " ) ; } }


"Check if array elements are consecutive | Added Method 3 |  ; The function checks if the array elements are consecutive If elements are consecutive , then returns true , else returns false ; 1 ) Get the minimum element in array ; 2 ) Get the maximum element in array ; 3 ) max - min + 1 is equal to n then only check all elements ; if the value at index j is negative then there is repitition ; If we do not see a negative value then all elements are distinct ; if ( max - min + 1 != n ) ; UTILITY FUNCTIONS ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static bool areConsecutive ( int [ ] arr , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { int j ; if ( arr [ i ] < 0 ) j = - arr [ i ] - min ; else j = arr [ i ] - min ; if ( arr [ j ] > 0 ) arr [ j ] = - arr [ j ] ; else return false ; } return true ; } return false ; } static int getMin ( int [ ] arr , int n ) { int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } static int getMax ( int [ ] arr , int n ) { int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void Main ( ) { int [ ] arr = { 5 , 4 , 2 , 3 , 1 , 6 } ; int n = arr . Length ; if ( areConsecutive ( arr , n ) == true ) Console . Write ( " Array ▁ elements ▁ " + " are ▁ consecutive " ) ; else Console . Write ( " Array ▁ elements ▁ " + " are ▁ not ▁ consecutive " ) ; } }


"Check if array sum can be made K by three operations on it | C # Program to find if Array can have a sum of K by applying three types of possible ; Check if it is possible to achieve a sum with three operation allowed . ; If sum is negative . ; If going out of bound . ; If sum is achieved . ; If the current state is not evaluated yet . ; Replacing element with negative value of the element . ; Substracting index number from the element . ; Adding index number to the element . ; Wrapper Function ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int check ( int i , int sum , int n , int k , int [ ] a , int [ , ] dp ) { if ( sum <= 0 ) { return 0 ; } if ( i >= n ) { if ( sum == k ) { return 1 ; } return 0 ; } if ( dp [ i , sum ] != - 1 ) { return dp [ i , sum ] ; } dp [ i , sum ] = check ( i + 1 , sum - 2 * a [ i ] , n , k , a , dp ) | check ( i + 1 , sum , n , k , a , dp ) ; dp [ i , sum ] = check ( i + 1 , sum - ( i + 1 ) , n , k , a , dp ) | dp [ i , sum ] ; dp [ i , sum ] = check ( i + 1 , sum + i + 1 , n , k , a , dp ) | dp [ i , sum ] ; return dp [ i , sum ] ; } static int wrapper ( int n , int k , int [ ] a ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } int [ , ] dp = new int [ MAX , MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i , j ] = - 1 ; } } return check ( 0 , sum , n , k , a , dp ) ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 5 ; if ( wrapper ( n , k , a ) == 1 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if binary representation of a given number and its complement are anagram | An efficient C # program to check if binary representations of a number and it 's complement are anagram. ; Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code"
Solution in C#:

using System ; class GFG { static byte longSize = 8 ; static int ULL_SIZE = 8 * longSize ; static bool bit_anagram_check ( long a ) { return ( BitCount ( ( int ) a ) == ( ULL_SIZE >> 1 ) ) ; } static int BitCount ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static void Main ( String [ ] args ) { long a = 4294967295L ; Console . WriteLine ( bit_anagram_check ( a ) ) ; } }


"Check if binary representations of 0 to N are present as substrings in given binary string | C # program for the above approach ; Function to convert decimal to binary representation ; Iterate over all bits of N ; If bit is 1 ; Return binary representation ; Function to check if binary conversion of numbers from N to 1 exists in the String as a subString or not ; To store the count of number exists as a subString ; Traverse from N to 1 ; If current number is not present in map ; Store current number ; Find binary of t ; If the String s is a subString of str ; Mark t as true ; Increment the count ; Update for t / 2 ; Special judgment '0' ; If the count is N + 1 , return " yes " ; Driver Code ; Given String ; Given number ; Function call"
Solution in C#:

using System ; class GFG { static String decimalToBinary ( int N ) { String ans = " " ; while ( N > 0 ) { if ( N % 2 == 1 ) { ans = '1' + ans ; } else { ans = '0' + ans ; } N /= 2 ; } return ans ; } static String checkBinaryString ( String str , int N ) { int [ ] map = new int [ N + 10 ] ; int cnt = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( map [ i ] == 0 ) { int t = i ; String s = decimalToBinary ( t ) ; if ( str . Contains ( s ) ) { while ( t > 0 && map [ t ] == 0 ) { map [ t ] = 1 ; cnt ++ ; t >>= 1 ; } } } } for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] == '0' ) { cnt ++ ; break ; } } if ( cnt == N + 1 ) return " True " ; else return " False " ; } public static void Main ( String [ ] args ) { String str = "0110" ; int N = 3 ; Console . Write ( checkBinaryString ( str , N ) ) ; } }


"Check if binary string multiple of 3 using DFA | C # program to check if the binary String is divisible by 3. ; Function to check if the binary String is divisible by 3. ; checking if the bit is nonzero ; checking if the nonzero bit is at even position ; Checking if the difference of non - zero oddbits and evenbits is divisible by 3. ; Driver Program"
Solution in C#:

 using System ; public class GFG { static void CheckDivisibilty ( String A ) { int oddbits = 0 , evenbits = 0 ; for ( int counter = 0 ; counter < A . Length ; counter ++ ) { if ( A [ counter ] == '1' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( Math . Abs ( oddbits - evenbits ) % 3 == 0 ) { Console . Write ( " Yes " + "
 " ) ; } else { Console . Write ( " No " + "
 " ) ; } } public static void Main ( String [ ] args ) { String A = "10101" ; CheckDivisibilty ( A ) ; } }


"Check if bits in range L to R of two numbers are complement of each other or not | C # implementation to check whether all the bits in the given range of two numbers are complement of each other ; function to check whether all the bits are set in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if both are equal , then all bits are set in the given range ; else all bits are not set ; function to check whether all the bits in the given range of two numbers are complement of each other ; Driver Code"
Solution in C#:

using System ; class GFG { static bool allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( num == new_num ) return true ; return false ; } static bool bitsAreComplement ( int a , int b , int l , int r ) { int xor_value = a ^ b ; return allBitsSetInTheGivenRange ( xor_value , l , r ) ; } static public void Main ( ) { int a = 10 , b = 5 ; int l = 1 , r = 3 ; if ( bitsAreComplement ( a , b , l , r ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if bits of a number has count of consecutive set bits in increasing order | C # program to check if counts of consecutive 1 s are increasing order . ; Returns true if n has counts of consecutive 1 's are increasing  order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code"
Solution in C#:

using System ; class GFG { static bool areSetBitsIncreasing ( int n ) { int prev_count = int . MaxValue ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = n / 2 ; int curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } static public void Main ( ) { int n = 10 ; if ( areSetBitsIncreasing ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if bitwise AND of any subset is power of two | C # Program to check if Bitwise AND of any subset is power of two ; Check for power of 2 or not ; Check if there exist a subset whose bitwise AND is power of 2. ; if there is only one element in the set . ; Finding a number with all bit sets . ; check all the positions at which the bit is set . ; include all those elements whose i - th bit is set ; check for the set contains elements make a power of 2 or not ; Driver Code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int NUM_BITS = 32 ; static bool isPowerOf2 ( int num ) { if ( num != 0 && ( num & ( num - 1 ) ) == 0 ) return true ; return false ; } static bool checkSubsequence ( int [ ] arr , int n ) { if ( n == 1 ) return isPowerOf2 ( arr [ 0 ] ) ; int total = 0 ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) total = total | ( 1 << i ) ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) { int ans = total ; for ( int j = 0 ; j < n ; j ++ ) { int p = arr [ j ] & ( 1 << i ) ; if ( p == 0 ) ans = ans & arr [ j ] ; } if ( isPowerOf2 ( ans ) ) return true ; } return false ; } public static void Main ( ) { int [ ] arr = { 12 , 13 , 7 } ; int n = arr . Length ; if ( checkSubsequence ( arr , n ) ) Console . Write ( " YES
 " ) ; else Console . Write ( " NO
 " ) ; } }


"Check if both halves of a string are Palindrome or not | C # program to check whether both halves of a string is Palindrome or not ; Function to check and return if both halves of a string are palindromic or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver code"
Solution in C#:

using System ; class GFG { public static void checkPalindrome ( String S ) { int N = S . Length ; bool first_half = true ; bool second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S [ i ] != S [ cnt ] ) { first_half = false ; break ; } if ( S [ N / 2 + i ] != S [ N / 2 + cnt ] ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { String S = " momdad " ; checkPalindrome ( S ) ; } }


"Check if both halves of the string have at least one different character | C # implementation of the problem ; Function which break string into two halves Increments frequency of characters for first half Decrements frequency of characters for second half true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; static bool function ( String str ) { int l = str . Length ; int [ ] counter = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) counter [ i ] = 0 ; for ( int i = 0 ; i < l / 2 ; i ++ ) counter [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter [ str [ i ] - ' a ' ] -- ; for ( int i = 0 ; i < MAX ; i ++ ) if ( counter [ i ] != 0 ) return true ; return false ; } public static void Main ( ) { string str = " abcasdsabcae " ; if ( function ( str ) ) Console . Write ( " Yes , ▁ both ▁ halves " + " ▁ differ ▁ by ▁ at ▁ least ▁ one ▁ " + " character " ) ; else Console . Write ( " No , ▁ both ▁ halves " + " ▁ do ▁ not ▁ differ ▁ at ▁ all " ) ; } }


"Check if both halves of the string have at least one different character | C # implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Counts frequency of characters in each half Compares the two counter array and returns true if these counter arrays differ ; Declaration and initialization of counter array ; Driver function"
Solution in C#:

using System ; class GeeksforGeeks { static int MAX = 26 ; static bool function ( String str ) { int l = str . Length ; int [ ] counter1 = new int [ MAX ] ; int [ ] counter2 = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { counter1 [ i ] = 0 ; counter2 [ i ] = 0 ; } for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str [ i ] - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( counter2 [ i ] != counter1 [ i ] ) return true ; } return false ; } public static void Main ( ) { String str = " abcasdsabcae " ; if ( function ( str ) ) Console . WriteLine ( " Yes , ▁ both ▁ halves ▁ " + " differ ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else Console . WriteLine ( " No , ▁ both ▁ halves ▁ " + " do ▁ not ▁ differ ▁ at ▁ all " ) ; } }


"Check if both halves of the string have at least one different character | C # implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver code"
Solution in C#:

using System ; class GFG { static Boolean function ( char [ ] str ) { int l = str . Length ; Array . Sort ( str , 0 , ( l / 2 ) ) ; Array . Sort ( str , ( l / 2 ) , l - ( l / 2 ) ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } public static void Main ( String [ ] args ) { char [ ] str = ( " abcasdsabcae " ) . ToCharArray ( ) ; if ( function ( str ) ) Console . WriteLine ( " Yes , ▁ both ▁ halves ▁ differ " + " ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else Console . WriteLine ( " No , ▁ both ▁ halves ▁ do " + " ▁ not ▁ differ ▁ at ▁ all " ) ; } }


"Check if both halves of the string have same set of characters | C # program to check if it two half of string contain same Character set or not ; function to check both halves for equality ; Counter array initialized with 0 ; Length of the string ; traverse till the middle element is reached ; First half ; Second half ; Checking if values are different set flag to 1 ; Driver program to test above function ; String to be checked"
Solution in C#:

using System ; class GFG { static int MAX_CHAR = 26 ; static bool checkCorrectOrNot ( string s ) { int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; int n = s . Length ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count1 [ s [ i ] - ' a ' ] ++ ; count2 [ s [ j ] - ' a ' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void Main ( ) { string s = " abab " ; if ( checkCorrectOrNot ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


using System ; public class GFG { static int MAX_CHAR = 26 ; static bool checkCorrectOrNot ( String s ) { int [ ] count = new int [ MAX_CHAR ] ; int n = s . Length ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - ' a ' ] ++ ; count [ s [ j ] - ' a ' ] -- ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } public static void Main ( String [ ] args ) { String s = " abab " ; if ( checkCorrectOrNot ( s ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }


"Check if ceil of number divided by power of two exist in sorted array | C # implementation to check if a number divided by power of two exist in the sorted array ; Function to find there exist a number or not in the array ; Loop to check if there exist a number by divided by power of 2 ; Binary Search ; Condition to check the number is found in the array or not ; Otherwise divide the number by increasing the one more power of 2 ; Driver Code"
Solution in C#:

using System ; class GFG { static int findNumberDivByPowerofTwo ( int [ ] ar , int k , int n ) { int found = - 1 , m = k ; while ( m > 0 ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != - 1 ) { break ; } m = m / 2 ; } return found ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 7 , 8 , 10 } ; int k = 4 , n = 5 ; Console . WriteLine ( findNumberDivByPowerofTwo ( arr , k , n ) ) ; } }


"Check if characters of a given string can be rearranged to form a palindrome | C # Implementation of the above approach ; bitvector to store the record of which character appear odd and even number of times ; Driver Code"
Solution in C#:

using System ; public class GFG { static bool canFormPalindrome ( string a ) { int bitvector = 0 , mask = 0 ; for ( int i = 0 ; i < a . Length ; i ++ ) { int x = a [ i ] - ' a ' ; mask = 1 << x ; bitvector = bitvector ^ mask ; } return ( bitvector & ( bitvector - 1 ) ) == 0 ; } static public void Main ( ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if characters of a given string can be rearranged to form a palindrome | C # implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code"
Solution in C#:

using System ; class GFG { static int NO_OF_CHARS = 256 ; static bool canFormPalindrome ( string str ) { int [ ] count = new int [ NO_OF_CHARS ] ; Array . Fill ( count , 0 ) ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ ( int ) ( str [ i ] ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void Main ( ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Check if characters of a string can be made non | C # program for the above approach ; Function to check if it 's possible  to make the String s non decreasing ; Stores length of String ; Stores the last character that is not ' ? ' ; Traverse the String S ; If current character of the String is ' ? ' ; If s [ i ] is not ' ? ' and is less than C ; Otherwise ; Update C ; Return 1 ; Driver Code"
Solution in C#:

 using System ; public class GFG { static int nonDecreasing ( char [ ] s ) { int size = s . Length ; char c = ' a ' ; for ( int i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ? ' ) { continue ; } else if ( ( s [ i ] != ' ? ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } public static void Main ( String [ ] args ) { String S = " abb ? xy ? " ; if ( nonDecreasing ( S . ToCharArray ( ) ) == 1 ) Console . Write ( " Yes " + "
 " ) ; else Console . Write ( " No " + "
 " ) ; } }


"Check if characters of one string can be swapped to form other | C # program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code"
Solution in C#:

using System ; class GFG { static int MAX = 26 ; static bool targetstring ( string str1 , string str2 ) { int l1 = str1 . Length ; int l2 = str2 . Length ; if ( l1 != l2 ) return false ; int [ ] map = new int [ MAX ] ; Array . Clear ( map , 0 , 26 ) ; for ( int i = 0 ; i < l1 ; i ++ ) map [ str1 [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) { map [ str2 [ i ] - ' a ' ] -- ; if ( map [ str2 [ i ] - ' a ' ] < 0 ) return false ; } return true ; } static void Main ( ) { string str1 = " geeksforgeeks " ; string str2 = " geegeeksksfor " ; if ( targetstring ( str1 , str2 ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }


"Check if concatenation of any permutation of given list of arrays generates the given array | C # program for the above approach ; Function to check if it is possible to obtain array by concatenating the arrays in list pieces [ ] ; Stores the index of element in the given array arr [ ] ; Traverse over the list pieces ; If item size is 1 and exists in map ; If item contains > 1 element then check order of element ; If end of the array ; Check the order of elements ; If order is same as the array elements ; Increment idx ; If order breaks ; Otherwise ; Return false if the first element doesn 't exist in m ; Return true ; Driver Code ; Given target list ; Given array of list ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; using System . Linq ; class GFG { static bool check ( List < int > arr , List < List < int > > pieces ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) m . Add ( arr [ i ] , i ) ; for ( int i = 0 ; i < pieces . Count ; i ++ ) { if ( pieces [ i ] . Count == 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { continue ; } else if ( pieces [ i ] . Count > 1 && m . ContainsKey ( pieces [ i ] [ 0 ] ) ) { int idx = m [ pieces [ i ] [ 0 ] ] ; idx ++ ; if ( idx >= arr . Count ) return false ; for ( int j = 1 ; j < pieces [ i ] . Count ; j ++ ) { if ( arr [ idx ] == pieces [ i ] [ j ] ) { idx ++ ; if ( idx >= arr . Count && j < pieces [ i ] . Count - 1 ) return false ; } else { return false ; } } } else { return false ; } } return true ; } static public void Main ( ) { List < int > arr = new List < int > ( ) { 1 , 2 , 4 , 3 } ; List < List < int > > pieces = new List < List < int > > ( ) ; pieces . Add ( new List < int > ( ) { 1 } ) ; pieces . Add ( new List < int > ( ) { 4 , 3 } ) ; pieces . Add ( new List < int > ( ) { 2 } ) ; if ( check ( arr , pieces ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }


"Check if concatenation of first and last digits forms a prime number or not for each array element | C # program for above approach package whatever do not write package name here ; Stores if i is prime ( 1 ) or non - prime ( 0 ) ; Function to build sieve array ; Inititalize all the values in sieve equals to 1 ; Sieve of Eratosthenes ; If current number is prime ; Set all multiples as non - prime ; Function to check if the numbers formed by combining first and last digits generates a prime number or not ; Check if any of the numbers formed is a prime number or not ; Traverse the array of queries ; Extract the last digit ; Extract the first digit ; If any of the two numbers is prime ; Otherwise ; Driver code ; Computes and stores primes using Sieve ; Function call to perform queries"
Solution in C#:

 using System ; public class GFG { static int [ ] sieve = new int [ 105 ] ; static void buildSieve ( ) { for ( int i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( int i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( int j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } static bool isAnyPrime ( int first , int last ) { int num1 = first * 10 + last ; int num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } static void performQueries ( int [ ] q ) { for ( int i = 0 ; i < q . Length ; i ++ ) { int A = q [ i ] ; int last = A % 10 ; int first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) Console . Write ( " True
 " ) ; else Console . Write ( " False
 " ) ; } } public static void Main ( String [ ] args ) { int [ ] q = { 30 , 66 } ; buildSieve ( ) ; performQueries ( q ) ; } }


"Check if concatenation of two strings is balanced or not | C # program to check if sequence obtained by concatenating two bracket sequences is balanced or not . ; Check if given string is balanced bracket sequence or not . ; If current bracket is an opening bracket push it to stack . ; If current bracket is a closing bracket then pop from stack if it is not empty . If stack is empty then sequence is not balanced . ; If stack is not empty , then sequence is not balanced . ; Function to check if string obtained by concatenating two bracket sequences is balanced or not . ; Check if s1 + s2 is balanced or not . ; Check if s2 + s1 is balanced or not . ; Driver code ."
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static bool isBalanced ( String s ) { Stack < char > st = new Stack < char > ( ) ; int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) { st . Push ( s [ i ] ) ; } else if ( st . Count == 0 ) { return false ; } else { st . Pop ( ) ; } } if ( st . Count != 0 ) { return false ; } return true ; } static bool isBalancedSeq ( String s1 , String s2 ) { if ( isBalanced ( s1 + s2 ) ) { return true ; } return isBalanced ( s2 + s1 ) ; } public static void Main ( String [ ] args ) { String s1 = " ) ( ) ( ( ) ) ) ) " ; String s2 = " ( ( ) ( ( ) ( " ; if ( isBalancedSeq ( s1 , s2 ) ) { Console . WriteLine ( " Balanced " ) ; } else { Console . WriteLine ( " Not ▁ Balanced " ) ; } } }


"Check if count of 1 s can be made greater in a Binary string by changing 0 s adjacent to 1 s | C # program for the above approach ; Function to check whether in a given binary string can we make number of 1 ' s ▁ greater ▁ than ▁ the ▁ number ▁ of ▁ 0' s by doing the given operation ; Stores the count of 0 's ; Stores the count of 1 's ; Traverse through the string S ; Check current character is 1 ; Update cnt1 ; Update cnt0 ; Traverse through the string S ; Check curretn character is 1 ; Check if left adjacent character is 0 ; Change the left adjacent character to _ ; Update the cnt0 ; Check if right adjacent character is 0 ; Change the right adjacent character to _ ; Update the cnt0 ; Check count of 1 ' s ▁ is ▁ greater ▁ ▁ than ▁ the ▁ count ▁ of ▁ 0' s ; Driver Code"
Solution in C#:

using System ; using System . Text ; class GFG { static void isOnesGreater ( string S , int N ) { StringBuilder st = new StringBuilder ( S ) ; int cnt0 = 0 ; int cnt1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) cnt1 ++ ; else cnt0 ++ ; } for ( int i = 0 ; i < N ; i ++ ) { if ( st [ i ] == '1' ) { if ( i > 0 && st [ i - 1 ] == '0' ) { st [ i - 1 ] = ' _ ' ; cnt0 -- ; } else if ( i < N && st [ i + 1 ] == '0' ) { st [ i + 1 ] = ' _ ' ; cnt0 -- ; } } } if ( cnt1 > cnt0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } public static void Main ( ) { string S = "01" ; int N = S . Length ; isOnesGreater ( S , N ) ; } }


"Check if count of Alphabets and count of Numbers are equal in the given String | C # program to check if the count of alphabets and numbers in a String are equal or not . ; Function to count the number of alphabets ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code"
Solution in C#:

 using System ; class GFG { static int countOfLetters ( String str ) { int letter = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) letter ++ ; } return letter ; } static int countOfNumbers ( String str ) { int number = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) number ++ ; } return number ; } static void check ( String str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) Console . Write ( " Yes
 " ) ; else Console . Write ( " No
 " ) ; } public static void Main ( String [ ] args ) { String str = " GeeKs01324" ; check ( str ) ; } }