"Count triangles required to form a House of Cards of height N | C # implementation of the above approach ; Function to find number of triangles ; Driver Code ; Function call"
Solution in C#:

using System ; class GFG { public static int noOfTriangles ( int n ) { return ( n * ( n + 2 ) * ( 2 * n + 1 ) / 8 ) ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( noOfTriangles ( n ) ) ; } }


"Count triplet of indices ( i , j , k ) such that XOR of elements between [ i , j ) equals [ j , k ] | C # program to count the Number of triplets in array having subarray XOR equal ; Function return the count of triplets having subarray XOR equal ; XOR value till i ; Count and ways array as defined above ; Using the formula stated ; Increase the frequency of x ; Add i + 1 to ways [ x ] for upcoming indices ; Driver code"
Solution in C#:

using System ; class GFG { static int CountOfTriplets ( int [ ] a , int n ) { int answer = 0 ; int x = 0 ; int [ ] count = new int [ 100005 ] ; int [ ] ways = new int [ 100005 ] ; for ( int i = 0 ; i < n ; i ++ ) { x ^= a [ i ] ; answer += count [ x ] * i - ways [ x ] ; count [ x ] ++ ; ways [ x ] += ( i + 1 ) ; } return answer ; } public static void Main ( String [ ] args ) { int [ ] Arr = { 3 , 6 , 12 , 8 , 6 , 2 , 1 , 5 } ; int N = Arr . Length ; Console . Write ( CountOfTriplets ( Arr , N ) ) ; } }


"Count triplet pairs ( A , B , C ) of points in 2 | C # implementation of the approach ; Function to return the count of possible triplets ; Insert all the points in a set ; If the mid point exists in the set ; Return the count of valid triplets ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int countTriplets ( int n , List < pair > points ) { HashSet < pair > pts = new HashSet < pair > ( ) ; int ct = 0 ; for ( int i = 0 ; i < n ; i ++ ) pts . Add ( points [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int x = points [ i ] . first + points [ j ] . first ; int y = points [ i ] . second + points [ j ] . second ; if ( x % 2 == 0 && y % 2 == 0 ) if ( ! pts . Contains ( new pair ( x / 2 , y / 2 ) ) ) ct ++ ; } return ct ; } public static void Main ( String [ ] args ) { List < pair > points = new List < pair > ( ) ; points . Add ( new pair ( 1 , 1 ) ) ; points . Add ( new pair ( 2 , 2 ) ) ; points . Add ( new pair ( 3 , 3 ) ) ; int n = points . Count ; Console . WriteLine ( countTriplets ( n , points ) ) ; } }


"Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | C # implementation of the approach ; Function returns the count of the triplets ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver code"
Solution in C#:

using System ; class GFG { static long count_triples ( int n , int k ) { int i = 0 , j = 0 , l = 0 ; int count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } public static void Main ( ) { int n = 3 ; int k = 2 ; long ans = count_triples ( n , k ) ; Console . WriteLine ( ans ) ; } }


"Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | Set 2 | C # program for the above approach ; Function to count the number of triplets from the range [ 1 , N - 1 ] having sum of all pairs divisible by K ; If K is even ; Otherwise ; Driver Code"
Solution in C#:

using System ; class GFG { static int countTriplets ( int N , int K ) { if ( K % 2 == 0 ) { int x = N / K ; int y = ( N + ( K / 2 ) ) / K ; return x * x * x + y * y * y ; } else { int x = N / K ; return x * x * x ; } } static void Main ( ) { int N = 2 , K = 2 ; Console . Write ( countTriplets ( N , K ) ) ; } }


"Count triplets from a sorted array having difference between adjacent elements equal to D | C # program for the above approach ; Function to count the number of triplets having difference between adjacent elements equal to D ; Stores the frequency of array elements ; Stores the count of resultant triplets ; Traverse the array ; Check if arr [ i ] - D and arr [ i ] - 2 * D exists in the Hashmap or not ; Update the value of ans ; Increase the frequency of the current element ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countTriplets ( int D , int [ ] arr ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( freq . ContainsKey ( arr [ i ] - D ) && freq . ContainsKey ( arr [ i ] - 2 * D ) ) { ans += freq [ arr [ i ] - D ] * freq [ arr [ i ] - 2 * D ] ; } if ( ! freq . ContainsKey ( arr [ i ] ) ) freq [ arr [ i ] ] = 0 ; freq [ arr [ i ] ] ++ ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 5 , 7 , 8 , 10 } ; int D = 1 ; Console . WriteLine ( countTriplets ( D , arr ) ) ; } }


"Count triplets from an array which can form quadratic equations with real roots | C # program for the above approach ; Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values of a and c are equal to b ; Increment a ; Decrement c ; Condition for having real roots for a quadratic equation ; If b lies in between a and c ; Update count ; Update count ; Increment a ; Decrement c ; For each pair two values are possible of a and c ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int getCount ( int [ ] arr , int N ) { Array . Sort ( arr ) ; int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { int a = 0 , c = N - 1 ; int d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } static public void Main ( ) { int [ ] arr = { 3 , 6 , 10 , 13 , 21 } ; int N = arr . Length ; Console . Write ( getCount ( arr , N ) ) ; } }


"Count triplets from an array which can form quadratic equations with real roots | C # program for the above approach ; Function to find count of triplets ( a , b , c ) such that the equations ax ^ 2 + bx + c = 0 has real roots ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Generate all possible triplets ( a , b , c ) ; If the coefficient of X ^ 2 and X are equal ; If coefficient of X ^ 2 or x are equal to the constant ; Condition for having real roots ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int getCount ( int [ ] arr , int N ) { int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { for ( int a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( int c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; int d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . WriteLine ( getCount ( arr , N ) ) ; } }


"Count triplets in a sorted doubly linked list whose product is equal to a given value x | C # implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Structure of node of doubly linked list ; Function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; Unordered_map ' um ' implemented as hash table ; Insert the < node data , node pointer > tuple in ' um ' ; Generate all possible pairs ; p_product = product of elements in the current pair ; If ' x / p _ product ' is present in ' um ' and either of the two nodes are not equal to the ' um [ x / p _ product ] ' node ; Increment count ; Required count of triplets division by 3 as each triplet is counted 3 times ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data ; Driver code ; Start with an empty doubly linked list ; Insert values in sorted order"
Solution in C#:

using System ; using System . Collections . Generic ; class Node { public int data ; public Node next , prev ; } class GFG { static int countTriplets ( Node head , int x ) { Node ptr , ptr1 , ptr2 ; int count = 0 ; Dictionary < int , Node > um = new Dictionary < int , Node > ( ) ; for ( ptr = head ; ptr != null ; ptr = ptr . next ) { um . Add ( ptr . data , ptr ) ; } for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) { for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) { int p_product = ( ptr1 . data * ptr2 . data ) ; if ( um . ContainsKey ( x / p_product ) && um [ x / p_product ] != ptr1 && um [ x / p_product ] != ptr2 ) { count ++ ; } } } return ( count / 3 ) ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( head == null ) { head = temp ; } else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } static public void Main ( ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; Console . WriteLine ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }


"Count triplets in a sorted doubly linked list whose product is equal to a given value x | C # implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count pairs whose product equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if product is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with product ( x / current . data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver code ; start with an empty doubly linked list ; insert values in sorted order"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node next , prev ; } ; static int countPairs ( Node first , Node second , int value ) { int count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data * second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data * second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } static int countTriplets ( Node head , int x ) { if ( head == null ) return 0 ; Node current , first , last ; int count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x / current . data ) ; } return count ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; Console . WriteLine ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }


"Count triplets in a sorted doubly linked list whose product is equal to a given value x | C # implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; Represents node of a doubly linked list ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; generate all possible triplets ; if elements in the current triplet sum up to ' x ' ; increment count ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; Driver code ; start with an empty doubly linked list ; insert values in sorted order"
Solution in C#:

using System ; public class Node { public int data ; public Node prev , next ; public Node ( int val ) { data = val ; prev = null ; next = null ; } } class GFG { static int countTriplets ( Node head , int x ) { Node ptr1 , ptr2 , ptr3 ; int count = 0 ; for ( ptr1 = head ; ptr1 != null ; ptr1 = ptr1 . next ) for ( ptr2 = ptr1 . next ; ptr2 != null ; ptr2 = ptr2 . next ) for ( ptr3 = ptr2 . next ; ptr3 != null ; ptr3 = ptr3 . next ) if ( ( ptr1 . data * ptr2 . data * ptr3 . data ) == x ) count ++ ; return count ; } static Node insert ( Node head , int val ) { Node temp = new Node ( val ) ; if ( head == null ) head = temp ; else { temp . next = head ; head . prev = temp ; head = temp ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; Console . WriteLine ( " count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }


"Count triplets in a sorted doubly linked list whose sum is equal to a given value x | C # implementation to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count pairs whose sum equal to given ' value ' ; The loop terminates when either of two pointers become null , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; pair found ; increment count ; move first in forward direction ; move second in backward direction ; if sum is greater than ' value ' move second in backward direction ; else move first in forward direction ; required count of pairs ; function to count triplets in a sorted doubly linked list whose sum is equal to a given value ' x ' ; if list is empty ; get pointer to the last node of the doubly linked list ; traversing the doubly linked list ; for each current node ; count pairs with sum ( x - current . data ) in the range first to last and add it to the ' count ' of triplets ; required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver program to test above ; start with an empty doubly linked list ; insert values in sorted order"
Solution in C#:

using System ; class GFG { class Node { public int data ; public Node next , prev ; } ; static int countPairs ( Node first , Node second , int value ) { int count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data + second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data + second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } static int countTriplets ( Node head , int x ) { if ( head == null ) return 0 ; Node current , first , last ; int count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x - current . data ) ; } return count ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } public static void Main ( String [ ] args ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 17 ; Console . Write ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }


"Count triplets such that product of two numbers added with third number is N | C # program for the above approach ; Function to find the divisors of the number ( N - i ) ; Stores the resultant count of divisors of ( N - i ) ; Iterate over range [ 1 , sqrt ( N ) ] ; Return the total divisors ; Function to find the number of triplets such that A * B - C = N ; Loop to fix the value of C ; Adding the number of divisors in count ; Return count of triplets ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countDivisors ( int n ) { int divisors = 0 ; int i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) { divisors ++ ; } } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) { divisors ++ ; } } return divisors ; } static int possibleTriplets ( int N ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { count += countDivisors ( N - i ) ; } return count ; } public static void Main ( ) { int N = 10 ; Console . Write ( possibleTriplets ( N ) ) ; } }


"Count triplets with sum smaller than a given value | A Simple C # program to count triplets with sum smaller than a given value ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver method to test the above function"
Solution in C#:

using System ; class Test { static int [ ] arr = new int [ ] { 5 , 1 , 3 , 4 , 7 } ; static int countTriplets ( int n , int sum ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) ans ++ ; } } return ans ; } public static void Main ( ) { int sum = 12 ; Console . Write ( countTriplets ( arr . Length , sum ) ) ; } }


"Count triplets with sum smaller than a given value | A Simple C # program to count triplets with sum smaller than a given value ; Sort input array ; Initialize result ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there can be total k - j third elements . ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ ] arr = new int [ ] { 5 , 1 , 3 , 4 , 7 } ; static int countTriplets ( int n , int sum ) { Array . Sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { ans += ( k - j ) ; j ++ ; } } } return ans ; } public static void Main ( ) { int sum = 12 ; Console . Write ( countTriplets ( arr . Length , sum ) ) ; } }


"Count twin prime pairs in an Array | C # program to count Twin Prime pairs in array ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i and i + 2 then it is not prime ; A utility function that check if n1 and n2 are Twin Primes or not ; Function to find Twin Prime pairs from the given array ; Iterate through all pairs ; Increment count if twin prime pair ; Driver 's code ; Function call to find Twin Primes pair"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static bool twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . Abs ( n1 - n2 ) == 2 ) ; } static int countTwinPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( twinPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 11 } ; int n = arr . Length ; Console . Write ( countTwinPairs ( arr , n ) ) ; } }


"Count unique numbers that can be generated from N by adding one and removing trailing zeros | C # implementation of the approach ; Function to count the unique numbers ; If the number has already been visited ; Insert the number to the set ; First step ; Second step remove trailing zeros ; Recur again for the new number ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void count_unique ( HashSet < int > s , int n ) { if ( s . Contains ( n ) ) return ; s . Add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void Main ( String [ ] args ) { int n = 10 ; HashSet < int > s = new HashSet < int > ( ) ; count_unique ( s , n ) ; Console . WriteLine ( s . Count ) ; } }


"Count unique paths is a matrix whose product of elements contains odd number of divisors | C # program for the above approach ; Function to calculate and store all the paths product in vector ; Base Case ; If reaches the bottom right corner and product is a perfect square ; Find square root ; If square root is an integer ; Move towards down in the matrix ; Move towards right in the matrix ; Driver Code ; Given matrix mat [ ] [ ] ; Function call"
Solution in C#:

using System ; class GFG { static int countPaths = 0 ; static void CountUniquePaths ( int [ , ] a , int i , int j , int m , int n , int ans ) { if ( i >= m j >= n ) return ; if ( i == m - 1 && j == n - 1 ) { double sr = Math . Sqrt ( ans * a [ i , j ] ) ; if ( ( sr - Math . Floor ( sr ) ) == 0 ) countPaths ++ ; } CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i , j ] ) ; CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i , j ] ) ; } public static void Main ( String [ ] args ) { int M = 3 , N = 2 ; int [ , ] mat = { { 1 , 1 } , { 3 , 1 } , { 3 , 1 } } ; CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ; Console . Write ( countPaths ) ; } }


"Count unique paths with given sum in an N | C # program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int findTotalPath ( int X , int n , int [ ] dp ) { if ( X == 0 ) { return 1 ; } int ans = 0 ; if ( dp [ X ] != - 1 ) { return dp [ X ] ; } for ( int i = 1 ; i <= Math . Min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } dp [ X ] = ans ; return ans ; } public static void Main ( ) { int n = 3 , X = 2 ; int [ ] dp = new int [ X + 1 ] ; Array . Fill ( dp , - 1 ) ; Console . WriteLine ( findTotalPath ( X , n , dp ) ) ; } }


"Count unique stairs that can be reached by moving given number of steps forward or backward | C # program for the above approach ; Function to count the number of unique stairs visited ; Checks whether the current stair is visited or not ; Store the possible moves from the current position ; Initialize a queue ; / Push the starting position ; Mark the starting position S as visited ; Iterate until queue is not empty ; Store the current stair number ; Pop it from the queue ; Check for all possible moves from the current stair ; Store the new stair number ; If it is valid and unvisited ; Push it into queue ; Mark the stair as visited ; Store the result ; Count the all visited stairs ; Print the result ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void countStairs ( int n , int x , int a , int b ) { int [ ] vis = new int [ n + 1 ] ; Array . Clear ( vis , 0 , vis . Length ) ; int [ ] moves = { + a , - a , + b , - b } ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( x ) ; vis [ x ] = 1 ; while ( q . Count > 0 ) { int currentStair = q . Peek ( ) ; q . Dequeue ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && vis [ newStair ] == 0 ) { q . Enqueue ( newStair ) ; vis [ newStair ] = 1 ; } } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; Console . WriteLine ( cnt ) ; } public static void Main ( ) { int N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ; } }


"Count unique substrings of a string S present in a wraparound string | C # program for the above approach ; Function to find the count of non - empty substrings of p present in s ; Stores the required answer ; Stores the length of substring present in p ; Stores the current length of substring that is present in string s starting from each character of p ; Iterate over the characters of the string ; Check if the current character can be added with previous substring to form the required substring ; Increment current length ; To avoid repetition ; Update arr [ cur ] ; Print the answer ; Driver code ; Function call to find the count of non - empty substrings of p present in s"
Solution in C#:

using System ; class GFG { static void findSubstringInWraproundString ( string p ) { int ans = 0 ; int curLen = 0 ; int [ ] arr = new int [ 26 ] ; for ( int i = 0 ; i < ( int ) p . Length ; i ++ ) { int curr = p [ i ] - ' a ' ; if ( i > 0 && ( p [ i - 1 ] != ( ( curr + 26 - 1 ) % 26 + ' a ' ) ) ) { curLen = 0 ; } curLen ++ ; if ( curLen > arr [ curr ] ) { ans += ( curLen - arr [ curr ] ) ; arr [ curr ] = curLen ; } } Console . Write ( ans ) ; } static void Main ( ) { string p = " zab " ; findSubstringInWraproundString ( p ) ; } }


"Count unordered pairs ( i , j ) such that product of a [ i ] and a [ j ] is power of two | C # program to Count unordered pairs ( i , j ) in array such that product of a [ i ] and a [ j ] can be expressed as power of two ; Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Function to Count unordered pairs ; is a number can be expressed as power of two ; count total number of unordered pairs ; Driver code"
Solution in C#:

using System ; public class GFG { static bool isPowerOfTwo ( int x ) { return ( x > 0 && ( ! ( ( x & ( x - 1 ) ) > 0 ) ) ) ; } static void Count_pairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; Console . WriteLine ( ans ) ; } static public void Main ( ) { int [ ] a = { 2 , 5 , 8 , 16 , 128 } ; int n = a . Length ; Count_pairs ( a , n ) ; } }


"Count unset bits in a range | C # implementation to count unset bits in the given range ; Function to get no of set bits in the binary representation of ' n ' ; function to count unset bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; returns number of unset bits in the range ' l ' to ' r ' in ' n ' ; Driver code"
Solution in C#:

using System ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countUnsetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } public static void Main ( ) { int n = 80 ; int l = 1 , r = 4 ; Console . Write ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }


"Count unset bits of a number | An optimized C # program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; To store the count of set bits ; Driver code"
Solution in C#:

using System ; class GFG { static int countUnsetBits ( int n ) { int x = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return BitCount ( x ^ n ) ; } static int BitCount ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } public static void Main ( String [ ] args ) { int n = 17 ; Console . WriteLine ( countUnsetBits ( n ) ) ; } }


"Count valid pairs in the array satisfying given conditions | C # implementation of the approach ; Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer"
Solution in C#:

using System ; class GFG { static int ValidPairs ( int [ ] arr , int n ) { int [ ] count = new int [ 121 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] += 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) continue ; if ( Math . Abs ( arr [ i ] - arr [ j ] ) % 2 == 1 ) continue ; ans += count [ arr [ i ] ] * count [ arr [ j ] ] ; if ( arr [ i ] == arr [ j ] ) ans -= count [ arr [ i ] ] ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 16 , 17 , 18 } ; int n = arr . Length ; Console . WriteLine ( ValidPairs ( arr , n ) ) ; } }


"Count ways to build street under given constraints | C # program of above approach ; Program to count ways ; Iterate from 2 to n ; Driver code ; Count Ways"
Solution in C#:

using System ; class GFG { static int countways ( int n ) { int [ ] A = new int [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 3 ; A [ 2 ] = 7 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = 2 * A [ i - 1 ] + A [ i - 2 ] ; } return A [ n ] ; } public static void Main ( ) { int n = 5 ; Console . Write ( countways ( n ) ) ; } }


"Count ways to build street under given constraints | C # program to count ways to build street under given constraints ; function to count ways of building a street of n rows ; base case ; ways of building houses in both the spots of ith row ; ways of building an office in one of the two spots of ith row ; total ways for n rows ; driver program for checking above function"
Solution in C#:

using System ; public class GFG { static long countWays ( int n ) { long [ , ] dp = new long [ 2 , n + 1 ] ; dp [ 0 , 1 ] = 1 ; dp [ 1 , 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 , i ] = dp [ 0 , i - 1 ] + dp [ 1 , i - 1 ] ; dp [ 1 , i ] = dp [ 0 , i - 1 ] * 2 + dp [ 1 , i - 1 ] ; } return dp [ 0 , n ] + dp [ 1 , n ] ; } public static void Main ( ) { int n = 5 ; Console . Write ( " Total ▁ no ▁ of ▁ ways ▁ with ▁ n ▁ = ▁ " + n + " ▁ are : ▁ " + countWays ( n ) ) ; } }


"Count ways to distribute exactly one coin to each worker | C # program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function Call"
Solution in C#:

using System ; using System . Collections ; class GFG { static int MOD = 1000000007 ; static int solve ( ArrayList values , ArrayList salary ) { int ret = 1 ; int amt = 0 ; values . Sort ( ) ; salary . Sort ( ) ; while ( salary . Count > 0 ) { while ( values . Count > 0 && ( int ) values [ values . Count - 1 ] >= ( int ) salary [ salary . Count - 1 ] ) { amt ++ ; values . RemoveAt ( values . Count - 1 ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . RemoveAt ( salary . Count - 1 ) ; } return ret ; } public static void Main ( string [ ] args ) { ArrayList values = new ArrayList ( ) ; values . Add ( 1 ) ; values . Add ( 2 ) ; ArrayList salary = new ArrayList ( ) ; salary . Add ( 2 ) ; Console . Write ( solve ( values , salary ) ) ; } }


"Count ways to distribute m items among n people | C # code for calculating number of ways to distribute m mangoes amongst n people where all mangoes and people are identical ; function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; Driver Code ; m represents number of mangoes n represents number of people"
Solution in C#:

using System ; class GFG { public static int binomial_coefficient ( int n , int m ) { int res = 1 ; if ( m > n - m ) m = n - m ; for ( int i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static int calculate_ways ( int m , int n ) { if ( m < n ) { return 0 ; } int ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } public static void Main ( ) { int m = 7 , n = 5 ; int result = calculate_ways ( m , n ) ; Console . WriteLine ( result . ToString ( ) ) ; } }


"Count ways to divide C in two parts and add to A and B to make A strictly greater than B | C # implementation of the above approach ; Function to count the number of ways to divide C into two parts and add to A and B such that A is strictly greater than B ; Minimum value added to A to satisfy the given relation ; Number of different values of A , i . e . , number of ways to divide C ; Driver Code"
Solution in C#:

using System ; class GFG { static int countWays ( int A , int B , int C ) { int minAddA = Math . Max ( 0 , ( C + B - A + 2 ) / 2 ) ; int count_ways = Math . Max ( C - minAddA + 1 , 0 ) ; return count_ways ; } public static void Main ( String [ ] args ) { int A = 3 , B = 5 , C = 5 ; Console . Write ( countWays ( A , B , C ) ) ; } }


"Count ways to divide circle using N non | C # code to count ways to divide circle using N non - intersecting chords . ; n = no of points required ; dp array containing the sum ; returning the required number ; Driver code"
Solution in C#:

using System ; class GFG { static int chordCnt ( int A ) { int n = 2 * A ; int [ ] dpArray = new int [ n + 1 ] ; dpArray [ 0 ] = 1 ; dpArray [ 2 ] = 1 ; for ( int i = 4 ; i <= n ; i += 2 ) { for ( int j = 0 ; j < i - 1 ; j += 2 ) { dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ; } } return dpArray [ n ] ; } public static void Main ( ) { int N ; N = 2 ; Console . WriteLine ( chordCnt ( N ) ) ; N = 1 ; Console . WriteLine ( chordCnt ( N ) ) ; N = 4 ; Console . WriteLine ( chordCnt ( N ) ) ; } }


"Count ways to divide circle using N non | Java implementation of the above approach ; Function to calculate x ^ y % mod efficiently ; Initialize the answer ; If power is odd ; Update the answer ; Square the base and half the exponent ; Return the value ; Function to calculate ncr % mod efficiently ; Initialize the answer ; Calculate ncr in O ( r ) ; Multiply with the numerator factor ; Calculate the inverse of factor of denominator ; Multiply with inverse value ; Return answer value ; Function to return the number of non intersecting chords ; define mod value ; Value of C ( 2 n , n ) ; Modulo inverse of ( n + 1 ) ; Multiply with modulo inverse ; Return the answer ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int power ( long x , int y , int mod ) { long res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; x = ( x * x ) % mod ; y = ( y >> 1 ) ; } return ( int ) ( res % mod ) ; } static int ncr ( int n , int r , int mod ) { long res = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { res = ( res * ( n - i + 1 ) ) % mod ; int inv = power ( i , mod - 2 , mod ) ; res = ( res * inv ) % mod ; } return ( int ) ( res % mod ) ; } static int NoOfChords ( int A ) { int mod = ( int ) ( 1e9 + 7 ) ; long ans = ncr ( 2 * A , A , mod ) ; int inv = power ( A + 1 , mod - 2 , mod ) ; ans = ( ans * inv ) % mod ; return ( int ) ( ans % mod ) ; } public static void Main ( ) { int N = 2 ; Console . WriteLine ( NoOfChords ( N ) ) ; } }


"Count ways to express even number â €˜ nâ €™ as sum of even integers | C # program to count ways to write number as sum of even integers ; Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Return number of ways to write ' n ' as sum of even integers ; Driver code"
Solution in C#:

using System ; class GFG { static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( 1 * res * x ) % p ; x = ( 1 * x * x ) % p ; } return res ; } static int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } public static void Main ( ) { int n = 6 ; Console . WriteLine ( countEvenWays ( n ) ) ; n = 8 ; Console . WriteLine ( countEvenWays ( n ) ) ; } }


"Count ways to form minimum product triplets | C # program to count number of ways we can form triplets with minimum product . ; function to calculate number of triples ; Sort the array ; Count occurrences of third element ; If all three elements are same ( minimum element appears at least 3 times ) . Answer is nC3 . ; If minimum element appears once . Answer is nC2 . ; Minimum two elements are distinct . Answer is nC1 . ; driver code"
Solution in C#:

using System ; class GFG { static long noOfTriples ( long [ ] arr , int n ) { Array . Sort ( arr ) ; long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == arr [ 2 ] ) count ++ ; if ( arr [ 0 ] == arr [ 2 ] ) return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 ; else if ( arr [ 1 ] == arr [ 2 ] ) return ( count - 1 ) * ( count ) / 2 ; return count ; } public static void Main ( ) { long [ ] arr = { 1 , 3 , 3 , 4 } ; int n = arr . Length ; Console . Write ( noOfTriples ( arr , n ) ) ; } }


"Count ways to generate an array having distinct elements at M consecutive indices | C # program for the above approach ; Modular function to calculate factorial ; Stores factorial of N ; Iterate over the range [ 1 , N ] ; Update result ; Function to count ways to replace array elements having 0 s with non - zero elements such that any M consecutive elements are distinct ; Store m consecutive distinct elements such that arr [ i ] is equal to B [ i % M ] ; Stores frequency of array elements ; Traverse the array arr [ ] ; If arr [ i ] is non - zero ; If B [ i % M ] is equal to 0 ; Update B [ i % M ] ; Update frequency of arr [ i ] ; If a duplicate element found in M consecutive elements ; Handling the case of inequality ; Stores count of 0 s in B [ ] ; Traverse the array , B [ ] ; If B [ i ] is 0 ; Update cnt ; Calculate factorial ; Driver Code ; Given M ; Given array ; Size of the array ; Function Call"
Solution in C#:

using System ; class GFG { static int Fact ( int N ) { int result = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { result = ( result * i ) ; } return result ; } static void numberOfWays ( int M , int [ ] arr , int N ) { int [ ] B = new int [ M ] ; int [ ] counter = new int [ M + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) { if ( B [ i % M ] == 0 ) { B [ i % M ] = arr [ i ] ; counter [ arr [ i ] ] ++ ; if ( counter [ arr [ i ] ] > 1 ) { Console . WriteLine ( 0 ) ; return ; } } else if ( B [ i % M ] != arr [ i ] ) { Console . WriteLine ( 0 ) ; return ; } } } int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( B [ i ] == 0 ) { cnt ++ ; } } Console . WriteLine ( Fact ( cnt ) ) ; } static public void Main ( ) { int M = 4 ; int [ ] arr = new int [ ] { 1 , 0 , 3 , 0 , 0 } ; int N = arr . Length ; numberOfWays ( M , arr , N ) ; } }


"Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively | C # program for the above approach ; Function to return the count of possible pairs of A and B whose Bitwise XOR is X and Y respectively ; Stores the count of pairs ; Iterate till any bit are set ; Extract i - th bit of X and Y ; Divide X and Y by 2 ; If Xi = 1 and Yi = 2 , multiply counter by 2 ; Increase required count ; If Xi = 1 and Yi = 1 ; No answer exists ; Return the readonly count ; Driver Code ; Given X and Y ; Function Call"
Solution in C#:

using System ; class GFG { static int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x > 0 y > 0 ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 && bit2 == 0 ) { counter *= 2 ; continue ; } if ( ( bit1 & bit2 ) > 0 ) { counter = 0 ; break ; } } return counter ; } public static void Main ( String [ ] args ) { int X = 2 , Y = 5 ; Console . Write ( countOfPairs ( X , Y ) ) ; } }


"Count ways to increase LCS length of two strings by one | C # program to get number of ways to increase LCS by 1 ; Method returns total ways to increase LCS length by 1 ; Fill positions of each character in vector ; Initializing 2D array by 0 values ; Filling LCS array for prefix substrings ; Filling LCS array for suffix substrings ; Looping for all possible insertion positions in first string ; Trying all possible lower case characters ; Now for each character , loop over same character positions in second string ; If both , left and right substrings make total LCS then increase result by 1 ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int M = 26 ; static int waysToIncreaseLCSBy1 ( String str1 , String str2 ) { int m = str1 . Length , n = str2 . Length ; List < int > [ ] position = new List < int > [ M ] ; for ( int i = 0 ; i < M ; i ++ ) position [ i ] = new List < int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) position [ str2 [ i - 1 ] - ' a ' ] . Add ( i ) ; int [ , ] lcsl = new int [ m + 2 , n + 2 ] ; int [ , ] lcsr = new int [ m + 2 , n + 2 ] ; for ( int i = 0 ; i <= m + 1 ; i ++ ) for ( int j = 0 ; j <= n + 1 ; j ++ ) lcsl [ i , j ] = lcsr [ i , j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsl [ i , j ] = 1 + lcsl [ i - 1 , j - 1 ] ; else lcsl [ i , j ] = Math . Max ( lcsl [ i - 1 , j ] , lcsl [ i , j - 1 ] ) ; } } for ( int i = m ; i >= 1 ; i -- ) { for ( int j = n ; j >= 1 ; j -- ) { if ( str1 [ i - 1 ] == str2 [ j - 1 ] ) lcsr [ i , j ] = 1 + lcsr [ i + 1 , j + 1 ] ; else lcsr [ i , j ] = Math . Max ( lcsr [ i + 1 , j ] , lcsr [ i , j + 1 ] ) ; } } int ways = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int d = 0 ; d < 26 ; d ++ ) { for ( int j = 0 ; j < position [ d ] . Count ; j ++ ) { int p = position [ d ] [ j ] ; if ( lcsl [ i , p - 1 ] + lcsr [ i + 1 , p + 1 ] == lcsl [ m , n ] ) ways ++ ; } } } return ways ; } public static void Main ( String [ ] args ) { String str1 = " abcabc " ; String str2 = " abcd " ; Console . WriteLine ( waysToIncreaseLCSBy1 ( str1 , str2 ) ) ; } }


"Count ways to make the number formed by K concatenations of a numeric string divisible by 5 | C # program for the above approach ; Function to find the value of a ^ b modulo 1000000007 ; Stores the resultant value a ^ b ; Find the value of a ^ b ; Function to count the number of ways such that the formed number is divisible by 5 by removing digits ; Stores the count of ways ; Find the count for string S ; If the digit is 5 or 0 ; Find the count of string for K concatenation of string S ; Find the total count ; Driver Code"
Solution in C#:

using System ; public class GFG { static long MOD = 1000000007 ; public static long exp_mod ( long a , long b ) { long ret = 1 ; for ( ; b > 0 ; b >>= 1 , a = a * a % MOD ) { if ( ( b & 1 ) > 0 ) ret = ret * a % MOD ; } return ret ; } public static long countOfWays ( String s , int k ) { int N = s . Length ; long ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s [ i ] == '5' s [ 0 ] == '0' ) { ans = ( ans + exp_mod ( 2 , i ) ) % MOD ; } } long q = exp_mod ( 2 , N ) ; long qk = exp_mod ( q , k ) ; long inv = exp_mod ( q - 1 , MOD - 2 ) ; ans = ans * ( qk - 1 ) % MOD ; ans = ans * inv % MOD ; return ans ; } public static void Main ( String [ ] args ) { String S = "1256" ; int K = 1 ; Console . WriteLine ( countOfWays ( S , K ) ) ; } }


"Count ways to obtain triplets with positive product consisting of at most one negative element | C # Program to implement the above approach ; Function to calculate possible number of triplets ; counting frequency of positive numbers in array ; If current array element is positive ; Increment frequency ; Select a triplet from freq elements such that i < j < k . ; Driver Code"
Solution in C#:

using System ; public class GFG { static int possibleTriplets ( int [ ] arr , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { freq ++ ; } } return ( int ) ( ( freq * 1L * ( freq - 1 ) * ( freq - 2 ) ) / 6 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 5 , - 9 , - 3 , 6 } ; int N = arr . Length ; Console . Write ( possibleTriplets ( arr , N ) ) ; } }


"Count ways to partition a Binary String such that each substring contains exactly two 0 s | C # program for the above approach ; Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int totalWays ( int n , string str ) { ArrayList IdxOf0s = new ArrayList ( ) ; int cntWays = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { IdxOf0s . Add ( i ) ; } } int M = IdxOf0s . Count ; if ( ( M == 0 ) || ( ( M % 2 ) != 0 ) ) { return 0 ; } for ( int i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( Convert . ToInt32 ( IdxOf0s [ i ] ) - Convert . ToInt32 ( IdxOf0s [ i - 1 ] ) ) ; } return cntWays ; } static public void Main ( ) { string str = "00100" ; int n = str . Length ; Console . Write ( totalWays ( n , str ) ) ; } }


"Count ways to partition a string such that both parts have equal distinct characters | C # implementation to count the number of ways to partition the String such that each partition have same number of distinct characters in the String ; Function to count the distinct characters in the String ; Frequency of each character ; Loop to count the frequency of each character of the String ; If frequency is greater than 0 then the character occured ; Function to count the number of ways to partition the String such that each partition have same number of distinct character ; Loop to choose the partition index for the String ; Divide in two parts ; Check whether number of distinct characters are equal ; Driver Code"
Solution in C#:

using System ; class GFG { static int distinctChars ( string s ) { int [ ] freq = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) freq [ s [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } static int waysToSplit ( string s ) { int n = s . Length ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { string left = s . Substring ( 0 , i ) ; string right = s . Substring ( i , n - i ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { string s = " ababa " ; Console . WriteLine ( waysToSplit ( s ) ) ; } }


"Count ways to partition a string such that both parts have equal distinct characters | C # implementation to count the number of ways to partition the string such that each partition have same number of distinct characters in the string ; Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Prefix and suffix array for distinct character from start and end ; To check whether a character has appeared till ith index ; Calculating prefix array ; Character appears for the first time in string ; Character is visited ; Resetting seen for suffix calculation ; Calculating the suffix array ; Character appears for the first time ; This character has now appeared ; Loop to calculate the number partition points in the string ; Check whether number of distinct characters are equal ; Driver Code"
Solution in C#:

using System ; public class GFG { static int waysToSplit ( string s ) { int n = s . Length ; int answer = 0 ; int [ ] prefix = new int [ n ] ; int [ ] suffix = new int [ n ] ; int [ ] seen = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int prev = ( i - 1 >= 0 ? prefix [ i - 1 ] : 0 ) ; if ( seen [ s [ i ] - ' a ' ] == 0 ) { prefix [ i ] += ( prev + 1 ) ; } else prefix [ i ] = prev ; seen [ s [ i ] - ' a ' ] = 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) seen [ i ] = 0 ; suffix [ n - 1 ] = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int prev = suffix [ i ] ; if ( seen [ s [ i ] - ' a ' ] == 0 ) { suffix [ i - 1 ] += ( prev + 1 ) ; } else suffix [ i - 1 ] = prev ; seen [ s [ i ] - ' a ' ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( prefix [ i ] == suffix [ i ] ) answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { string s = " ababa " ; Console . WriteLine ( waysToSplit ( s ) ) ; } }


"Count ways to place ' + ' and ' | C # program for the above approach ; Function to count number of ways ' + ' and ' - ' operators can be placed in front of array elements to make the sum of array elements equal to K ; Stores sum of the array ; Stores count of 0 s in A [ ] ; Traverse the array ; Update sum ; Update count of 0 s ; Conditions where no arrangements are possible which adds up to K ; Required sum ; Dp array ; Base cases ; Fill the dp array ; Return answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; public class GFG { static int solve ( int [ ] A , int N , int K ) { int sum = 0 ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( A [ i ] == 0 ) c ++ ; } if ( ( K > sum ) || ( ( ( sum + K ) % 2 ) != 0 ) ) return 0 ; sum = ( sum + K ) / 2 ; int [ , ] dp = new int [ N + 1 , sum + 1 ] ; for ( int i = 0 ; i <= sum ; i ++ ) dp [ 0 , i ] = 0 ; for ( int i = 0 ; i <= N ; i ++ ) dp [ i , 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( ( A [ i - 1 ] <= j ) && ( A [ i - 1 ] != 0 ) ) dp [ i , j ] = dp [ i - 1 , j ] + dp [ i - 1 , j - A [ i - 1 ] ] ; else dp [ i , j ] = dp [ i - 1 , j ] ; } } return dp [ N , sum ] + ( int ) Math . Pow ( 2 , c ) ; } static public void Main ( ) { int [ ] A = { 1 , 1 , 2 , 3 } ; int N = A . Length ; int K = 3 ; Console . Write ( solve ( A , N , K ) ) ; } }


"Count ways to place M objects in distinct partitions of N boxes | C # implementation of the above Approach ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize Result ; Update x if x >= MOD to avoid multiplication overflow ; If y is odd , multiply x with result ; y = y / 2 ; Change x to x ^ 2 ; Utility function to find the Total Number of Ways ; Number of Even Indexed Boxes ; Number of partitions of Even Indexed Boxes ; Number of ways to distribute objects ; Driver Code ; N = number of boxes M = number of distinct objects ; Function call to get Total Number of Ways"
Solution in C#:

using System ; public class GFG { public static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { p = MOD ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void totalWays ( int N , int M ) { int X = N / 2 ; int S = ( X * ( X + 1 ) ) % MOD ; Console . WriteLine ( power ( S , M , MOD ) ) ; } static public void Main ( ) { int N = 5 , M = 2 ; totalWays ( N , M ) ; } }


"Count ways to place all the characters of two given strings alternately | C # program to implement the above approach ; Function to get the factorial of N ; Function to get the total number of distinct ways ; Length of str1 ; Length of str2 ; If both strings have equal length ; If both strings do not have equal length ; Driver code"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { res = res * i ; } return res ; } static int distinctWays ( string str1 , string str2 ) { int n = str1 . Length ; int m = str2 . Length ; if ( n == m ) { return 2 * fact ( n ) * fact ( m ) ; } return fact ( n ) * fact ( m ) ; } public static void Main ( ) { string str1 = " aegh " ; string str2 = " rsw " ; Console . Write ( distinctWays ( str1 , str2 ) ) ; } }


"Count ways to reach end from start stone with at most K jumps at each step | C # program to find total no . of ways to reach nth step ; Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; Driver Code ; no of steps ; Atmost steps allowed ; starting range"
Solution in C#:

using System ; class GFG { static int TotalWays ( int n , int s , int k ) { int [ ] dp = new int [ n ] ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = Math . Max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } public static void Main ( string [ ] args ) { int n = 5 ; int k = 2 ; int s = 2 ; Console . Write ( " Total ▁ Ways ▁ = ▁ " + TotalWays ( n , s , k ) ) ; } }


"Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3 | C # implementation to find the number the number of ways to reach Nth stair by taking 1 or 2 steps at a time and 3 rd step exactly once ; Single line to find factorial ; Function to find the number of ways ; Base Case ; Count of 2 - steps ; Count of 1 - steps ; Initial length of sequence ; Expected count of 2 - steps ; Loop to find the ways for every possible sequence ; Driver code"
Solution in C#:

using System ; class GFG { static int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : n * factorial ( n - 1 ) ; } static int ways ( int n ) { if ( n < 3 ) { return 0 ; } int c2 = 0 ; int c1 = n - 3 ; int l = c1 + 1 ; int s = 0 ; int exp_c2 = c1 / 2 ; while ( exp_c2 >= c2 ) { int f1 = factorial ( l ) ; int f2 = factorial ( c1 ) ; int f3 = factorial ( c2 ) ; int f4 = ( f2 * f3 ) ; s += f1 / f4 ; c2 += 1 ; c1 -= 2 ; l -= 1 ; } return s ; } static void Main ( ) { int n = 7 ; int ans = ways ( n ) ; Console . WriteLine ( ans ) ; } }


"Count ways to reach the Nth station | C # program for the above approach ; Function to find the number of ways to reach Nth station ; Declares the DP [ ] array ; Initialize dp [ ] [ ] array ; Only 1 way to reach station 1 ; Find the remaining states from the 2 nd station ; If the train A is present at station i - 1 ; If the train B is present at station i - 2 ; If train C is present at station i - 3 ; The total number of ways to reach station i ; Return the total count of ways ; Driver Code"
Solution in C#:


"Count ways to remove objects such that exactly M equidistant objects remain | C # program for the above approach ; Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If len > n ; Update the number of ways ; Print the result ; Driver code"
Solution in C#:

using System ; class GFG { static void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { Console . Write ( n ) ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } Console . Write ( ans ) ; } static void Main ( ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; } }


"Count ways to remove pairs from a matrix such that remaining elements can be grouped in vertical or horizontal pairs | C # program for the above approach ; Function to count ways to remove pairs such that the remaining elements can be arranged in pairs vertically or horizontally ; Store the size of matrix ; If N is odd , then no such pair exists ; Store the number of required pairs ; Initialize an auxiliary matrix and fill it with 0 s ; Traverse the matrix v [ ] [ ] ; Check if i + j is odd or even ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 0 ] by 1 ; Increment the value dp [ v [ i ] [ j ] - 1 ] [ 1 ] by 1 ; Iterate in range [ 0 , k - 1 ] using i ; Iterate in range [ i + 1 , k - 1 ] using j ; Update the ans ; Print the answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void numberofpairs ( List < List < int > > v , int k ) { int n = v . Count ; if ( n % 2 == 1 ) { Console . Write ( 0 ) ; return ; } int ans = 0 ; int [ , ] dp = new int [ k , 2 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( i + j ) % 2 == 0 ) dp [ v [ i ] [ j ] - 1 , 0 ] ++ ; else dp [ v [ i ] [ j ] - 1 , 1 ] ++ ; } } for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i + 1 ; j < k ; j ++ ) { ans += dp [ i , 0 ] * dp [ j , 1 ] ; ans += dp [ i , 1 ] * dp [ j , 0 ] ; } } Console . Write ( ans ) ; } static void Main ( ) { List < List < int > > mat = new List < List < int > > ( ) ; mat . Add ( new List < int > ( new int [ ] { 1 , 2 } ) ) ; mat . Add ( new List < int > ( new int [ ] { 3 , 4 } ) ) ; int K = 4 ; numberofpairs ( mat , K ) ; } }


"Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | C # program for the above approach ; Function to find the factorial of the given number N ; Stores the factorial ; Iterate over the range [ 2 , N ] ; Return the resultant result ; Function to find the number of ways of choosing r objects from n distinct objects ; Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T ; Traverse the string s ; If the current character is 1 ; Update the value of the sum1 ; Otherwise ; Traverse the string t ; If the current character is 1 , then update the value of sum2 ; If the current character is 0 ; Otherwise , update the value of K ; Check if P is greater than K or if K - P is odd ; Print the count of ways ; Driver code"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res = res * i ; } return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static void countWays ( string s , string t ) { int n = s . Length ; int sum1 = 0 , sum2 = 0 , K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { sum1 ++ ; } else sum1 -- ; } int m = t . Length ; for ( int i = 0 ; i < m ; i ++ ) { if ( t [ i ] == '1' ) { sum2 ++ ; } else if ( t [ i ] == '0' ) { sum2 -- ; } else K ++ ; } int P = Math . Abs ( sum1 - sum2 ) ; if ( ( P > K ) || ( ( K - P ) % 2 ) != 0 ) { Console . WriteLine ( 0 ) ; return ; } Console . WriteLine ( nCr ( K , ( P + K ) / 2 ) ) ; } static public void Main ( ) { string S1 = "1010" ; string S2 = "10 ? ? " ; countWays ( S1 , S2 ) ; } }


"Count ways to represent N as XOR of distinct integers not exceeding N | C # program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void countXorPartition ( int N ) { double a = Math . Pow ( 2 , ( int ) ( N - Math . Log ( N + 1 ) / Math . Log ( 2 ) ) ) ; Console . Write ( a ) ; } public static void Main ( ) { int N = 5 ; countXorPartition ( N ) ; } }


"Count ways to represent N as sum of powers of 2 | C # program to implement the above approach ; Base Cases ; Check if 2 ^ k can be used as one of the numbers or not ; Otherwise ; Count number of ways to N using 2 ^ k - 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int numberOfWays ( int n , int k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; if ( n >= ( int ) Math . Pow ( 2 , k ) ) { int curr_val = ( int ) Math . Pow ( 2 , k ) ; return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) ; } else return numberOfWays ( n , k - 1 ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int k = ( int ) ( Math . Log ( n ) / Math . Log ( 2 ) ) ; Console . WriteLine ( numberOfWays ( n , k ) ) ; } }


"Count ways to select K array elements lying in a given range | C # program to implement the above approach ; Function to calculate factorial of all the numbers up to N ; Factorial of 0 is 1 ; Calculate factorial of all the numbers upto N ; Calculate factorial of i ; Function to find the count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of ways to select at least K elements whose values in range [ L , R ] ; Stores count of numbers having value lies in the range [ L , R ] ; Traverse the array ; Checks if the array elements lie in the given range ; Update cntNum ; Stores factorial of numbers upto N ; Calculate total ways to select at least K elements whose values lies in [ L , R ] ; Update cntWays ; Driver Code"
Solution in C#:

using System ; class GFG { static int [ ] calculateFactorial ( int N ) { int [ ] fact = new int [ ( N + 1 ) ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } return fact ; } static int cntWaysSelection ( int [ ] arr , int N , int K , int L , int R ) { int cntWays = 0 ; int cntNum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= L && arr [ i ] <= R ) { cntNum ++ ; } } int [ ] fact = calculateFactorial ( cntNum ) ; for ( int i = K ; i <= cntNum ; i ++ ) { cntWays += fact [ cntNum ] / ( fact [ i ] * fact [ cntNum - i ] ) ; } return cntWays ; } public static void Main ( ) { int [ ] arr = { 12 , 4 , 6 , 13 , 5 , 10 } ; int N = arr . Length ; int K = 3 ; int L = 4 ; int R = 10 ; Console . WriteLine ( cntWaysSelection ( arr , N , K , L , R ) ) ; } }


"Count ways to spell a number with repeated digits | C # program to count number of ways we can spell a number ; Function to calculate all possible spells of a number with repeated digits num -- > string which is favourite number ; final count of total possible spells ; iterate through complete number ; count contiguous frequency of particular digit num [ i ] ; Compute 2 ^ ( count - 1 ) and multiply with result ; Driver code"
Solution in C#:

using System ; class GFG { static long spellsCount ( String num ) { int n = num . Length ; long result = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && num [ i + 1 ] == num [ i ] ) { count ++ ; i ++ ; } result = result * ( long ) Math . Pow ( 2 , count - 1 ) ; } return result ; } public static void Main ( ) { String num = "11112" ; Console . Write ( spellsCount ( num ) ) ; } }


"Count ways to split N ! into two distinct co | C # program for the above approach ; Maximum value of N ; Stores at each indices if given number is prime or not ; Stores count_of_primes ; Function to generate primes using Sieve of Eratsothenes ; Assume all odds are primes ; If a prime is encountered ; MArk all its multiples as non - prime ; Count all primes upto MAXN ; Function to calculate ( x ^ y ) % p in O ( log y ) ; Utility function to count the number of ways N ! can be split into two co - prime factors ; Driver Code ; Calling sieve function ; Given N ; Function call"
Solution in C#:

using System ; class GFG { static int MAXN = 1000000 ; static int [ ] is_prime ; static int [ ] count_of_primes ; static void sieve ( ) { is_prime = new int [ MAXN ] ; count_of_primes = new int [ MAXN ] ; Array . Fill ( is_prime , 0 ) ; Array . Fill ( count_of_primes , 0 ) ; for ( int i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( int i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] == 1 ) { for ( int j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } } is_prime [ 2 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } static long power ( long x , long y , long p ) { long result = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } static void numberOfWays ( int N ) { long count = count_of_primes [ N ] - 1 ; long mod = 1000000007 ; long answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; long ans = answer ; Console . Write ( ans ) ; } public static void Main ( ) { sieve ( ) ; int N = 7 ; numberOfWays ( N ) ; } }


"Count ways to split an array into subarrays such that sum of the i | C # program for the above approach ; Function to count ways to split an array into subarrays such that sum of the i - th subarray is divisible by i ; Stores the prefix sum of array ; Find the prefix sum ; Initialize dp [ ] [ ] array ; Stores the count of splitting ; Iterate over the range [ 0 , N ] ; Update the dp table ; If the last index is reached , then add it to the variable ans ; Return the possible count of splitting of array into subarrays ; Driver Code"
Solution in C#:

using System ; public class GFG { static int countOfWays ( int [ ] arr , int N ) { int [ ] pre = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pre [ i + 1 ] = pre [ i ] + arr [ i ] ; } int [ , ] dp = new int [ N + 2 , N + 2 ] ; dp [ 1 , 0 ] ++ ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N ; j >= 1 ; j -- ) { dp [ j + 1 , pre [ i + 1 ] % ( j + 1 ) ] += dp [ j , pre [ i + 1 ] % j ] ; if ( i == N - 1 ) { ans += dp [ j , pre [ i + 1 ] % j ] ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; Console . WriteLine ( countOfWays ( arr , N ) ) ; } }


"Count ways to split array into K non | C # program to implement the above approach ; Function to get the value of pow ( K , M ) ; Stores value of pow ( K , M ) ; Calculate value of pow ( K , N ) ; If N is odd , update res ; Update M to M / 2 ; Update K ; Function to print total ways to split the array that satisfies the given condition ; Stores total ways that satisfies the given condition ; Stores count of distinct elements in the given arr ; Store distinct elements of the given array ; Traverse the given array ; Insert current element into set st . ; Update M ; Update cntways ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } static int cntWays ( int [ ] arr , int N , int K ) { int cntways = 0 ; int M = 0 ; HashSet < int > st = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . Add ( arr [ i ] ) ; } M = st . Count ; cntways = power ( K , M ) ; return cntways ; } public static void Main ( ) { int [ ] arr = { 2 , 3 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( cntWays ( arr , N , K ) ) ; } }


"Count ways to split array into three non | C # program for the above approach ; Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Driver code ; Given Input ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countWays ( int [ ] arr , int N ) { int arr_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) arr_xor ^= arr [ i ] ; int pref_xor = 0 , suff_xor = 0 ; List < int > pref_ind = new List < int > ( ) ; int [ ] suff_inds = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pref_xor ^= arr [ i ] ; if ( pref_xor == arr_xor ) pref_ind . Add ( i ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suff_xor ^= arr [ i ] ; suff_inds [ i ] += suff_inds [ i + 1 ] ; if ( suff_xor == arr_xor ) suff_inds [ i ] ++ ; } int tot_ways = 0 ; foreach ( int idx in pref_ind ) { if ( idx < N - 1 ) tot_ways += suff_inds [ idx + 2 ] ; } return tot_ways ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 0 , 5 , 2 , 7 } ; int N = arr . Length ; Console . WriteLine ( countWays ( arr , N ) ) ; } }


"Count ways to split array into two equal sum subarrays by changing sign of any one array element | C # program for the above approach ; Function to count ways of splitting the array in two subarrays of equal sum by changing sign of any 1 element ; Stores the count of elements in prefix and suffix of array ; Stores the total sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto an index ; Stores sum of suffix from an index ; Stores the count of ways to split the array in 2 subarrays having equal sum ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix Map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix Map ; Store the difference between the subarrays ; Check if diff is even or not ; Count number of ways to split array at index i such that subarray sums are same ; Update the count ; Return the count ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubArraySignChange ( int [ ] arr , int N ) { Dictionary < int , int > prefixCount = new Dictionary < int , int > ( ) ; Dictionary < int , int > suffixCount = new Dictionary < int , int > ( ) ; int total = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total += arr [ i ] ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] + 1 ; } else { suffixCount . Add ( arr [ i ] , 1 ) ; } } int prefixSum = 0 ; int suffixSum = 0 ; int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixSum += arr [ i ] ; if ( prefixCount . ContainsKey ( arr [ i ] ) ) { prefixCount [ arr [ i ] ] = prefixCount [ arr [ i ] ] + 1 ; } else { prefixCount . Add ( arr [ i ] , 1 ) ; } suffixSum = total - prefixSum ; if ( suffixCount . ContainsKey ( arr [ i ] ) ) { suffixCount [ arr [ i ] ] = suffixCount [ arr [ i ] ] - 1 ; } int diff = prefixSum - suffixSum ; if ( diff % 2 == 0 ) { int x = ( prefixCount . ContainsKey ( diff / 2 ) ? prefixCount : 0 ) + ( suffixCount . ContainsKey ( - diff / 2 ) ? suffixCount [ - diff / 2 ] : 0 ) ; count = count + x ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , - 3 , 3 } ; int N = arr . Length ; Console . Write ( countSubArraySignChange ( arr , N ) ) ; } }


"Count ways to split array into two equal sum subarrays by replacing each array element to 0 once | C # program for the above approach ; Function to find number of ways to split array into 2 subarrays having equal sum by changing element to 0 once ; Stores the count of elements in prefix and suffix of array elements ; Stores the sum of array ; Traverse the array ; Increase the frequency of current element in suffix ; Stores prefix sum upto index i ; Stores sum of suffix of index i ; Stores the desired result ; Traverse the array ; Modify prefix sum ; Add arr [ i ] to prefix map ; Calculate suffix sum by subtracting prefix sum from total sum of elements ; Remove arr [ i ] from suffix map ; Store the difference between the subarrays ; Count number of ways to split the array at index i such that subarray sums are equal ; Update the final result ; Return the result ; Driver Code ; Function Call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countSubArrayRemove ( int [ ] arr , int N ) { Dictionary < int , int > prefix_element_count = new Dictionary < int , int > ( ) ; Dictionary < int , int > suffix_element_count = new Dictionary < int , int > ( ) ; int total_sum_of_elements = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total_sum_of_elements += arr [ i ] ; if ( ! suffix_element_count . ContainsKey ( arr [ i ] ) ) suffix_element_count [ arr [ i ] ] = 1 ; else suffix_element_count [ arr [ i ] ] ++ ; } int prefix_sum = 0 ; int suffix_sum = 0 ; int count_subarray_equal_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefix_sum += arr [ i ] ; if ( ! prefix_element_count . ContainsKey ( arr [ i ] ) ) prefix_element_count [ arr [ i ] ] = 1 ; else prefix_element_count [ arr [ i ] ] ++ ; suffix_sum = total_sum_of_elements - prefix_sum ; if ( ! suffix_element_count . ContainsKey ( arr [ i ] ) ) suffix_element_count [ arr [ i ] ] = 0 ; else suffix_element_count [ arr [ i ] ] -= 1 ; int difference = prefix_sum - suffix_sum ; int number_of_subarray_at_i_split = 0 ; if ( prefix_element_count . ContainsKey ( difference ) ) number_of_subarray_at_i_split = prefix_element_count [ difference ] ; if ( suffix_element_count . ContainsKey ( - difference ) ) number_of_subarray_at_i_split += suffix_element_count [ - difference ] ; count_subarray_equal_sum += number_of_subarray_at_i_split ; } return count_subarray_equal_sum ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 , 3 , 1 } ; int N = arr . Length ; Console . Write ( countSubArrayRemove ( arr , N ) ) ; } }


"Count ways to tile an N | C # program to implement the above approach ; Function to count the ways to tile N * 1 board using 1 * 1 and 2 * 1 tiles ; dp [ i ] : Stores count of ways to tile i * 1 board using given tiles ; Base Case ; Iterate over the range [ 2 , N ] ; Fill dp [ i ] using the recurrence relation ; Driver Code ; Given N ; Function Call"
Solution in C#:

using System ; class GFG { static void countWaysToTileBoard ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = ( 2 * dp [ i - 1 ] + dp [ i - 2 ] ) ; } Console . Write ( dp [ N ] ) ; } public static void Main ( String [ ] args ) { int N = 2 ; countWaysToTileBoard ( N ) ; } }


"Count words that appear exactly two times in an array of words | C # program to count all words with count exactly 2. ; Returns count of words with frequency exactly 2. ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countWords ( String [ ] str , int n ) { Dictionary < String , int > m = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( str [ i ] ) ) { int get = m [ str [ i ] ] ; m . Remove ( str [ i ] ) ; m . Add ( str [ i ] , get + 1 ) ; } else { m . Add ( str [ i ] , 1 ) ; } } int res = 0 ; foreach ( KeyValuePair < String , int > it in m ) { if ( it . Value == 2 ) res ++ ; } return res ; } public static void Main ( String [ ] args ) { String [ ] a = { " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " } ; int n = a . Length ; Console . WriteLine ( countWords ( a , n ) ) ; } }


"Count zeros in a row wise and column wise sorted matrix | C # program to count number of 0 s in the given row - wise and column - wise sorted binary matrix ; Function to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; start from bottom - left corner of the matrix ; stores number of zeroes in the matrix ; move up until you find a 0 ; if zero is not found in current column , we are done ; add 0 s present in current column to result ; move right to next column ; Driver Code"
Solution in C#:

using System ; class GFG { public static int N = 5 ; static int countZeroes ( int [ , ] mat ) { int row = N - 1 , col = 0 ; int count = 0 ; while ( col < N ) { while ( mat [ row , col ] > 0 ) if ( -- row < 0 ) return count ; count += ( row + 1 ) ; col ++ ; } return count ; } public static void Main ( ) { int [ , ] mat = { { 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 1 } , { 0 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } } ; Console . WriteLine ( countZeroes ( mat ) ) ; } }


"Counting Rock Samples | TCS Codevita 2020 | C # program of the above approach ; Function to find the rock samples in the ranges ; Iterate over the ranges ; Driver Code ; Function call"
Solution in C#:

using System . Collections . Generic ; using System ; class GFG { static void findRockSample ( int [ , ] ranges , int n , int r , int [ ] arr ) { List < int > a = new List < int > ( ) ; for ( int i = 0 ; i < r ; i ++ ) { int c = 0 ; int l = ranges [ i , 0 ] ; int h = ranges [ i , 1 ] ; for ( int j = 0 ; j < arr . Length ; j ++ ) { if ( l <= arr [ j ] && arr [ j ] <= h ) c += 1 ; } a . Add ( c ) ; } foreach ( var i in a ) { Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int n = 5 ; int r = 2 ; int [ ] arr = { 400 , 567 , 890 , 765 , 987 } ; int [ , ] ranges = { { 300 , 380 } , { 800 , 1000 } } ; findRockSample ( ranges , n , r , arr ) ; } }


"Counting cross lines in an array | C # program to count cross line in array ; Merges two subarrays of [ ] arr . First subarray is arr [ l . . m ] Second subarray is arr [ m + 1. . r ] ; create temp arrays ; Copy data to temp arrays L [ ] and R [ ] ; i = 0 ; Initial index of first subarray j = 0 ; Initial index of second subarray k = l ; Initial index of merged subarray ; == == == == == == == == == == == == == == == == == == == == == = MAIN PORTION OF CODE == == == == == == == == == == == == == == == == == == == == = add all line which is cross by current element ; Copy the remaining elements of L [ ] , if there are any ; Copy the remaining elements of R [ ] , if there are any ; l is for left index and r is right index of the sub - array of arr to be sorted ; Same as ( l + r ) / 2 , but avoids overflow for large l and h ; Sort first and second halves ; function return count of cross line in an array ; Driver Code"
Solution in C#:

using System ; class GFG { static int count_crossline ; static void merge ( int [ ] arr , int l , int m , int r ) { int i , j , k ; int n1 = m - l + 1 ; int n2 = r - m ; int [ ] L = new int [ n1 ] ; int [ ] R = new int [ n2 ] ; for ( i = 0 ; i < n1 ; i ++ ) { L [ i ] = arr [ l + i ] ; } for ( j = 0 ; j < n2 ; j ++ ) { R [ j ] = arr [ m + 1 + j ] ; } while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; count_crossline += ( n1 - i ) ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } static void mergeSort ( int [ ] arr , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static int countCrossLine ( int [ ] arr , int n ) { mergeSort ( arr , 0 , n - 1 ) ; return count_crossline ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . Length ; Console . WriteLine ( countCrossLine ( arr , n ) ) ; } }


"Counting cross lines in an array | C # program to count cross line in array ; function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver code"
Solution in C#:

using System ; class GFG { static int countCrossLine ( int [ ] arr , int n ) { int count_crossline = 0 ; int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } public static void Main ( ) { int [ ] arr = new int [ ] { 4 , 3 , 1 , 2 } ; int n = arr . Length ; Console . Write ( countCrossLine ( arr , n ) ) ; } }


"Counting even decimal value substrings in a binary string | C # Program to count all even decimal value substring . ; function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generate by this character produce even decimal value . ; Driver code"
Solution in C#:

using System ; class GFG { static int evenDecimalValue ( string str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) { result += ( n - i ) ; } } return result ; } public static void Main ( ) { string str = "10010" ; int n = 5 ; Console . WriteLine ( evenDecimalValue ( str , n ) ) ; } }


"Counting even decimal value substrings in a binary string | C # Program to count all even decimal value substring . ; generate all substring in arr [ 0. . n - 1 ] ; store the count ; Pick starting point ; Pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; Driver code"
Solution in C#:

using System ; class GFG { static int evenDecimalValue ( string str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int decimalValue = 0 ; int powerOf2 = 1 ; for ( int k = i ; k <= j ; k ++ ) { decimalValue += ( ( str [ k ] - '0' ) * powerOf2 ) ; powerOf2 *= 2 ; } if ( decimalValue % 2 == 0 ) result ++ ; } } return result ; } public static void Main ( ) { String str = "10010" ; int n = 5 ; Console . WriteLine ( evenDecimalValue ( str , n ) ) ; } }


"Counting numbers of n digits that are monotone | C # program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code ."
Solution in C#:

using System ; class GFG { static int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ , ] DP = new int [ len , DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 , i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i , 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i , j ] = DP [ i - 1 , j ] + DP [ i , j - 1 ] ; return DP [ len - 1 , DP_s - 1 ] ; } public static void Main ( ) { Console . WriteLine ( getNumMonotone ( 10 ) ) ; } }


"Counting numbers whose difference from reverse is a product of k | C # program to Count the numbers within a given range in which when you subtract a number from its reverse , the difference is a product of k ; function to check if the number and its reverse have their absolute difference divisible by k ; reverse the number ; Driver code"
Solution in C#:

using System ; class GFG { static bool isRevDiffDivisible ( int x , int k ) { int n = x ; int m = 0 ; while ( x > 0 ) { m = m * 10 + x % 10 ; x /= 10 ; } return ( Math . Abs ( n - m ) % k == 0 ) ; } static int countNumbers ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( isRevDiffDivisible ( i , k ) ) ++ count ; return count ; } public static void Main ( ) { int l = 35 , r = 45 , k = 5 ; Console . WriteLine ( countNumbers ( l , r , k ) ) ; } }


"Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of people reduce to ( x - 2 ) . ; Driver code"
Solution in C#:

using System ; class GFG { static int numberOfWays ( int x ) { if ( x == 0 x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } public static void Main ( ) { int x = 3 ; Console . WriteLine ( numberOfWays ( x ) ) ; } }


"Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code"
Solution in C#:

using System ; class GFG { static int numberOfWays ( int x ) { int [ ] dp = new int [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } public static void Main ( ) { int x = 3 ; Console . WriteLine ( numberOfWays ( x ) ) ; } }


"Counts 1 s that can be obtained in an Array by performing given operations | C # program to implement the above approach ; Function to count total number of 1 s in array by performing given operations ; Stores count of 1 s in the array by performing the operations ; Iterate over the range [ 1 , N ] ; Flip all array elements whose index is multiple of i ; Update arr [ i ] ; Traverse the array ; If current element is 1 ; Update cntOnes ; Driver Code"
Solution in C#:

using System ; class GFG { static int cntOnesArrWithGivenOp ( int [ ] arr , int N ) { int cntOnes = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . Length ; Console . Write ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }


"Counts 1 s that can be obtained in an Array by performing given operations | C # program to implement the above approach ; Function to count total number of 1 s in array by performing the given operations ; Stores count of 1 s in the array by performing the operations ; Update cntOnes ; Driver code"
Solution in C#:

using System ; class GFG { static int cntOnesArrWithGivenOp ( int [ ] arr , int N ) { int cntOnes = 0 ; cntOnes = ( int ) Math . Sqrt ( N ) ; return cntOnes ; } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . Length ; Console . WriteLine ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }


"Counts paths from a point to reach Origin : Memory Optimized | C # implementation to count the paths from a points to origin ; Function to count the paths from a point to the origin ; Base Case when the point is already at origin ; Base Case when the point is on the x or y - axis ; Loop to fill all the position as 1 in the array ; Loop to count the number of paths from a point to origin in bottom - up manner ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static long Count_Paths ( int x , int y ) { if ( x == 0 && y == 0 ) return 0 ; if ( x == 0 y == 0 ) return 1 ; int [ ] dp = new int [ Math . Max ( x , y ) + 1 ] ; int p = Math . Max ( x , y ) , q = Math . Min ( x , y ) ; for ( int i = 0 ; i <= p ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i <= q ; i ++ ) for ( int j = 1 ; j <= p ; j ++ ) dp [ j ] += dp [ j - 1 ] ; return dp [ p ] ; } public static void Main ( String [ ] args ) { int x = 3 , y = 3 ; Console . Write ( " Number ▁ of ▁ Paths ▁ " + Count_Paths ( x , y ) ) ; } }


"Covering maximum array elements with given value | C # implementation of the approach ; Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; Driver function"
Solution in C#:

using System ; using System . Linq ; class GFG { static int maxArrayCover ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int cc = 0 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } if ( a . Sum ( ) == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } public static void Main ( ) { int n = 3 ; int x = 70 ; int [ ] a = new int [ ] { 10 , 20 , 30 } ; Console . WriteLine ( maxArrayCover ( a , n , x ) ) ; } }


"Create a Graph by connecting divisors from N to M and find shortest path | C # program for the above approach ; Function to check the number is prime or not ; Base Cases ; Iterate till [ 5 , Math . Sqrt ( N ) ] to detect primarility of numbers ; Function to print the shortest path ; Use vector to store the factor of m and n ; Use map to check if largest common factor previously present or not ; First store m ; Check whether m is prime or not ; Largest common factor of m ; If m is divisible by i ; Store the largest common factor ; For number n ; Check whether n is prime ; Largest common factor of n ; Store the largest common factor ; Print the path Print factors from m ; To astatic void duplicate printing of same element ; Print the factors from n ; Driver Code ; Given N and M ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } static void shortestpath ( int m , int n ) { List < int > mfactor = new List < int > ( ) ; List < int > nfactor = new List < int > ( ) ; Dictionary < int , int > fre = new Dictionary < int , int > ( ) ; mfactor . Add ( m ) ; fre . Add ( m , 1 ) ; while ( m != 1 ) { if ( isprm ( m ) != 0 ) { mfactor . Add ( 1 ) ; if ( ! fre . ContainsKey ( 1 ) ) fre . Add ( 1 , 1 ) ; m = 1 ; } else { for ( int i = 2 ; i <= Math . Sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . Add ( m / i ) ; if ( ! fre . ContainsKey ( m / i ) ) fre . Add ( m / i , 1 ) ; m = ( m / i ) ; break ; } } } } nfactor . Add ( n ) ; while ( fre . ContainsKey ( n ) && fre [ n ] != 1 ) { if ( isprm ( n ) != 0 ) { nfactor . Add ( 1 ) ; n = 1 ; } else { for ( int i = 2 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . Add ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( int i = 0 ; i < mfactor . Count ; i ++ ) { if ( mfactor [ i ] == n ) break ; Console . Write ( mfactor [ i ] + " ▁ < - - > ▁ " ) ; } for ( int i = nfactor . Count - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) Console . Write ( nfactor [ i ] ) ; else Console . Write ( nfactor [ i ] + " ▁ < - - > ▁ " ) ; } } public static void Main ( String [ ] args ) { int m = 18 , n = 19 ; shortestpath ( m , n ) ; } }


"Create a Sorted Array Using Binary Search | C # program to create a sorted array using Binary Search ; Function to create a new sorted array using Binary Search ; Auxiliary Array ; if b is empty any element can be at first place ; Perform Binary Search to find the correct position of current element in the new array ; let the element should be at first index ; if a [ j ] is already present in the new array ; add a [ j ] at mid + 1. you can add it at mid ; if a [ j ] is lesser than b [ mid ] go right side ; means pos should be between start and mid - 1 ; else pos should be between mid + 1 and end ; if a [ j ] is the largest push it at last ; here Max ( 0 , pos ) is used because sometimes pos can be negative as smallest duplicates can be present in the array ; Print the new generated sorted array ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void createSorted ( int [ ] a , int n ) { List < int > b = new List < int > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( b . Count == 0 ) b . Add ( a [ j ] ) ; else { int start = 0 , end = b . Count - 1 ; int pos = 0 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( b [ mid ] == a [ j ] ) { b . Insert ( ( Math . Max ( 0 , mid + 1 ) ) , a [ j ] ) ; break ; } else if ( b [ mid ] > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . Insert ( Math . Max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 5 , 4 , 9 , 8 } ; int n = a . Length ; createSorted ( a , n ) ; } }


"Create a matrix with alternating rectangles of O and X | C # code to demonstrate the working . ; Function to print alternating rectangles of 0 and X ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Store given number of rows and columns for later use ; A 2D array to store the output to be printed ; Iniitialize the character to be stoed in a [ ] [ ] ; Fill characters in a [ ] [ ] in spiral form . Every iteration fills one rectangle of either Xs or Os ; Fill the first row from the remaining rows ; Fill the last column from the remaining columns ; Fill the last row from the remaining rows ; Print the first column from the remaining columns ; Flip character for next iteration ; Print the filled matrix ; Driver program to test above functions"
Solution in C#:

using System ; class GFG { static void fill0X ( int m , int n ) { int i , k = 0 , l = 0 ; int r = m , c = n ; char [ , ] a = new char [ m , n ] ; char x = ' X ' ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) a [ k , i ] = x ; k ++ ; for ( i = k ; i < m ; ++ i ) a [ i , n - 1 ] = x ; n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) a [ m - 1 , i ] = x ; m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) a [ i , l ] = x ; l ++ ; } x = ( x == '0' ) ? ' X ' : '0' ; } for ( i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) Console . Write ( a [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( ) { Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 5 , ▁ n ▁ = ▁ 6" ) ; fill0X ( 5 , 6 ) ; Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 4 , ▁ n ▁ = ▁ 4" ) ; fill0X ( 4 , 4 ) ; Console . WriteLine ( " Output ▁ for " + " ▁ m ▁ = ▁ 3 , ▁ n ▁ = ▁ 4" ) ; fill0X ( 3 , 4 ) ; } }


"Create a new string by alternately combining the characters of two halves of the string in reverse | C # program for creating a string by alternately combining the characters of two halves in reverse ; Function performing calculations ; Calculating the two halves of string s as first and second The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver method ; Calling function"
Solution in C#:

using System ; class GFG { public static void solve ( string s ) { int l = s . Length ; int x = l / 2 ; int y = l ; string p = " " ; while ( x > 0 && y > l / 2 ) { char ch = s [ x - 1 ] ; p += ch ; x -- ; ch = s [ y - 1 ] ; p += ch ; y -- ; } if ( y > l / 2 ) { char ch = s [ x - 1 ] ; p += ch ; y -- ; } Console . WriteLine ( p ) ; } public static void Main ( ) { string s = " sunshine " ; solve ( s ) ; } }


"Create an array of size N with sum S such that no subarray exists with sum S or S | C # program for the above approach ; Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Driver Code ; Given N and sum S ; Function call"
Solution in C#:

using System ; class GFG { static void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { Console . Write ( 2 + " ▁ " ) ; s -= 2 ; } Console . WriteLine ( s ) ; Console . WriteLine ( 1 ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; } }


"Create an array such that XOR of subarrays of length K is X | C # implementation to create an array in which the XOR of all elements of each contiguous sub - array of length K is X ; Function to construct the array ; Creating an array of size K , initialised with 0 ; Initialising the first element with the given XOR ; Driver code"
Solution in C#:

using System ; class GFG { public static void constructArray ( int N , int K , int X ) { int [ ] ans = new int [ K ] ; ans [ 0 ] = X ; for ( int i = 0 ; i < N ; ++ i ) { Console . Write ( ans [ i % K ] + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int N = 5 , K = 2 , X = 4 ; constructArray ( N , K , X ) ; } }