"Number of indexes with equal elements in given range | C # program to count the number of indexes in range L R such that Ai = Ai + 1 ; array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query"
Solution in C#:

using System ; class GFG { static int N = 1000 ; static int [ ] prefixans = new int [ N ] ; static void countIndex ( int [ ] a , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } static int answer_query ( int l , int r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } static void Main ( ) { int [ ] a = new int [ ] { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = a . Length ; countIndex ( a , n ) ; int L , R ; L = 1 ; R = 8 ; Console . WriteLine ( answer_query ( L , R ) ) ; L = 0 ; R = 4 ; Console . WriteLine ( answer_query ( L , R ) ) ; } }


"Number of indexes with equal elements in given range | C # program to count the number of indexes in range L R such that Ai = Ai + 1 ; function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query"
Solution in C#:

using System ; class GFG { static int answer_query ( int [ ] a , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } public static void Main ( ) { int [ ] a = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = a . Length ; int L , R ; L = 1 ; R = 8 ; Console . WriteLine ( answer_query ( a , n , L , R ) ) ; L = 0 ; R = 4 ; Console . WriteLine ( answer_query ( a , n , L , R ) ) ; } }


"Number of indices pair such that element pair sum from first Array is greater than second Array | C # program to find the number of indices pair such that pair sum from first Array is greater than second Array ; Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static long getPairs ( List < int > A , List < int > B , int n ) { int [ ] D = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { D [ i ] = A [ i ] - B [ i ] ; } Array . Sort ( D ) ; long total = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( D [ i ] > 0 ) { total += n - i - 1 ; } else { int k = upper_bound ( D , 0 , D . Length , - D [ i ] ) ; total += n - k ; } } return total ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void Main ( String [ ] args ) { int n = 5 ; List < int > A = new List < int > ( ) ; List < int > B = new List < int > ( ) ; A . Add ( 4 ) ; A . Add ( 8 ) ; A . Add ( 2 ) ; A . Add ( 6 ) ; A . Add ( 2 ) ; B . Add ( 4 ) ; B . Add ( 5 ) ; B . Add ( 4 ) ; B . Add ( 1 ) ; B . Add ( 3 ) ; Console . Write ( getPairs ( A , B , n ) ) ; } }


"Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | C # program to find the numbers of values that satisfy the equation ; This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } static void value ( int a , int b , int c ) { int no , x = 0 , q = 0 , w = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < 82 ; i ++ ) { no = ( int ) Math . Pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . Add ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . Count ; i ++ ) { Console . Write ( v [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ; } }


"Number of intersections between two ranges | C # implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int upperBound ( List < int > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } static int FindIntersection ( List < List < int > > type1 , int n , List < List < int > > type2 , int m ) { int ans = n * m ; List < int > start = new List < int > ( ) ; List < int > end = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { start . Add ( type1 [ i ] [ 0 ] ) ; end . Add ( type1 [ i ] [ 1 ] ) ; } start . Sort ( ) ; end . Sort ( ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 [ i ] [ 0 ] ; int R = type2 [ i ] [ 1 ] ; ans -= start . Count - upperBound ( start , 0 , start . Count , R ) ; ans -= upperBound ( end , 0 , end . Count , L - 1 ) ; } return ans ; } static public void Main ( ) { List < List < int > > type1 = new List < List < int > > ( ) ; type1 . Add ( new List < int > ( ) { 1 , 2 } ) ; type1 . Add ( new List < int > ( ) { 2 , 3 } ) ; type1 . Add ( new List < int > ( ) { 4 , 5 } ) ; type1 . Add ( new List < int > ( ) { 6 , 7 } ) ; List < List < int > > type2 = new List < List < int > > ( ) ; type2 . Add ( new List < int > ( ) { 1 , 5 } ) ; type2 . Add ( new List < int > ( ) { 2 , 3 } ) ; type2 . Add ( new List < int > ( ) { 4 , 7 } ) ; type2 . Add ( new List < int > ( ) { 5 , 7 } ) ; int n = type1 . Count ; int m = type2 . Count ; Console . WriteLine ( FindIntersection ( type1 , n , type2 , m ) ) ; } }


"Number of largest circles that can be inscribed in a rectangle | C # program to implement the above approach ; Function to count the number of largest circles in a rectangle ; If length exceeds breadth ; Swap to reduce length to smaller than breadth ; Return total count of circles inscribed ; Driver Code"
Solution in C#:

using System ; public class GFG { static int totalCircles ( int L , int B ) { if ( L > B ) { int temp = L ; L = B ; B = temp ; } return B / L ; } public static void Main ( String [ ] args ) { int L = 3 ; int B = 8 ; Console . Write ( totalCircles ( L , B ) ) ; } }


"Number of leading zeros in binary representation of a given number | C # program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Driver Code"
Solution in C#:

using System ; class GFG { static int countZeros ( int x ) { int y ; int n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } static public void Main ( ) { int x = 101 ; Console . WriteLine ( countZeros ( x ) ) ; } }


"Number of leading zeros in binary representation of a given number | C # program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Keep shifting x by one until leftmost bit does not become 1. ; Driver Code"
Solution in C#:

using System ; class GFG { static byte sizeofInt = 8 ; static int countZeros ( int x ) { int total_bits = sizeofInt * 8 ; int res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } public static void Main ( String [ ] args ) { int x = 101 ; Console . WriteLine ( countZeros ( x ) ) ; } }


"Number of lines from given N points not parallel to X or Y axis | C # program to find the number of lines which are formed from given N points and not parallel to X or Y axis ; Function to find the number of lines which are formed from given N points and not parallel to X or Y axis ; This will store the number of points has same x or y coordinates using the map as the value of coordinate can be very large ; Counting frequency of each x and y coordinates ; Total number of pairs can be formed ; We can not choose pairs from these as they have same x coordinatethus they will result line segment parallel to y axis ; we can not choose pairs from these as they have same y coordinate thus they will result line segment parallel to x - axis ; Return the required answer ; Driver Code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int NotParallel ( int [ , ] p , int n ) { Dictionary < int , int > x_axis = new Dictionary < int , int > ( ) ; Dictionary < int , int > y_axis = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( x_axis . ContainsKey ( p [ i , 0 ] ) ) x_axis [ p [ i , 0 ] ] = x_axis [ p [ i , 0 ] ] + 1 ; else x_axis . Add ( p [ i , 0 ] , 1 ) ; if ( y_axis . ContainsKey ( p [ i , 1 ] ) ) y_axis [ p [ i , 1 ] ] = y_axis [ p [ i , 1 ] ] + 1 ; else y_axis . Add ( p [ i , 1 ] , 1 ) ; } int total = ( n * ( n - 1 ) ) / 2 ; foreach ( KeyValuePair < int , int > i in x_axis ) { int c = i . Value ; total -= ( c * ( c - 1 ) ) / 2 ; } foreach ( KeyValuePair < int , int > i in y_axis ) { int c = i . Value ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } public static void Main ( String [ ] args ) { int [ , ] p = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = p . GetLength ( 0 ) ; Console . Write ( NotParallel ( p , n ) ) ; } }


"Number of local extrema in an array | C # to find number of extrema ; function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; Driver program"
Solution in C#:

using System ; class GFG { static int extrema ( int [ ] a , int n ) { int count = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } public static void Main ( ) { int [ ] a = { 1 , 0 , 2 , 1 } ; int n = a . Length ; Console . WriteLine ( extrema ( a , n ) ) ; } }


"Number of mismatching bits in the binary representation of two integers | C # implementation of the approach ; compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits"
Solution in C#:

using System ; class GFG { static void solve ( int A , int B ) { int count = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } Console . WriteLine ( " Number ▁ of ▁ different ▁ bits ▁ : ▁ " + count ) ; } public static void Main ( ) { int A = 12 , B = 15 ; solve ( A , B ) ; } }


"Number of moves required to guess a permutation . | C # implementation of the approach ; Function that returns the required moves ; Final move ; Driver Program to test above function"
Solution in C#:

using System ; class GFG { static int countMoves ( int n ) { int ct = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ct += i * ( n - i ) ; ct += n ; return ct ; } static void Main ( ) { int n = 3 ; Console . WriteLine ( countMoves ( n ) ) ; } }


"Number of n digit numbers that do not contain 9 | C # program to find number of n digit numbers that do not contain 9 as it 's digit ; function to find number of n digit numbers possible ; Driver Code"
Solution in C#:

using System ; public class GFG { static int totalNumber ( int n ) { return 8 * ( int ) Math . Pow ( 9 , n - 1 ) ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( totalNumber ( n ) ) ; } }


"Number of n digit stepping numbers | C # program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; driver program to test the above function"
Solution in C#:

using System ; class GFG { static long answer ( int n ) { int [ , ] dp = new int [ n + 1 , 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 , j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i , j ] = dp [ i - 1 , j + 1 ] ; else if ( j == 9 ) dp [ i , j ] = dp [ i - 1 , j - 1 ] ; else dp [ i , j ] = dp [ i - 1 , j - 1 ] + dp [ i - 1 , j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ n , j ] ; return sum ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( answer ( n ) ) ; } }


"Number of n | C # function to find number of diagonals in n sided convex polygon ; Function that returns count of non - decreasing numbers with n digits ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; driver program"
Solution in C#:

using System ; class GFG { static int nonDecNums ( int n ) { int [ , ] a = new int [ n + 1 , 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 , i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i , 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i , j ] = a [ i - 1 , j ] + a [ i , j + 1 ] ; return a [ n , 0 ] ; } public static void Main ( ) { int n = 2 ; Console . WriteLine ( " Non - decreasing ▁ digits ▁ = ▁ " + nonDecNums ( n ) ) ; } }


"Number of n | C # program To calculate Number of n - digits non - decreasing integers ; Returns factorial of n ; returns nCr ; Driver code"
Solution in C#:

using System ; class GFG { static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static long nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } public static void Main ( String [ ] args ) { int n = 2 ; Console . Write ( " Number ▁ of ▁ Non - Decreasing ▁ digits : ▁ " + nCr ( n + 9 , 9 ) ) ; } }


"Number of non | A naive C # solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Initialize result ; Consider all triplets and increment result whenever sum of a triplet is n . ; Driver code"
Solution in C#:

using System ; class GFG { static int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; } public static void Main ( ) { int n = 3 ; Console . Write ( countIntegralSolutions ( n ) ) ; } }


"Number of non | C # implementation of the approach ; Function to return the count of increasing subarrays of length k ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Updating the required count ; Updating i ; Returning res ; Driver code"
Solution in C#:

using System ; class GFG { static int cntSubArrays ( int [ ] arr , int n , int k ) { int res = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; res += Math . Max ( j - i - k + 1 , 0 ) ; i = j ; } return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 2 , 5 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( cntSubArrays ( arr , n , k ) ) ; } }


"Number of non | C # implementation of the approach ; Function to return the required count ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Update ret ; Update i ; Return ret ; Driver code"
Solution in C#:

using System ; class GFG { static int findCnt ( int [ ] arr , int n , int k ) { int ret = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; int x = Math . Max ( 0 , j - i - k + 1 ) ; ret += ( x * ( x + 1 ) ) / 2 ; i = j ; } return ret ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 , 2 , 1 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( findCnt ( arr , n , k ) ) ; } }


"Number of non | C # program to find the numbers of non negative integral solutions ; return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver code"
Solution in C#:

using System ; class GFG { static int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } public static void Main ( ) { int n = 5 ; int val = 20 ; Console . WriteLine ( countSolutions ( n , val ) ) ; } }


"Number of non | C # solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Driver code"
Solution in C#:

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


"Number of occurrences of 2 as a digit in numbers from 0 to n | C # code for above implementation ; Driver code"
Solution in C#:

using System ; class GFG { static int numberOf2sinRange ( int n ) { string s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += i + " " ; int count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == '2' ) { count ++ ; } } return count ; } public static void Main ( string [ ] args ) { int n = 30 ; Console . Write ( numberOf2sinRange ( n ) ) ; } }


"Number of occurrences of 2 as a digit in numbers from 0 to n | C # program to count 2 s from 0 to n ; Counts the number of '2' digits in a single number ; Counts the number of '2' digits between 0 and n ; Initialize result ; Count 2 's in every number  from 2 to n ; Driver code"
Solution in C#:

using System ; class GFG { static int number0f2s ( int n ) { int count = 0 ; while ( n > 0 ) { if ( n % 10 == 2 ) count ++ ; n = n / 10 ; } return count ; } static int numberOf2sinRange ( int n ) { int count = 0 ; for ( int i = 2 ; i <= n ; i ++ ) count += number0f2s ( i ) ; return count ; } public static void Main ( ) { Console . Write ( numberOf2sinRange ( 22 ) ) ; Console . WriteLine ( ) ; Console . Write ( numberOf2sinRange ( 100 ) ) ; } }


"Number of occurrences of 2 as a digit in numbers from 0 to n | C # program to count 2 s from 0 to n ; Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code"
Solution in C#:

using System ; class GFG { static int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) Math . Pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } static int numberOf2sinRange ( int number ) { string convert ; convert = number . ToString ( ) ; string s = convert ; int len = s . Length ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } public static void Main ( ) { Console . WriteLine ( numberOf2sinRange ( 22 ) ) ; Console . WriteLine ( numberOf2sinRange ( 100 ) ) ; } }


"Number of occurrences of a given angle formed using 3 vertices of a n | C # implementation of the approach ; Function that calculates occurrences of given angle that can be created using any 3 sides ; Maximum angle in a regular n - gon is equal to the interior angle If the given angle is greater than the interior angle then the given angle cannot be created ; The given angle times n should be divisible by 180 else it cannot be created ; Initialise answer ; Calculate the frequency of given angle for each vertex ; Multiply answer by frequency . ; Multiply answer by the number of vertices . ; Driver code"
Solution in C#:

using System ; class GFG { static int solve ( int ang , int n ) { if ( ( ang * n ) > ( 180 * ( n - 2 ) ) ) { return 0 ; } else if ( ( ang * n ) % 180 != 0 ) { return 0 ; } int ans = 1 ; int freq = ( ang * n ) / 180 ; ans = ans * ( n - 1 - freq ) ; ans = ans * n ; return ans ; } public static void Main ( String [ ] args ) { int ang = 90 , n = 4 ; Console . WriteLine ( solve ( ang , n ) ) ; } }


"Number of odd and even results for every value of x in range [ min , max ] after performing N steps | C # program to print Number of odd / even results for every value of x in range [ min , end ] after performing N steps ; Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code"
Solution in C#:

using System ; class GFG { static void count_even_odd ( int min , int max , int [ , ] steps ) { int a , b , even , odd ; bool beven = true , aeven = false ; int n = 2 ; for ( int i = 0 ; i < n ; i ++ ) { a = steps [ i , 0 ] ; b = steps [ i , 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } else { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; else odd += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; Console . Write ( " even ▁ = ▁ " + even + " , ▁ odd ▁ = ▁ " + odd ) ; } public static void Main ( ) { int min = 1 , max = 4 ; int [ , ] steps = { { 1 , 2 } , { 3 , 4 } } ; count_even_odd ( min , max , steps ) ; } }


"Number of ones in the smallest repunit | C # program to print the number of 1 s in smallest repunit multiple of the number . ; Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function"
Solution in C#:

using System ; class GFG { static int countOnes ( int n ) { int count = 1 ; int rem = 1 ; while ( rem != 0 ) { rem = ( rem * 10 + 1 ) % n ; count ++ ; } return count ; } static public void Main ( ) { int n = 13 ; Console . WriteLine ( countOnes ( n ) ) ; } }


"Number of open doors | TCS Coding Question | Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code"
Solution in C#:

using System ; class GFG { static int countOpenDoors ( int N ) { int doorsOpen = ( int ) Math . Sqrt ( N ) ; return doorsOpen ; } public static void Main ( ) { int N = 100 ; Console . Write ( countOpenDoors ( N ) ) ; } }


"Number of ordered pairs such that ( Ai & Aj ) = 0 | C # program to calculate the number of ordered pairs such that their bitwise and is zero ; Efficient function to count pairs ; Stores the frequency of each number ; Count the frequency of every element ; Iterate for al possible values that a [ i ] can be ; If the last bit is ON ; is the last bit is OFF ; Iterate till n ; If mask 's ith bit is set ; If mask 's ith bit is not set ; Iterate for all the array element and count the number of pairs ; return answer ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 15 ; static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; int [ , ] dp = new int [ 1 << N , N + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash . ContainsKey ( a [ i ] ) ) { hash [ a [ i ] ] += 1 ; } else { hash . Add ( a [ i ] , 1 ) ; } } for ( int mask = 0 ; mask < ( 1 << N ) ; ++ mask ) { if ( ( mask & 1 ) != 0 ) { if ( hash . ContainsKey ( mask ) ) { dp [ mask , 0 ] = hash [ mask ] ; } if ( hash . ContainsKey ( mask ^ 1 ) ) { dp [ mask , 0 ] += hash [ mask ^ 1 ] ; } } else { if ( hash . ContainsKey ( mask ) ) { dp [ mask , 0 ] = hash [ mask ] ; } } for ( int i = 1 ; i <= N ; ++ i ) { if ( ( mask & ( 1 << i ) ) != 0 ) { dp [ mask , i ] = dp [ mask , i - 1 ] + dp [ mask ^ ( 1 << i ) , i - 1 ] ; } else { dp [ mask , i ] = dp [ mask , i - 1 ] ; } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] , N ] ; } return ans ; } static void Main ( ) { int [ ] a = { 5 , 4 , 1 , 6 } ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n ) ) ; } }


"Number of ordered pairs such that ( Ai & Aj ) = 0 | C # program to calculate the number of ordered pairs such that their bitwise and is zero ; Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) arev considered different ; Driver Code"
Solution in C#:

using System ; class GFG { static int countPairs ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( a [ i ] & a [ j ] ) == 0 ) count += 2 ; } return count ; } public static void Main ( ) { int [ ] a = { 3 , 4 , 2 } ; int n = a . Length ; Console . Write ( countPairs ( a , n ) ) ; } }


"Number of ordered points pair satisfying line equation | C # code to find number of ordered points satisfying line equation ; Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c"
Solution in C#:

using System ; class GFG { static bool isValid ( int [ ] arr , int i , int j , int m , int c ) { if ( i == j ) return false ; int lhs = arr [ j ] ; int rhs = m * arr [ i ] + c ; return ( lhs == rhs ) ; } static int findOrderedPoints ( int [ ] arr , int n , int m , int c ) { int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int firstIndex = i , secondIndex = j ; if ( isValid ( arr , firstIndex , secondIndex , m , c ) ) counter ++ ; } } return counter ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 2 } ; int n = arr . Length ; int m = 1 , c = 1 ; Console . Write ( findOrderedPoints ( arr , n , m , c ) ) ; } }


"Number of pair of positions in matrix which are not accessible | C # program to count number of pair of positions in matrix which are not accessible ; Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int k ; static void dfs ( List < int > [ ] graph , bool [ ] visited , int x ) { for ( int i = 0 ; i < graph [ x ] . Count ; i ++ ) { if ( ! visited [ graph [ x ] [ i ] ] ) { ( k ) ++ ; visited [ graph [ x ] [ i ] ] = true ; dfs ( graph , visited , graph [ x ] [ i ] ) ; } } } static int countNonAccessible ( List < int > [ ] graph , int N ) { bool [ ] visited = new bool [ N * N + N ] ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i ) ; ans += k * ( N * N - k ) ; } } return ans ; } static void insertpath ( List < int > [ ] graph , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . Add ( b ) ; graph [ b ] . Add ( a ) ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > [ ] graph = new List < int > [ N * N + 1 ] ; for ( int i = 1 ; i <= N * N ; i ++ ) graph [ i ] = new List < int > ( ) ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; Console . WriteLine ( countNonAccessible ( graph , N ) ) ; } }


"Number of pairs from the first N natural numbers whose sum is divisible by K | C # implementation of the approach ; Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs"
Solution in C#:

class GfG { static int findPairCount ( int N , int K ) { int count = 0 ; int [ ] rem = new int [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } static void Main ( ) { int N = 10 , K = 4 ; System . Console . WriteLine ( findPairCount ( N , K ) ) ; } }


"Number of pairs in an array having sum equal to product | C # implementation of the approach ; Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code"
Solution in C#:

using System ; class GFG { static int sumEqualProduct ( int [ ] a , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . Length ; Console . Write ( sumEqualProduct ( a , n ) ) ; } }


"Number of pairs such that path between pairs has the two vertices A and B | C # program to find the number of pairs such that the path between every pair contains two given vertices ; Function to perform DFS on the given graph by fixing the a vertex ; To mark a particular vertex as visited ; Variable to store the count of the vertices which can be reached from a ; Performing the DFS by iterating over the visited array ; If the vertex is not visited and removing the vertex b ; Function to return the number of pairs such that path between any two pairs consists of the given two vertices A and B ; Initializing the visited array and assigning it with 0 's ; Initially , the count of vertices is 0 ; Performing DFS by removing the vertex B ; Count the vertices which cannot be reached after removing the vertex B ; Again reinitializing the visited array ; Setting the count of vertices to 0 to perform the DFS again ; Performing the DFS by removing the vertex A ; Count the vertices which cannot be reached after removing the vertex A ; Multiplying both the vertices set ; Driver code ; Loop to store the graph"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int N = 1000001 ; static int c , n , m , a , b ; static void dfs ( int a , int b , List < int > [ ] v , int [ ] vis ) { vis [ a ] = 1 ; c ++ ; foreach ( int i in v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } static void Calculate ( List < int > [ ] v ) { int [ ] vis = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( a , b , v , vis ) ; int ans1 = n - c - 1 ; for ( int i = 0 ; i < n + 1 ; i ++ ) vis [ i ] = 0 ; c = 0 ; dfs ( b , a , v , vis ) ; int ans2 = n - c - 1 ; Console . Write ( ans1 * ans2 + "
 " ) ; } public static void Main ( String [ ] args ) { n = 7 ; m = 7 ; a = 3 ; b = 5 ; int [ , ] edges = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 7 , 5 } } ; List < int > [ ] v = new List < int > [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { v [ i ] = new List < int > ( ) ; } for ( int i = 0 ; i < m ; i ++ ) { v [ edges [ i , 0 ] ] . Add ( edges [ i , 1 ] ) ; v [ edges [ i , 1 ] ] . Add ( edges [ i , 0 ] ) ; } Calculate ( v ) ; } }


"Number of pairs such that their HCF and LCM is equal | C # program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( frequency . ContainsKey ( a [ i ] ) ) { var val = frequency [ a [ i ] ] ; frequency . Remove ( a [ i ] ) ; frequency . Add ( a [ i ] , val + 1 ) ; } else { frequency . Add ( a [ i ] , 1 ) ; } } int count = 0 ; foreach ( KeyValuePair < int , int > entry in frequency ) { int f = entry . Value ; count += f * ( f - 1 ) / 2 ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . Write ( countPairs ( arr , n ) ) ; } }


"Number of pairs such that their HCF and LCM is equal | Naive C # program to count number of pairs such that their hcf and lcm are equal ; Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int countPairs ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int n = arr . Length ; Console . Write ( countPairs ( arr , n ) ) ; } }


"Number of pairs whose product is a power of 2 | C # program for the above approach ; Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function call"
Solution in C#:

using System ; using System . Linq ; class GFG { static void countPairs ( int [ ] arr , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( Convert . ToString ( arr [ i ] , 2 ) ) . Count ( f => ( f == '1' ) ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; Console . WriteLine ( desiredPairs + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 7 , 2 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }


"Number of pairs whose sum is a power of 2 | C # implementation of above approach ; Function to return the count of valid pairs ; Storing occurrences of each element ; Start taking largest element each time ; If element has already been paired ; Find the number which is greater than a [ i ] and power of two ; If there is a number which adds up with a [ i ] to form a power of two ; Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired ; Remove already paired elements ; Return the count ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int countPairs ( int [ ] a , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! map . ContainsKey ( a [ i ] ) ) map . Add ( a [ i ] , 1 ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map [ a [ i ] ] < 1 ) continue ; int cur = 1 ; while ( cur <= a [ i ] ) cur <<= 1 ; if ( map . ContainsKey ( cur - a [ i ] ) ) { if ( cur - a [ i ] == a [ i ] && map [ a [ i ] ] == 1 ) continue ; count ++ ; map [ cur - a [ i ] ] = map [ cur - a [ i ] ] - 1 ; map [ a [ i ] ] = map [ a [ i ] ] - 1 ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 3 , 11 , 14 , 5 , 13 } ; int n = a . Length ; Console . WriteLine ( countPairs ( a , n ) ) ; } }


"Number of pairs with Bitwise OR as Odd number | C # program to count pairs with odd OR ; Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main"
Solution in C#:

using System ; public class GFG { static int countOddPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 != 1 ) ) { count ++ ; } } int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } public static void Main ( ) { int [ ] A = { 5 , 6 , 2 , 8 } ; int N = A . Length ; Console . WriteLine ( countOddPair ( A , N ) ) ; } }


"Number of pairs with Bitwise OR as Odd number | C # program to count pairs with odd OR ; Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code"
Solution in C#:

using System ; public class GFG { static int findOddPair ( int [ ] A , int N ) { int oddPair = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } static public void Main ( ) { int [ ] A = { 5 , 6 , 2 , 8 } ; int N = A . Length ; Console . WriteLine ( findOddPair ( A , N ) ) ; } }


"Number of pairs with Pandigital Concatenation | C # program to count PanDigital pairs ; Stores digits present in String v [ i ] atleast once . We use a set as we only need digits which exist only once ( irrespective of reputation ) ; Calculate the mask by considering all digits existing atleast once ; Increment the frequency of this mask ; Returns number of pairs of Strings resulting in Pandigital Concatenations ; All possible Strings lie between 1 and 1023 so we iterate over every possible mask ; If the concatenation results in mask of Pandigital Concatenation , calculate all pairs formed with Masks i and j ; Since every pair is considers twice , we get rid of half of these ; Find frequencies of all masks in given vector of Strings ; Return all possiblg concatenations . ; Driver code"
Solution in C#:

 using System ; using System . Collections . Generic ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( List < String > v , Dictionary < int , int > freq ) { for ( int i = 0 ; i < v . Count ; i ++ ) { int mask = 0 ; HashSet < int > digits = new HashSet < int > ( ) ; for ( int j = 0 ; j < v [ i ] . Length ; j ++ ) digits . Add ( v [ i ] [ j ] - '0' ) ; foreach ( int it in digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . ContainsKey ( mask ) ) { freq [ mask ] ++ ; } else { freq . Add ( mask , 1 ) ; } } } static int pandigitalConcatenations ( Dictionary < int , int > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . ContainsKey ( j ) && freq . ContainsKey ( i ) ) { if ( i == j ) ans += ( freq [ i ] * ( freq [ i ] - 1 ) ) ; else ans += ( freq [ i ] * freq [ j ] ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( List < String > v ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void Main ( String [ ] args ) { List < String > v = new List < String > ( ) ; v . Add ( "123567" ) ; v . Add ( "098234" ) ; v . Add ( "14765" ) ; v . Add ( "19804" ) ; Console . Write ( countPandigitalPairs ( v ) + "
 " ) ; } }


"Number of pairs with Pandigital Concatenation | C # program to find all Pandigital concatenations of two strings . ; Checks if a given string is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int isPanDigital ( string s ) { int [ ] digits = new int [ 10 ] ; Array . Clear ( digits , 0 , 10 ) ; for ( int i = 0 ; i < s . Length ; i ++ ) digits [ s [ i ] - ( int ) '0' ] = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( digits [ i ] == 0 ) return 0 ; return 1 ; } static int countPandigitalPairs ( ref List < string > v ) { int pairs = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) for ( int j = i + 1 ; j < v . Count ; j ++ ) if ( isPanDigital ( v [ i ] + v [ j ] ) == 1 ) pairs ++ ; return pairs ; } static void Main ( ) { List < string > v = new List < string > { "123567" , "098234" , "14765" , "19804" } ; Console . WriteLine ( countPandigitalPairs ( ref v ) ) ; } }


"Number of pairs with maximum sum | C # program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; driver program"
Solution in C#:

using System ; class GFG { static int sum ( int [ ] a , int n ) { int maxVal = a [ 0 ] , maxCount = 1 ; int secondMax = int . MinValue ; int secondMaxCount = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * ( maxCount - 1 ) / 2 ; return secondMaxCount ; } public static void Main ( ) { int [ ] array = { 1 , 1 , 1 , 2 , 2 , 2 , 3 } ; int n = array . Length ; Console . WriteLine ( sum ( array , n ) ) ; } }


"Number of pairs with maximum sum | C # program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; traverse through all the pairs ; traverse through all pairs and keep a count of the number of maximum pairs ; driver program to test the above function"
Solution in C#:

using System ; class GFG { static int sum ( int [ ] a , int n ) { int maxSum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) maxSum = Math . Max ( maxSum , a [ i ] + a [ j ] ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } public static void Main ( ) { int [ ] array = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = array . Length ; Console . WriteLine ( sum ( array , n ) ) ; } }


"Number of palindromic permutations | Set 1 | C # program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 256 ; static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int countPalinPermutations ( string str ) { int n = str . Length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] ] ++ ; long res = fact ( n / 2 ) ; bool oddFreq = false ; for ( int i = 0 ; i < MAX ; i ++ ) { int half = freq [ i ] / 2 ; if ( freq [ i ] % 2 != 0 ) { if ( oddFreq == true ) return 0 ; oddFreq = true ; } res = res / fact ( half ) ; } return ( int ) res ; } public static void Main ( ) { string str = " gffg " ; Console . WriteLine ( countPalinPermutations ( str ) ) ; } }


"Number of palindromic subsequences of length k where k <= 3 | C # program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driver code"
Solution in C#:

using System ; class GFG { static int MAX = 100 ; static int MAX_CHAR = 26 ; static void precompute ( string s , int n , int [ , ] l , int [ , ] r ) { l [ s [ 0 ] - ' a ' , 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) l [ j , i ] += l [ j , i - 1 ] ; l [ s [ i ] - ' a ' , i ] ++ ; } r [ s [ n - 1 ] - ' a ' , n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) r [ j , i ] += r [ j , i + 1 ] ; r [ s [ i ] - ' a ' , i ] ++ ; } } static int countPalindromes ( int k , int n , int [ , ] l , int [ , ] r ) { int ans = 0 ; if ( k == 1 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i , n - 1 ] ; return ans ; } if ( k == 2 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i , n - 1 ] * ( l [ i , n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( int i = 1 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j , i - 1 ] * r [ j , i + 1 ] ; return ans ; } public static void Main ( ) { string s = " aabab " ; int k = 2 ; int n = s . Length ; int [ , ] l = new int [ MAX_CHAR , MAX ] ; int [ , ] r = new int [ MAX_CHAR , MAX ] ; precompute ( s , n , l , r ) ; Console . Write ( countPalindromes ( k , n , l , r ) ) ; } }


"Number of perfect cubes between two given numbers | A Simple Method to count cubes between a and b ; Function to count cubes between two numbers ; Traverse through all numbers ; Check if current number ' i ' is perfect cube ; Driver code"
Solution in C#:

using System ; class GFG { static int countCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j * j <= i ; j ++ ) if ( j * j * j == i ) cnt ++ ; return cnt ; } public static void Main ( ) { int a = 7 , b = 30 ; Console . Write ( " Count ▁ of ▁ Cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }


"Number of perfect cubes between two given numbers | An Efficient Method to count cubes between a and b C # implementation of the above approach ; Function to count cubes between two numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int countCubes ( int a , int b ) { return ( int ) ( Math . Floor ( Math . Cbrt ( b ) ) - Math . Ceiling ( Math . Cbrt ( a ) ) + 1 ) ; } public static void Main ( string [ ] args ) { int a = 7 , b = 28 ; Console . WriteLine ( " Count ▁ of ▁ cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }


"Number of perfect squares between two given numbers | C # program for efficient method to count squares between a & b ; Function to count squares ; Driver Code"
Solution in C#:

using System ; class GFG { static double countSquares ( int a , int b ) { return ( Math . Floor ( Math . Sqrt ( b ) ) - Math . Ceiling ( Math . Sqrt ( a ) ) + 1 ) ; } public static void Main ( ) { int a = 9 , b = 25 ; Console . Write ( " Count ▁ of ▁ squares ▁ is ▁ " + ( int ) countSquares ( a , b ) ) ; } }


"Number of permutation with K inversions | C # program to find number of permutation with K inversion using Memoization ; Limit on N and K ; 2D array memo for stopping solving same problem again ; method recursively calculates permutation with K inversion ; base cases ; if already solved then return result directly ; calling recursively all subproblem of permutation size N - 1 ; Call recursively only if total inversion to be made are less than size ; store result into memo ; Driver Code"
Solution in C#:

using System ; class GFG { static int M = 100 ; static int [ , ] memo = new int [ M , M ] ; static int numberOfPermWithKInversion ( int N , int K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N , K ] != 0 ) return memo [ N , K ] ; int sum = 0 ; for ( int i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N , K ] = sum ; return sum ; } static public void Main ( ) { int N = 4 ; int K = 2 ; Console . WriteLine ( numberOfPermWithKInversion ( N , K ) ) ; } }


"Number of permutations of a string in which all the occurrences of a given character occurs together | C # implementation of above approach ; Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't present in the string  then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase"
Solution in C#:

using System ; class GFG { static int fact ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } static int getResult ( string str , char ch ) { int [ ] has = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) has [ str [ i ] - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . Length ; total = total - particular + 1 ; int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } public static void Main ( ) { string str = " MISSISSIPPI " ; Console . WriteLine ( getResult ( str , ' S ' ) ) ; } }


"Number of points lying inside a rectangle as well as a triangle | C # program for the above approach ; Function to calculate area of a triangle ; Return the resultant area ; Function to check if a point lies inside a triangle or not ; Calculate area of triangle ABC ; Calculate area of triangle formed by connecting B , C , point ; Calculate area of triangle formed by connecting A , C , point ; Calculate area of triangle formed by connecting A , B , point ; Check if the sum of the areas of above three triangles the same as ABC ; Function to count the number of points lying inside a triangle & rectangle ; Stores the coordinates of the vertices of the triangles ; Stores the number of points lying inside the triangle and rectangle ; Traverse the array of points ; Stores whether the current point lies inside triangle1 or not ; Stores whether the current point lies inside triangle2 or not ; Stores whether the current point lies inside triangle3 or not ; Stores whether the current point lies inside triangle4 or not ; Stores whether the current point lies inside given triangle or not ; If current point lies inside given triangle as well as inside any of the four obtained triangles ; Print the count of points ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; public class GFG { static int getArea ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . Abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) ; } static int isInside ( List < List < int > > triangle , List < int > point ) { List < int > A = triangle [ 0 ] ; List < int > B = triangle [ 1 ] ; List < int > C = triangle [ 2 ] ; int x = point [ 0 ] ; int y = point [ 1 ] ; int ABC = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; int BPC = getArea ( x , y , B [ 0 ] , B [ 1 ] , C [ 0 ] , C [ 1 ] ) ; int APC = getArea ( A [ 0 ] , A [ 1 ] , x , y , C [ 0 ] , C [ 1 ] ) ; int APB = getArea ( A [ 0 ] , A [ 1 ] , B [ 0 ] , B [ 1 ] , x , y ) ; return ABC == ( APC + APB + BPC ) ? 1 : 0 ; } static void countPoints ( List < List < int > > rectangle , List < List < int > > triangle , List < List < int > > points ) { int n = rectangle . Count ; List < List < int > > triangle1 = new List < List < int > > ( ) ; for ( int i = 1 ; i < n ; i ++ ) triangle1 . Add ( rectangle [ i ] ) ; List < List < int > > triangle2 = new List < List < int > > ( ) ; for ( int i = 0 ; i < 3 ; i ++ ) { triangle2 . Add ( rectangle [ i ] ) ; } List < List < int > > triangle3 = new List < List < int > > ( ) ; for ( int i = 0 ; i < 2 ; i ++ ) { triangle3 . Add ( rectangle [ i ] ) ; } triangle3 . Add ( rectangle [ 3 ] ) ; List < List < int > > triangle4 = new List < List < int > > ( ) ; for ( int i = n - 2 ; i < n ; i ++ ) { triangle4 . Add ( rectangle [ i ] ) ; } triangle4 . Add ( rectangle [ 0 ] ) ; int ans = 0 ; foreach ( List < int > point in points ) { int condOne = isInside ( triangle1 , point ) ; int condTwo = isInside ( triangle2 , point ) ; int condThree = isInside ( triangle3 , point ) ; int condFour = isInside ( triangle4 , point ) ; int condFive = isInside ( triangle , point ) ; if ( ( condOne != 0 condTwo != 0 condThree != 0 condFour != 0 ) && condFive != 0 ) ans += 1 ; } Console . WriteLine ( ans ) ; } static public void Main (...


"Number of positions such that adding K to the element is greater than sum of all other elements | C # implementation of the approach ; Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code"
Solution in C#:

using System ; class GFG { static int validPosition ( int [ ] arr , int N , int K ) { int count = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 6 , 7 } ; int K = 4 ; int N = arr . Length ; Console . WriteLine ( validPosition ( arr , N , K ) ) ; } }


"Number of positions where a letter can be inserted such that a string becomes palindrome | C # code to find the no . of positions where a letter can be inserted to make it a palindrome . ; Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if the length is even ; sub - case - I ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPalindrome ( String s , int i , int j ) { int p = j ; for ( int k = i ; k <= p ; k ++ ) { if ( s [ k ] != s [ p ] ) return false ; p -- ; } return true ; } static int countWays ( String s ) { int n = s . Length ; int count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( int i = n / 2 ; i < n ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) count ++ ; else break ; } if ( n % 2 == 0 ) { count ++ ; count = 2 * count + 1 ; } else count = 2 * count + 2 ; } else { for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( s [ i ] != s [ n - 1 - i ] ) { int j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( s [ k ] != s [ j ] ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( int k = n - i ; k < n ; k ++ ) { if ( s [ k ] != s [ i ] ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } public static void Main ( ) { String s = " abca " ; Console . Write ( countWays ( s ) ) ; } }


"Number of possible permutations when absolute difference between number of elements to the right and left are given | C # implementation of the above approach ; Function to find the number of permutations possible of the original array to satisfy the given absolute differences ; To store the count of each a [ i ] in a map ; if n is odd ; check the count of each whether it satisfy the given criteria or not ; there is only 1 way for middle element . ; for others there are 2 ways . ; now find total ways ; When n is even . ; there will be no middle element so for each a [ i ] there will be 2 ways ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int totalways ( int [ ] arr , int n ) { Dictionary < int , int > cnt = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( cnt . ContainsKey ( arr [ i ] ) ) { cnt [ arr [ i ] ] = cnt [ arr [ i ] ] + 1 ; } else { cnt . Add ( arr [ i ] , 1 ) ; } } if ( n % 2 == 1 ) { int start = 0 , endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { if ( i == 0 ) { if ( cnt [ i ] != 1 ) { return 0 ; } } else { if ( cnt [ i ] != 2 ) { return 0 ; } } } int ways = 1 ; start = 2 ; endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } else if ( n % 2 == 0 ) { int start = 1 , endd = n - 1 ; for ( int i = 1 ; i <= endd ; i = i + 2 ) { if ( cnt [ i ] != 2 ) return 0 ; } int ways = 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } return int . MinValue ; } public static void Main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 2 , 4 , 4 , 0 , 2 } ; Console . WriteLine ( totalways ( arr , N ) ) ; } }


"Number of primes in a subarray ( with updates ) | C # program to find number of prime numbers in a subarray and performing updates ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; A utility function to get the middle index from corner indexes . ; A recursive function to get the number of primes in a given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree index -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ index ] qs & qe -- > Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the number of primes in the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; A recursive function to update the nodes which have the given index in their range . The following are parameters st , si , ss and se are same as getSumUtil ( ) i -- > index of the element to be updated . This index is in input array . diff -- > Value to be added to all nodes which have i in range ; Base Case : If the input index lies outside the range of this segment ; If the input index is in range of this node , then update the value of the node and its children ; The function to update a value in input array and segment tree . It uses updateValueUtil ( ) to update the value in segment tree ; Check for erroneous input index ; Update the value in array ; Case 1 : Old and new values both are primes ; Case 2 : Old and new values both non primes ; Case 3 : Old value was prime , new value is non prime ; Case 4 : Old value was non prime , new_val is prime ; Update the values of nodes in segment tree ; Return number of primes in range from index qs ( query start ) to qe ( query end ) . It mainly uses queryPrimesUtil ( ) ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si..."
Solution in C#:

using System ; public class GFG { static int MAX = 1000 ; static void sieveOfEratosthenes ( bool [ ] isPrime ) { isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) isPrime [ i ] = false ; } } } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryPrimesUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryPrimesUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryPrimesUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int [ ] arr , int [ ] st , int n , int i , int new_val , bool [ ] isPrime ) { if ( i < 0 i > n - 1 ) { Console . WriteLine ( " Invalid ▁ Input " ) ; return ; } int diff = 0 ; int oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPrime [ oldValue ] && isPrime [ new_val ] ) return ; if ( ( ! isPrime [ oldValue ] ) && ( ! isPrime [ new_val ] ) ) return ; if ( isPrime [ oldValue ] && ! isPrime [ new_val ] ) { diff = - 1 ; } if ( ! isPrime [ oldValue ] && isPrime [ new_val ] ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void queryPrimes ( int [ ] st , int n , int qs , int qe ) { int primesInRange = queryPrimesUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; Console . WriteLine ( " Number ▁ of ▁ Primes ▁ in ▁ subarray ▁ from ▁ " + qs + " ▁ to ▁ " + qe + " ▁ = ▁ " + primesInRange ) ; } static int constructSTUtil ( int [ ] arr , int ss , int se , int [ ] st , int si , bool [ ] isPrime ) { if ( ss == se ) { if ( isPrime [ arr [ ss ] ] ) st [ si ] = 1 ;...


"Number of quadrilateral formed with N distinct points on circumference of Circle | C # implementation to find the number of quadrilaterals formed with N distinct points ; Function to find the number of combinations in the N ; Function to find the factorial of the given number N ; Loop to find the factorial of the given number ; Driver Code ; Function Call"
Solution in C#:

using System ; class GFG { static int nCr ( int n , int r ) { return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i < n + 1 ; i ++ ) res = res * i ; return res ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . Write ( nCr ( n , 4 ) ) ; } }


"Number of quadrilaterals possible from the given points | C # implementation of above approach ; public class Point points ; determines the orientation of points ; check whether the distinct line segments intersect ; check if points overlap ( similar ) ; it is same , we are returning false because quadrilateral is not possible in this case ; it is not same , So there is a possibility of a quadrilateral ; check for collinearity ; it is collinear , we are returning false because quadrilateral is not possible in this case ; it is not collinear , So there is a possibility of a quadrilateral ; check if any of the points are same ; similar points exist ; check for collinearity ; points are collinear ; Checking for cases where no of quadrilaterals = 1 or 3 ; Driver code ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 5 , 9 ) ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 ) ; A = ( 0 , 9 ) , B = ( 0 , 10 ) , C = ( 0 , 11 ) , D = ( 0 , 12 ) ; A = ( 0 , 9 ) , B = ( 0 , 9 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 )"
Solution in C#:

using System ; class GFG { { public int x ; public int y ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static bool doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; return false ; } static bool similar ( Point p1 , Point p2 ) { if ( p1 . x == p2 . x && p1 . y == p2 . y ) return false ; return true ; } static bool collinear ( Point p1 , Point p2 , Point p3 ) { int x1 = p1 . x , y1 = p1 . y ; int x2 = p2 . x , y2 = p2 . y ; int x3 = p3 . x , y3 = p3 . y ; if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) return false ; else return true ; } static int no_of_quads ( Point p1 , Point p2 , Point p3 , Point p4 ) { bool same = true ; same = same & similar ( p1 , p2 ) ; same = same & similar ( p1 , p3 ) ; same = same & similar ( p1 , p4 ) ; same = same & similar ( p2 , p3 ) ; same = same & similar ( p2 , p4 ) ; same = same & similar ( p3 , p4 ) ; if ( same == false ) return 0 ; bool coll = true ; coll = coll & collinear ( p1 , p2 , p3 ) ; coll = coll & collinear ( p1 , p2 , p4 ) ; coll = coll & collinear ( p1 , p3 , p4 ) ; coll = coll & collinear ( p2 , p3 , p4 ) ; if ( coll == false ) return 0 ; int check = 0 ; if ( doIntersect ( p1 , p2 , p3 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p3 , p2 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p2 , p4 , p3 ) ) check = 1 ; if ( check == 0 ) return 3 ; return 1 ; } static void Main ( ) { Point p1 , p2 , p3 , p4 ; p1 = new Point ( ) ; p2 = new Point ( ) ; p3 = new Point ( ) ; p4 = new Point ( ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 5 ; p4 . y = 9 ; Console . WriteLine ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x =...


"Number of rectangles in N * M grid | C # Code to count number of rectangles in N * M grid ; Driver code"
Solution in C#:

using System ; class GFG { public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } public static void Main ( ) { int n = 5 , m = 4 ; Console . WriteLine ( rectCount ( n , m ) ) ; } }


"Number of rectangles in a circle of radius R | C # program to find the number of rectangles that can be cut from a circle of Radius R ; Function to return the total possible rectangles that can be cut from the circle ; Diameter = 2 * Radius ; Square of diameter which is the square of the maximum length diagonal ; generate all combinations of a and b in the range ( 1 , ( 2 * Radius - 1 ) ) ( Both inclusive ) ; Calculate the Diagonal length of this rectangle ; If this rectangle 's  Diagonal Length is  less than the Diameter,  it is a valid rectangle,  thus increment counter ; Driver Code ; Radius of the circle"
Solution in C#:

using System ; class GFG { static int countRectangles ( int radius ) { int rectangles = 0 ; int diameter = 2 * radius ; int diameterSquare = diameter * diameter ; for ( int a = 1 ; a < 2 * radius ; a ++ ) { for ( int b = 1 ; b < 2 * radius ; b ++ ) { int diagonalLengthSquare = ( a * a + b * b ) ; if ( diagonalLengthSquare <= diameterSquare ) { rectangles ++ ; } } } return rectangles ; } public static void Main ( ) { int radius = 2 ; int totalRectangles ; totalRectangles = countRectangles ( radius ) ; Console . WriteLine ( totalRectangles + " ▁ rectangles ▁ can ▁ be ▁ " + " cut ▁ from ▁ a ▁ circle ▁ of " + " ▁ Radius ▁ " + radius ) ; } }


"Number of relations that are neither Reflexive nor Irreflexive on a Set | C # program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 in O ( log y ) ; Stores the result of ( x ^ y ) ; Update x , if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with res ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of relations that are neither reflexive nor irreflexive ; Return the resultant count ; Driver Code"
Solution in C#:

using System ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static void countRelations ( int N ) { Console . Write ( ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ) ; } public static void Main ( String [ ] args ) { int N = 2 ; countRelations ( N ) ; } }


"Number of segments where all elements are greater than X | C # program to count the number of segments in which all elements are greater than X ; Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code"
Solution in C#:

using System ; public class GFG { static int countSegments ( int [ ] a , int n , int x ) { bool flag = false ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { flag = true ; } else { if ( flag ) count += 1 ; flag = false ; } } if ( flag ) count += 1 ; return count ; } static public void Main ( ) { int [ ] a = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = a . Length ; int x = 13 ; Console . WriteLine ( countSegments ( a , n , x ) ) ; } }


"Number of sequences which has HEAD at alternate positions to the right of the first HEAD | C # program to find number of sequences ; function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code"
Solution in C#:

using System ; public class GFG { static int findAllSequence ( int N ) { if ( N % 2 == 0 ) { return ( int ) ( Math . Pow ( 2 , N / 2 + 1 ) + Math . Pow ( 2 , N / 2 ) - 2 ) ; } else { return ( int ) ( Math . Pow ( 2 , ( N + 1 ) / 2 ) + Math . Pow ( 2 , ( N + 1 ) / 2 ) - 2 ) ; } } public static void Main ( ) { int N = 2 ; Console . WriteLine ( findAllSequence ( N ) ) ; } }


"Number of shortest paths to reach every cell from bottom | C # program to find number of shortest paths ; Function to find number of shortest paths ; Compute the grid starting from the bottom - left corner ; Print the grid ; Driver code ; Function call"
Solution in C#:

 using System ; class GFG { static void NumberOfShortestPaths ( int n , int m ) { int [ , ] a = new int [ n , m ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < m ; j ++ ) { if ( j == 0 i == n - 1 ) a [ i , j ] = 1 ; else a [ i , j ] = a [ i , j - 1 ] + a [ i + 1 , j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . Write ( "
 " ) ; } } public static void Main ( String [ ] args ) { int n = 5 , m = 2 ; NumberOfShortestPaths ( n , m ) ; } }


"Number of siblings of a given Node in n | C # program to find number of siblings of a given node ; Represents a node of an n - ary tree ; Function to calculate number of siblings of a given node ; Creating a queue and pushing the root ; Dequeue an item from queue and check if it is equal to x If YES , then return number of children ; Enqueue all children of the dequeued item ; If the value of children is equal to x , then return the number of siblings ; Driver code ; Creating a generic tree as shown in above figure ; Node whose number of siblings is to be calculated ; Function calling"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public List < Node > child ; public Node ( int data ) { key = data ; child = new List < Node > ( ) ; } } ; static int numberOfSiblings ( Node root , int x ) { if ( root == null ) return 0 ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count > 0 ) { Node p = q . Peek ( ) ; q . Dequeue ( ) ; for ( int i = 0 ; i < p . child . Count ; i ++ ) { if ( p . child [ i ] . key == x ) return p . child . Count - 1 ; q . Enqueue ( p . child [ i ] ) ; } } return - 1 ; } public static void Main ( String [ ] args ) { Node root = new Node ( 50 ) ; ( root . child ) . Add ( new Node ( 2 ) ) ; ( root . child ) . Add ( new Node ( 30 ) ) ; ( root . child ) . Add ( new Node ( 14 ) ) ; ( root . child ) . Add ( new Node ( 60 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 15 ) ) ; ( root . child [ 0 ] . child ) . Add ( new Node ( 25 ) ) ; ( root . child [ 0 ] . child [ 1 ] . child ) . Add ( new Node ( 70 ) ) ; ( root . child [ 0 ] . child [ 1 ] . child ) . Add ( new Node ( 100 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 6 ) ) ; ( root . child [ 1 ] . child ) . Add ( new Node ( 1 ) ) ; ( root . child [ 2 ] . child ) . Add ( new Node ( 7 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 17 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 99 ) ) ; ( root . child [ 2 ] . child [ 0 ] . child ) . Add ( new Node ( 27 ) ) ; ( root . child [ 3 ] . child ) . Add ( new Node ( 16 ) ) ; int x = 100 ; Console . WriteLine ( numberOfSiblings ( root , x ) ) ; } }


"Number of sink nodes in a graph | C # program to count number if sink nodes ; Return the number of Sink NOdes . ; Array for marking the non - sink node . ; Marking the non - sink node . ; Counting the sink nodes . ; Driver Code"
Solution in C#:

using System ; class GFG { static int countSink ( int n , int m , int [ ] edgeFrom , int [ ] edgeTo ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; } public static void Main ( String [ ] args ) { int n = 4 , m = 2 ; int [ ] edgeFrom = { 2 , 4 } ; int [ ] edgeTo = { 3 , 3 } ; Console . WriteLine ( countSink ( n , m , edgeFrom , edgeTo ) ) ; } }


"Number of smaller circles that can be inscribed in a larger circle | C # program for the above approach ; Function to count number of smaller circles that can be inscribed in the larger circle touching its boundary ; If R2 is greater than R1 ; Stores the angle made by the smaller circle ; Stores the ratio of R2 / ( R1 - R2 ) ; Stores the count of smaller circles that can be inscribed ; Stores the ratio ; If the diameter of smaller circle is greater than the radius of the larger circle ; Otherwise ; Find the angle using formula ; Divide 360 with angle and take the floor value ; Return the final result ; Driver Code"
Solution in C#:

using System ; class GFG { static int countInscribed ( int R1 , int R2 ) { if ( R2 > R1 ) return 0 ; double angle ; double ratio ; int number_of_circles = 0 ; ratio = R2 / ( double ) ( R1 - R2 ) ; if ( R1 < 2 * R2 ) { number_of_circles = 1 ; } else { angle = Math . Abs ( Math . Asin ( ratio ) * 180 ) / 3.14159265 ; number_of_circles = ( int ) ( 360 / ( 2 * Math . Floor ( angle ) ) ) ; } return number_of_circles ; } public static void Main ( ) { int R1 = 3 ; int R2 = 1 ; Console . WriteLine ( countInscribed ( R1 , R2 ) ) ; } }


"Number of solutions for the equation x + y + z <= n | C # program to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; Driver code"
Solution in C#:

using System ; public class GFG { static int NumberOfSolutions ( int x , int y , int z , int n ) { int ans = 0 ; for ( int i = 0 ; i <= x ; i ++ ) { for ( int j = 0 ; j <= y ; j ++ ) { int temp = n - i - j ; if ( temp >= 0 ) { temp = Math . Min ( temp , z ) ; ans += temp + 1 ; } } } return ans ; } static public void Main ( ) { int x = 1 , y = 2 , z = 3 , n = 4 ; Console . WriteLine ( NumberOfSolutions ( x , y , z , n ) ) ; } }


"Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | C # implementation of above approach ; function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call"
Solution in C#:

using System ; class GFG { static int NumberOfSolutions ( int a , int b , int c , int d ) { int ans = 0 ; for ( int i = a ; i <= b ; i ++ ) if ( d >= Math . Max ( c , i + 1 ) ) ans += d - Math . Max ( c , i + 1 ) + 1 ; return ans ; } public static void Main ( ) { int a = 2 , b = 3 , c = 3 , d = 4 ; Console . WriteLine ( NumberOfSolutions ( a , b , c , d ) ) ; } }


"Number of solutions of n = x + n âŠ • x | C # implementation of above approach ; Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code"
Solution in C#:

using System ; class GFG { static int numberOfSolutions ( int n ) { int c = 0 ; for ( int x = 0 ; x <= n ; ++ x ) if ( n == x + ( n ^ x ) ) ++ c ; return c ; } public static void Main ( ) { int n = 3 ; Console . Write ( numberOfSolutions ( n ) ) ; } }


"Number of solutions to Modular Equations | C # Program to find number of possible values of X to satisfy A mod X = B ; Returns the number of divisors of ( A - B ) greater than B ; if N is divisible by i ; count only the divisors greater than B ; checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; if A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; if A < B , there are no possible values of X satisfying the equation ; the last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; if infinitely many solutions available ; Driver code"
Solution in C#:

 using System ; class GFG { static int calculateDivisors ( int A , int B ) { int N = ( A - B ) ; int noOfDivisors = 0 ; double a = Math . Sqrt ( N ) ; for ( int i = 1 ; i <= ( int ) ( a ) ; i ++ ) { if ( ( N % i ) == 0 ) { if ( i > B ) noOfDivisors ++ ; if ( ( N / i ) != i && ( N / i ) > B ) noOfDivisors ++ ; } } return noOfDivisors ; } static int numberOfPossibleWaysUtil ( int A , int B ) { if ( A == B ) return - 1 ; if ( A < B ) return 0 ; int noOfDivisors = 0 ; noOfDivisors = calculateDivisors ( A , B ) ; return noOfDivisors ; } public static void numberOfPossibleWays ( int A , int B ) { int noOfSolutions = numberOfPossibleWaysUtil ( A , B ) ; if ( noOfSolutions == - 1 ) { Console . Write ( " For ▁ A ▁ = ▁ " + A + " ▁ and ▁ B ▁ = ▁ " + B + " , ▁ X ▁ can ▁ take ▁ Infinitely ▁ many ▁ values " + " ▁ greater ▁ than ▁ " + A + "
 " ) ; } else { Console . Write ( " For ▁ A ▁ = ▁ " + A + " ▁ and ▁ B ▁ = ▁ " + B + " , ▁ X ▁ can ▁ take ▁ " + noOfSolutions + " ▁ values
 " ) ; } } public static void Main ( ) { int A = 26 , B = 2 ; numberOfPossibleWays ( A , B ) ; A = 21 ; B = 5 ; numberOfPossibleWays ( A , B ) ; } }


"Number of square matrices with all 1 s | C # program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code"
Solution in C#:

using System ; class GFG { static int n = 3 ; static int m = 3 ; static int countSquareMatrices ( int [ , ] a , int N , int M ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i , j ] == 0 ) continue ; a [ i , j ] = Math . Min ( Math . Min ( a [ i - 1 , j ] , a [ i , j - 1 ] ) , a [ i - 1 , j - 1 ] ) + 1 ; } } for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) count += a [ i , j ] ; return count ; } public static void Main ( ) { int [ , ] arr = { { 1 , 0 , 1 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } } ; Console . WriteLine ( countSquareMatrices ( arr , n , m ) ) ; } }


"Number of squares of side length required to cover an N * M rectangle | CSHARP program to find number of squares of a * a required to cover n * m rectangle ; function to find a number of squares of a * a required to cover n * m rectangle ; Driver code ; function call"
Solution in C#:

using System ; class GFG { static int Squares ( int n , int m , int a ) { return ( ( m + a - 1 ) / a ) * ( ( n + a - 1 ) / a ) ; } static void Main ( ) { int n = 6 , m = 6 , a = 4 ; Console . WriteLine ( Squares ( n , m , a ) ) ; } }


"Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | C # program to find the number of steps required to reach ( x , y ) from ( 0 , 0 ) following a zig - zag path ; Function to return the required position ; Driver Code"
Solution in C#:

using System ; class GfG { static int countSteps ( int x , int y ) { if ( x < y ) { return x + y + 2 * ( ( y - x ) / 2 ) ; } else { return x + y + 2 * ( ( ( x - y ) + 1 ) / 2 ) ; } } public static void Main ( ) { int x = 4 , y = 3 ; Console . WriteLine ( countSteps ( x , y ) ) ; } }


"Number of steps to convert to prime factors | C # program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; Driver code ; call sieve to calculate the factors"
Solution in C#:

using System ; class GFG { static int MAX = 1000001 ; static int [ ] factor = new int [ MAX ] ; static void cal_factor ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } static int no_of_representations ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; int flag = 0 ; while ( factor [ temp ] != 1 ) { flag = - 1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } public static void Main ( ) { cal_factor ( ) ; int [ ] a = { 4 , 4 , 4 } ; int n = a . Length ; Console . WriteLine ( no_of_representations ( a , n ) ) ; } }


"Number of strings of length N with no palindromic sub string | C # program to count number of strings of size m such that no substring is palindrome . ; Return the count of strings with no palindromic substring . ; Driver Code"
Solution in C#:

using System ; class GFG { static int numofstring ( int n , int m ) { if ( n == 1 ) return m ; if ( n == 2 ) return m * ( m - 1 ) ; return m * ( m - 1 ) * ( int ) Math . Pow ( m - 2 , n - 2 ) ; } public static void Main ( ) { int n = 2 , m = 3 ; Console . Write ( numofstring ( n , m ) ) ; } }


"Number of strings that satisfy the given condition | C # implementation of the approach ; Function to return the count of valid strings ; Set to store indices of valid strings ; Find the maximum digit for current position ; Add indices of all the strings in the set that contain maximal digit ; Return number of strings in the set ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GfG { static int countStrings ( int n , int m , String [ ] s ) { HashSet < int > ind = new HashSet < int > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . Max ( mx , ( int ) ( s [ i ] [ j ] - '0' ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] [ j ] - '0' == mx ) ind . Add ( i ) ; } return ind . Count ; } public static void Main ( ) { String [ ] s = { "223" , "232" , "112" } ; int m = s [ 0 ] . Length ; int n = s . Length ; Console . WriteLine ( countStrings ( n , m , s ) ) ; } }


"Number of strings which starts and ends with same character after rotations | C # program for the above approach ; Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Driver Code ; Given string ; Function call"
Solution in C#:

using System ; class GFG { static int countStrings ( String s ) { int cnt = 0 ; for ( int i = 1 ; i < s . Length - 1 ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { cnt ++ ; } } return cnt ; } public static void Main ( String [ ] args ) { String str = " aacbb " ; Console . WriteLine ( countStrings ( str ) ) ; } }


"Number of sub arrays with negative product | C # implementation of the approach ; Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Driver code"
Solution in C#:

using System ; class GFG { static int negProdSubArr ( int [ ] arr , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } static public void Main ( ) { int [ ] arr = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . Length ; Console . Write ( negProdSubArr ( arr , n ) ) ; } }


"Number of sub arrays with odd sum | C # code to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by an even - odd pair ; Driver code"
Solution in C#:

using System ; class GFG { static int countOddSum ( int [ ] ar , int n ) { int [ ] temp = { 1 , 0 } ; int result = 0 , val = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { val = ( ( val + ar [ i ] ) % 2 + 2 ) % 2 ; temp [ val ] ++ ; } result = temp [ 0 ] * temp [ 1 ] ; return ( result ) ; } public static void Main ( ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ odd ▁ sum ▁ is ▁ " + countOddSum ( ar , n ) ) ; } }


"Number of sub arrays with odd sum | C # code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code"
Solution in C#:

using System ; class GFG { static int countOddSum ( int [ ] ar , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int val = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { val = val + ar [ j ] ; if ( val % 2 != 0 ) result ++ ; } } return ( result ) ; } public static void Main ( ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ odd ▁ sum ▁ is ▁ " ) ; Console . WriteLine ( countOddSum ( ar , n ) ) ; } }


"Number of sub arrays with odd sum | C # program to find number of subarrays with odd sum ; Function to find number of subarrays with odd sum ; ' odd ' stores number of odd numbers upto ith index ' c _ odd ' stores number of odd sum subarrays starting at ith index ' Result ' stores the number of odd sum subarrays ; First find number of odd sum subarrays starting at 0 th index ; Find number of odd sum subarrays starting at ith index add to result ; Driver code"
Solution in C#:

using System ; public class GFG { static int countOddSum ( int [ ] a , int n ) { int c_odd = 0 , result = 0 ; bool odd = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd = ! odd ; } if ( odd ) { c_odd ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 == 1 ) { c_odd = ( n - i - c_odd ) ; } } return result ; } public static void Main ( String [ ] args ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays ▁ " + " with ▁ odd ▁ sum ▁ is ▁ " + countOddSum ( ar , n ) ) ; } }


"Number of sub | C # implementation of above approach ; Function to return the number of subsequences which have at least one consecutive pair with difference less than or equal to 1 ; Not required sub - sequences which turn required on adding i ; Required sub - sequence till now will be required sequence plus sub - sequence which turns required ; Similarly for not required ; Also updating total required and not required sub - sequences ; Also , storing values in dp ; Driver code"
Solution in C#:

using System ; public class GFG { static int N = 10000 ; static int count_required_sequence ( int n , int [ ] arr ) { int total_required_subsequence = 0 ; int total_n_required_subsequence = 0 ; int [ , ] dp = new int [ N , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { int turn_required = 0 ; for ( int j = - 1 ; j <= 1 ; j ++ ) turn_required += dp [ arr [ i ] + j , 0 ] ; int required_end_i = ( total_required_subsequence + turn_required ) ; int n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ; total_required_subsequence += required_end_i ; total_n_required_subsequence += n_required_end_i ; dp [ arr [ i ] , 1 ] += required_end_i ; dp [ arr [ i ] , 0 ] += n_required_end_i ; } return total_required_subsequence ; } static public void Main ( ) { int [ ] arr = { 1 , 6 , 2 , 1 , 9 } ; int n = arr . Length ; Console . WriteLine ( count_required_sequence ( n , arr ) ) ; } }


"Number of sub | C # implementation of th approach ; Function to return the number of sub - sequences divisible by 3 ; Base - cases ; If the state has been solved before then return its value ; Marking the state as solved ; Recurrence relation ; Driver code"
Solution in C#:

using System ; class GFG { static readonly int N = 100 ; static int [ , ] dp = new int [ N , 3 ] ; static int [ , ] v = new int [ N , 3 ] ; static int findCnt ( String s , int i , int r ) { if ( i == s . Length ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i , r ] == 1 ) return dp [ i , r ] ; v [ i , r ] = 1 ; dp [ i , r ] = findCnt ( s , i + 1 , ( r * 2 + ( s [ i ] - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i , r ] ; } public static void Main ( String [ ] args ) { String s = "11" ; Console . Write ( findCnt ( s , 0 , 0 ) - 1 ) ; } }


"Number of sub | C # implementation of the approach ; Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubString ( char [ ] s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . Length ; while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s [ right ] == c ) freq ++ ; right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s [ right ] != c ) { if ( s [ right ] == c ) freq ++ ; right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } public static void Main ( String [ ] args ) { String s = " abada " ; char c = ' a ' ; int k = 2 ; Console . WriteLine ( countSubString ( s . ToCharArray ( ) , c , k ) ) ; } }


"Number of sub | C # implementation of the approach ; Function to return the count of the required substrings ; To store the final answer ; Loop to find the answer ; Condition to update the answer ; Driver code"
Solution in C#:

using System ; class GFG { static int countSubStr ( string str , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) ans += ( i + 1 ) ; } return ans ; } public static void Main ( ) { string str = "10010" ; int len = str . Length ; Console . WriteLine ( countSubStr ( str , len ) ) ; } }


"Number of sub | C # program to find the number of sub - Strings of s1 which are anagram of any sub - String of s2 ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of String pat [ ] in String txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; cout << pat << " ▁ " << txt << " ▁ " ; ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Function to return the number of sub - Strings of s1 that are anagrams of any sub - String of s2 ; initializing variables ; outer loop for picking starting point ; loop for different length of subStrings ; If s2 has any subString which is anagram of s1 . substr ( i , len ) ; increment the count ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX_LEN = 1005 ; static int MAX_CHAR = 26 ; static int ALL_CHARS = 256 ; static bool compare ( char [ ] arr1 , char [ ] arr2 ) { for ( int i = 0 ; i < ALL_CHARS ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static bool search ( String pat , String txt ) { int M = pat . Length ; int N = txt . Length ; int i ; char [ ] countP = new char [ ALL_CHARS ] ; char [ ] countTW = new char [ ALL_CHARS ] ; for ( i = 0 ; i < M ; i ++ ) { ( countP [ pat [ i ] ] ) ++ ; ( countTW [ txt [ i ] ] ) ++ ; } for ( i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) { return true ; } ( countTW [ txt [ i ] ] ) ++ ; countTW [ txt [ i - M ] ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } static int calculatesubString ( String s1 , String s2 , int n ) { int count = 0 , j = 0 , x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { if ( search ( s1 . Substring ( i , len ) , s2 ) ) { count = count + 1 ; } } } return count ; } public static void Main ( String [ ] args ) { String str1 = " PLEASEHELPIMTRAPPED " ; String str2 = " INAKICKSTARTFACTORY " ; int len = str1 . Length ; Console . WriteLine ( calculatesubString ( str1 , str2 , len ) ) ; } }


"Number of subarrays consisting only of Pronic Numbers | C # program for the approach ; Function to check if a number is pronic number or not ; Iterate over the range [ 1 , sqrt ( N ) ] ; Return true if N is pronic ; Otherwise , return false ; Function to count the number of subarrays consisting of pronic numbers ; Stores the count of subarrays ; Stores the number of consecutive array elements which are pronic ; Traverse the array ; If i is pronic ; Return the total count ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPronic ( int n ) { int range = ( int ) Math . Sqrt ( n ) ; for ( int i = 0 ; i < range + 1 ; i ++ ) { if ( i * ( i + 1 ) == n ) return true ; } return false ; } static int countSub ( int [ ] arr , int n ) { int ans = 0 ; int ispro = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPronic ( arr [ i ] ) ) ispro += 1 ; else ispro = 0 ; ans += ispro ; } return ans ; } static void Main ( ) { int [ ] arr = { 5 , 6 , 12 , 3 , 4 } ; int n = arr . Length ; Console . WriteLine ( countSub ( arr , n ) ) ; } }