"Find the position of the given row in a 2 | C # implementation of the approach ; Function that compares both the arrays and returns - 1 , 0 and 1 accordingly ; Return 1 if mid row is less than arr [ ] ; Return 1 if mid row is greater than arr [ ] ; Both the arrays are equal ; Function to find a row in the given matrix using binary search ; If current row is equal to the given array then return the row number ; If arr [ ] is greater , ignore left half ; If arr [ ] is smaller , ignore right half ; No valid row found ; Driver code"
Solution in C#:

using System ; class GFG { static int m = 6 , n = 4 ; static int compareRow ( int [ ] a1 , int [ ] a2 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return - 1 ; } return 0 ; } static int binaryCheck ( int [ , ] ar , int [ ] arr ) { int l = 0 , r = m - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int temp = compareRow ( GetRow ( ar , mid ) , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } public static int [ ] GetRow ( int [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new int [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int [ ] row = { 10 , 9 , 22 , 23 } ; Console . WriteLine ( binaryCheck ( mat , row ) ) ; } }


"Find the position of the given row in a 2 | C # implementation of the approach ; Function to find a row in the given matrix using linear search ; Assume that the current row matched with the given array ; If any element of the current row doesn 't  match with the corresponding element  of the given array ; Set matched to false and break ; ; If matched then return the row number ; No row matched with the given array ; Driver code"
Solution in C#:

using System ; class GFG { static int m = 6 ; static int n = 4 ; static int linearCheck ( int [ , ] ar , int [ ] arr ) { for ( int i = 0 ; i < m ; i ++ ) { bool matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i , j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } static public void Main ( ) { int [ , ] mat = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int [ ] row = { 10 , 9 , 22 , 23 } ; Console . Write ( linearCheck ( mat , row ) ) ; } }


"Find the position of the last removed element from the array | C # program to find the position of the last removed element from the array ; Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code"
Solution in C#:

using System ; class GFG { static int getPosition ( int [ ] a , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] / m + ( a [ i ] % m ) ) ; } int ans = - 1 , max = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( max < a [ i ] ) { max = a [ i ] ; ans = i ; } } return ans + 1 ; } static public void Main ( ) { int [ ] a = { 2 , 5 , 4 } ; int n = a . Length ; int m = 2 ; Console . WriteLine ( getPosition ( a , n , m ) ) ; } }


"Find the possible permutation of the bits of N | C # implementation of the above approach ; Function that returns true if it is possible to arrange the bits of n in alternate fashion ; To store the count of 1 s in the binary representation of n ; If the number set bits and the number of unset bits is equal ; Driver code"
Solution in C#:

using System ; class GFG { static int TOTAL_BITS = 32 ; static int CountBits ( int value ) { int count = 0 ; while ( value != 0 ) { count ++ ; value &= value - 1 ; } return count ; } static bool isPossible ( int n ) { int cnt = CountBits ( n ) ; if ( cnt == TOTAL_BITS / 2 ) return true ; return false ; } public static void Main ( String [ ] arr ) { int n = 524280 ; if ( isPossible ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }


"Find the previous fibonacci number | C # implementation of the approach ; Function to return the previous fibonacci number ; Driver code"
Solution in C#:

using System ; class GFG { static int previousFibonacci ( int n ) { double a = n / ( ( 1 + Math . Sqrt ( 5 ) ) / 2.0 ) ; return ( int ) Math . Round ( a ) ; } public static void Main ( ) { int n = 8 ; Console . Write ( previousFibonacci ( n ) ) ; } }


"Find the prime P using given four integers | C # program to possible prime number ; Function to check if a number is prime or not ; Function to find possible prime number ; Find a possible prime number ; Last condition ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static bool Prime ( int n ) { for ( int j = 2 ; j <= Math . Sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } static int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= Math . Max ( Math . Sqrt ( n ) , Math . Sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; return int . MinValue ; } public static void Main ( ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; Console . WriteLine ( find_prime ( x , xsqmodp , y , ysqmodp ) ) ; } }


"Find the prime numbers which can written as sum of most consecutive primes | C # program to find longest sum of consecutive primes ; Store prime number in vector ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half . This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function find the prime number which can be written as the sum of the most consecutive primes ; To store maximum length of consecutive primes that can sum to a limit ; The prime number ( or result ) that can be represented as sum of maximum number of primes . ; Consider all lengths of consecutive primes below limit . ; If we cross the limit , then break the loop ; sum_prime [ i ] - sum_prime [ j ] is prime number or not ; Check if sum of current length of consecutives is prime or not . ; Update the length and prime number ; Returns the prime number that can written as sum of longest chain of consecutive primes . ; Calculate sum of prime numbers and store them in sum_prime array . sum_prime [ i ] stores sum of prime numbers from primes [ 0 ] to primes [ i - 1 ] ; Process all queries one by one ; Driver code"
Solution in C#:

using System ; using System . Collections ; class GFG { static int MAX = 10000 ; static ArrayList primes = new ArrayList ( ) ; static void sieveSundaram ( ) { bool [ ] marked = new bool [ MAX / 2 + 1 ] ; Array . Fill ( marked , false ) ; for ( int i = 1 ; i <= ( Math . Sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . Add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . Add ( 2 * i + 1 ) ; } static int LSCPUtil ( int limit , long [ ] sum_prime ) { int max_length = - 1 ; int prime_number = - 1 ; for ( int i = 0 ; ( int ) primes [ i ] <= limit ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( sum_prime [ i ] - sum_prime [ j ] > limit ) break ; long consSum = sum_prime [ i ] - sum_prime [ j ] ; int [ ] prime = ( int [ ] ) primes . ToArray ( typeof ( int ) ) ; if ( Array . BinarySearch ( prime , ( int ) consSum ) >= 0 ) { if ( max_length < i - j + 1 ) { max_length = i - j + 1 ; prime_number = ( int ) consSum ; } } } } return prime_number ; } static void LSCP ( int [ ] arr , int n ) { sieveSundaram ( ) ; long [ ] sum_prime = new long [ primes . Count + 1 ] ; sum_prime [ 0 ] = 0 ; for ( int i = 1 ; i <= primes . Count ; i ++ ) sum_prime [ i ] = ( int ) primes [ i - 1 ] + sum_prime [ i - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( LSCPUtil ( arr [ i ] , sum_prime ) + " ▁ " ) ; } public static void Main ( string [ ] arg ) { int [ ] arr = { 10 , 30 , 40 , 50 , 1000 } ; int n = arr . Length ; LSCP ( arr , n ) ; } }


"Find the probability of reaching all points after N moves from point N | C # program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Calling function to print the array with probabilities ; Function that prints the array ; Driver Code"
Solution in C#:

using System ; class GFG { static void printProbabilities ( int n , double left ) { double right = 1 - left ; double [ , ] arr = new double [ n + 1 , 2 * n + 1 ] ; arr [ 1 , n + 1 ] = right ; arr [ 1 , n - 1 ] = left ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= 2 * n ; j ++ ) { arr [ i , j ] += ( arr [ i - 1 , j - 1 ] * right ) ; } for ( int j = 2 * n - 1 ; j >= 0 ; j -- ) { arr [ i , j ] += ( arr [ i - 1 , j + 1 ] * left ) ; } } printArray ( arr , n ) ; } static void printArray ( double [ , ] arr , int n ) { for ( int i = 0 ; i < GetRow ( arr , 0 ) . GetLength ( 0 ) ; i ++ ) { Console . Write ( " { 0 : F4 } ▁ " , arr [ n , i ] ) ; } } public static double [ ] GetRow ( double [ , ] matrix , int row ) { var rowLength = matrix . GetLength ( 1 ) ; var rowVector = new double [ rowLength ] ; for ( var i = 0 ; i < rowLength ; i ++ ) rowVector [ i ] = matrix [ row , i ] ; return rowVector ; } public static void Main ( String [ ] args ) { int n = 2 ; double left = 0.5 ; printProbabilities ( n , left ) ; } }


"Find the product of sum of two diagonals of a square Matrix | C # program to find the product of the sum of diagonals . ; Function to find the product of the sum of diagonals . ; Initialize sums of diagonals ; Return the answer ; Driven code ; Function call"
Solution in C#:

using System ; class GFG { static long product ( int [ , ] mat , int n ) { long d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += mat [ i , i ] ; d2 += mat [ i , n - i - 1 ] ; } return 1L * d1 * d2 ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 5 , 8 , 1 } , { 5 , 10 , 3 } , { - 6 , 17 , - 9 } } ; int n = mat . GetLength ( 0 ) ; Console . Write ( product ( mat , n ) ) ; } }


"Find the quadratic equation from the given roots | C # implementation of the above approach ; Function to find the quadratic equation whose roots are a and b ; Driver code"
Solution in C#:

using System ; class GFG { static void findEquation ( int a , int b ) { int sum = ( a + b ) ; int product = ( a * b ) ; Console . WriteLine ( " x ^ 2 ▁ - ▁ ( " + sum + " x ) ▁ + ▁ ( " + product + " ) ▁ = ▁ 0" ) ; } public static void Main ( ) { int a = 2 , b = 3 ; findEquation ( a , b ) ; } }


"Find the radii of the circles which are lined in a row , and distance between the centers of first and last circle is given | C # program to find radii of the circles which are lined in a row and distance between the centers of first and last circle is given ; Driver code"
Solution in C#:

using System ; class GFG { static void radius ( int n , int d ) { Console . Write ( " The ▁ radius ▁ of ▁ each ▁ circle ▁ is ▁ " + d / ( 2 * n - 2 ) ) ; } static public void Main ( ) { int d = 42 , n = 4 ; radius ( n , d ) ; } }


"Find the ratio of LCM to GCD of a given Array | C # Program to implement above approach ; Function to calculate and return GCD of the given array ; Initialise GCD ; Once GCD is 1 , it will always be 1 with all other elements ; Return GCD ; Function to calculate and return LCM of the given array ; Initialise LCM ; LCM of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; Return LCM ; Function to print the ratio of LCM to GCD of the given array ; Driver Code"
Solution in C#:

using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findGCD ( int [ ] arr , int n ) { int gcd = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( arr [ i ] , gcd ) ; if ( gcd == 1 ) { return 1 ; } } return gcd ; } static int findLCM ( int [ ] arr , int n ) { int lcm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { lcm = ( ( ( arr [ i ] * lcm ) ) / ( __gcd ( arr [ i ] , lcm ) ) ) ; } return lcm ; } static void findRatio ( int [ ] arr , int n ) { int gcd = findGCD ( arr , n ) ; int lcm = findLCM ( arr , n ) ; Console . Write ( ( lcm / gcd ) ) ; Console . Write ( " : 1" ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 6 , 12 , 36 } ; int N = 3 ; findRatio ( arr , N ) ; } }


"Find the ratio of number of elements in two Arrays from their individual and combined average | C # program to Find the Ratio of number of Elements in two Arrays from their individual and combined Average ; function to find the ratio of number of array elements ; calculating GCD of them ; make neumarator and denominator coprime ; Driver Code"
Solution in C#:

using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void FindRatio ( int a , int b , int c ) { int up = Math . Abs ( b - c ) ; int down = Math . Abs ( c - a ) ; int g = gcd ( up , down ) ; up /= g ; down /= g ; Console . WriteLine ( up + " : " + down ) ; } public static void Main ( String [ ] args ) { int a = 4 , b = 10 , c = 6 ; FindRatio ( a , b , c ) ; } }


"Find the remainder when First digit of a number is divided by its Last digit | C # program to find the remainder when the First digit of a number is divided by its Last digit ; Function to find the remainder ; Get the last digit ; Get the first digit ; Compute the remainder ; Driver code"
Solution in C#:

using System ; class GFG { static void findRemainder ( int n ) { int l = n % 10 ; while ( n >= 10 ) n /= 10 ; int f = n ; int remainder = f % l ; Console . WriteLine ( remainder ) ; } public static void Main ( ) { int n = 5223 ; findRemainder ( n ) ; } }


"Find the remainder when N is divided by 4 using Bitwise AND operator | C # implementation to find N modulo 4 using Bitwise AND operator ; Driver code ; Function to find the remainder ; Bitwise AND with 3 ; return x"
Solution in C#:

using System ; class GFG { public static void Main ( ) { int N = 43 ; int ans = findRemainder ( N ) ; Console . Write ( ans ) ; } public static int findRemainder ( int n ) { int x = n & 3 ; return x ; } } is by


"Find the remaining balance after the transaction | C # program to find the remaining balance ; Function to find the balance ; Check if the transaction can be successful or not ; Transaction is successful ; Transaction is unsuccessful ; Driver Code"
Solution in C#:

 using System ; class GFG { static void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { Console . Write ( " { 0 : F2 }
 " , bal - x - 1.50 ) ; } else { Console . Write ( " { 0 : F2 }
 " , bal ) ; } } public static void Main ( String [ ] args ) { int x = 50 ; float bal = ( float ) 100.50 ; findBalance ( x , bal ) ; } }


"Find the remaining vertices of a square from two given vertices | C # program for the above approach ; Function to find the remaining vertices of a square ; Check if the x - coordinates are equal ; Check if the y - coordinates are equal ; If the the given coordinates forms a diagonal of the square ; Otherwise ; Square does not exist ; Driver Code ; Given two vertices"
Solution in C#:

 using System ; class GFG { static void findVertices ( int x1 , int y1 , int x2 , int y2 ) { if ( x1 == x2 ) { Console . Write ( ( x1 + y2 - y1 ) + " , ▁ " + y1 + "
 " ) ; Console . Write ( ( x2 + y2 - y1 ) + " , ▁ " + y2 ) ; } else if ( y1 == y2 ) { Console . Write ( x1 + " , ▁ " + ( y1 + x2 - x1 ) + "
 " ) ; Console . Write ( x2 + " , ▁ " + ( y2 + x2 - x1 ) ) ; } else if ( Math . Abs ( x2 - x1 ) == Math . Abs ( y2 - y1 ) ) { Console . Write ( x1 + " , ▁ " + y2 + "
 " ) ; Console . Write ( x2 + " , ▁ " + y1 ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int x1 = 1 , y1 = 2 ; int x2 = 3 , y2 = 4 ; findVertices ( x1 , y1 , x2 , y2 ) ; } }


"Find the repeating and the missing | Added 3 new methods | C # program to Find the repeating and missing elements ; Driver program"
Solution in C#:

using System ; class GFG { static void printTwoElements ( int [ ] arr , int size ) { int i ; Console . Write ( " The ▁ repeating ▁ element ▁ is ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { int abs_val = Math . Abs ( arr [ i ] ) ; if ( arr [ abs_val - 1 ] > 0 ) arr [ abs_val - 1 ] = - arr [ abs_val - 1 ] ; else Console . WriteLine ( abs_val ) ; } Console . Write ( " And ▁ the ▁ missing ▁ element ▁ is ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] > 0 ) Console . WriteLine ( i + 1 ) ; } } public static void Main ( ) { int [ ] arr = { 7 , 3 , 4 , 5 , 5 , 6 , 2 } ; int n = arr . Length ; printTwoElements ( arr , n ) ; } }


"Find the repeating and the missing | Added 3 new methods | C # program to Find the repeating and missing elements ; The output of this function is stored at * x and * y ; Will hold xor of all elements and numbers from 1 to n ; Will have only single set bit of xor1 ; Get the xor of all array elements ; XOR the previous result with numbers from 1 to n ; Get the rightmost set bit in set_bit_no ; Now divide elements in two sets by comparing rightmost set bit of xor1 with bit at same position in each element . Also , get XORs of two sets . The two XORs are the output elements . The following two for loops serve the purpose ; arr [ i ] belongs to first set ; arr [ i ] belongs to second set ; i belongs to first set ; i belongs to second set ; * x and * y hold the desired output elements ; Driver program"
Solution in C#:

using System ; class GFG { static int x , y ; static void getTwoElements ( int [ ] arr , int n ) { int xor1 ; int set_bit_no ; int i ; x = 0 ; y = 0 ; xor1 = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) xor1 = xor1 ^ arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor1 = xor1 ^ i ; set_bit_no = xor1 & ~ ( xor1 - 1 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { if ( ( i & set_bit_no ) != 0 ) x = x ^ i ; else y = y ^ i ; } } public static void Main ( ) { int [ ] arr = { 1 , 3 , 4 , 5 , 1 , 6 , 2 } ; int n = arr . Length ; getTwoElements ( arr , n ) ; Console . Write ( " ▁ The ▁ missing ▁ element ▁ is ▁ " + x + " and ▁ the ▁ " + " repeating ▁ number ▁ is ▁ " + y ) ; } }


"Find the repeating and the missing | Added 3 new methods | C # program to find the repeating and missing elements using Maps"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 6 , 2 , 1 , 1 } ; Dictionary < int , Boolean > numberMap = new Dictionary < int , Boolean > ( ) ; int max = arr . Length ; foreach ( int i in arr ) { if ( ! numberMap . ContainsKey ( i ) ) { numberMap . Add ( i , true ) ; } else { Console . WriteLine ( " Repeating ▁ = ▁ " + i ) ; } } for ( int i = 1 ; i <= max ; i ++ ) { if ( ! numberMap . ContainsKey ( i ) ) { Console . WriteLine ( " Missing ▁ = ▁ " + i ) ; } } } }


"Find the repeating element in an Array of size N consisting of first M natural numbers | C ++ program for the above approach ; Function to calculate the repeating character in a given permutation ; variables to store maximum element and sum of the array respectively . ; calculate sum of array ; calculate maximum element in the array ; calculating sum of permutation ; calculate required answer ; Driver code ; Input ; Function call"
Solution in C#:

using System ; public class GFG { public static int repeatingElement ( int [ ] arr , int N ) { int M = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; M = Math . Max ( M , arr [ i ] ) ; } int sum1 = M * ( M + 1 ) / 2 ; int ans = ( sum - sum1 ) / ( N - M ) ; return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 6 , 4 , 3 , 1 , 5 , 2 } ; int N = 7 ; Console . WriteLine ( repeatingElement ( arr , N ) ) ; } }


"Find the resultant String after replacing X with Y and removing Z | C # program to find the resultant String after replacing X with Y and removing Z ; Function to replace and remove ; Two pointer start and end points to beginning and end position in the string ; If start is having Z find X pos in end and replace Z with another character ; Find location for having different character instead of Z ; If found swap character at start and end ; Else increment start Also checkin for X at start position ; Driver code"
Solution in C#:

using System ; class GFG { static String replaceRemove ( char [ ] s , char X , char Y , char Z ) { int start = 0 , end = s . Length - 1 ; while ( start <= end ) { if ( s [ start ] == Z ) { while ( end >= 0 && s [ end ] == Z ) { end -- ; } char temp ; if ( end > start ) { temp = s [ start ] ; s [ start ] = s [ end ] ; s [ end ] = temp ; if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } else { if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } String new_s = new String ( s ) ; while ( new_s . Length > 0 && new_s [ new_s . Length - 1 ] == Z ) { new_s = new_s . Substring ( 0 , new_s . Length - 1 ) ; } return new_s ; } public static void Main ( String [ ] args ) { String str = " batman " ; char X = ' a ' , Y = ' d ' , Z = ' b ' ; str = replaceRemove ( str . ToCharArray ( ) , X , Y , Z ) ; if ( str . Length == 0 ) { Console . WriteLine ( - 1 ) ; } else { Console . WriteLine ( str ) ; } } }


"Find the resulting Colour Combination | C # program to find the resultant colour combination ; Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code"
Solution in C#:

using System ; class GFG { static char Colour_Combination ( string s ) { char temp = s [ 0 ] ; for ( int i = 1 ; i < s . Length ; i ++ ) { if ( temp != s [ i ] ) { if ( ( temp == ' B ' temp == ' G ' ) && ( s [ i ] == ' G ' s [ i ] == ' B ' ) ) temp = ' Y ' ; else if ( ( temp == ' B ' temp == ' Y ' ) && ( s [ i ] == ' Y ' s [ i ] == ' B ' ) ) temp = ' G ' ; else temp = ' B ' ; } } return temp ; } static void Main ( ) { string s = " GBYGB " ; Console . WriteLine ( Colour_Combination ( s ) ) ; } }


"Find the row whose product has maximum count of prime factors | C # implementation to find the row whose product has maximum number of prime factors ; function for SieveOfEratosthenes ; Create a bool array " isPrime [ 0 . . N ] " and initialize all entries it as true . A value in isPrime [ i ] will finally be false if i is not a prime , else true . ; check if isPrime [ p ] is not changed ; Update all multiples of p ; Print all isPrime numbers ; function to display the answer ; function to Count the row number of divisors in particular row multiplication ; Find count of occurrences of each prime factor ; Compute count of all divisors ; Update row number if factors of this row is max ; Clearing map to store prime factors for next row ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static readonly int N = 3 ; static readonly int M = 5 ; static int Large = ( int ) 1e6 ; static List < int > prime = new List < int > ( ) ; static void SieveOfEratosthenes ( ) { bool [ ] isPrime = new bool [ Large + 1 ] ; for ( int p = 0 ; p <= Large ; p ++ ) isPrime [ p ] = true ; for ( int p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . Add ( p ) ; } static void Display ( int [ , ] arr , int row ) { for ( int i = 0 ; i < M ; i ++ ) Console . Write ( arr [ row , i ] + " ▁ " ) ; } static void countDivisorsMult ( int [ , ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int row_no = 0 ; long max_factor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int no = arr [ i , j ] ; for ( int k = 0 ; k < prime . Count ; k ++ ) { while ( no > 1 && no % prime [ k ] == 0 ) { no /= prime [ k ] ; if ( mp . ContainsKey ( prime [ k ] ) ) mp [ prime [ k ] ] = prime [ k ] + 1 ; else mp . Add ( prime [ k ] , 1 ) ; } if ( no == 1 ) break ; } } int res = 1 ; foreach ( KeyValuePair < int , int > it in mp ) { res *= ( it . Value + 1 ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . Clear ( ) ; } Display ( arr , row_no ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 , 10 , 23 } , { 4 , 5 , 6 , 7 , 8 } , { 7 , 8 , 9 , 15 , 45 } } ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ; } }


"Find the segment that overlaps with maximum number of segments | C # program for the above approach ; Function to find the segment which overlaps with maximum number of segments ; ' L ' & ' R ' co - ordinates of all segments are stored in lvalues & rvalues ; Assign co - ordinates ; Co - ordinate compression ; Stores the required segment ; Stores the current maximum number of intersections ; Find number of ' R ' coordinates which are less than the ' L ' value of the current segment ; Find number of ' L ' coordinates which are greater than the ' R ' value of the current segment ; Segments excluding ' lesser ' and ' greater ' gives the number of intersections ; Update the current maximum ; Print segment coordinates ; Driver Code ; Given segments ; Size of segments array"
Solution in C#:

using System ; public class GFG { static void maxIntersection ( int [ , ] segments , int N ) { int [ ] rvalues = new int [ N ] ; int [ ] lvalues = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i , 0 ] ; rvalues [ i ] = segments [ i , 1 ] ; } Array . Sort ( lvalues ) ; Array . Sort ( rvalues ) ; int [ ] answer = { - 1 , - 1 } ; int numIntersections = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int lesser = lower_bound ( rvalues , 0 , segments . GetLength ( 0 ) , segments [ i , 0 ] ) ; int greater = Math . Max ( 0 , N - ( upper_bound ( lvalues , 0 , segments . GetLength ( 0 ) , segments [ i , 1 ] ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = new int [ ] { segments [ i , 0 ] , segments [ i , 1 ] } ; numIntersections = ( N - lesser - greater ) ; } } Console . Write ( answer [ 0 ] + " ▁ " + answer [ 1 ] ) ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } 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 [ , ] segments = { { 1 , 4 } , { 2 , 3 } , { 3 , 6 } } ; int N = segments . GetLength ( 0 ) ; maxIntersection ( segments , N ) ; } }


"Find the sequence number of a triangular number | C # code to print sequence number of a triangular number ; If N is not tringular number"
Solution in C#:

using System ; class GFG { public static void Main ( ) { int N = 21 ; int A = ( int ) ( Math . Sqrt ( 2 * N + 0.25 ) - 0.5 ) ; int B = A ; if ( B != A ) Console . Write ( " - 1" ) ; else Console . Write ( B ) ; } }


"Find the side of the squares which are inclined diagonally and lined in a row | C # program to find side of the squares inclined and touch each other externally at vertices and are lined in a row and distance between the centers of first and last squares is given ; Driver code"
Solution in C#:

using System ; class GFG { static void radius ( double n , double d ) { Console . WriteLine ( " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " + d / ( ( n - 1 ) * Math . Sqrt ( 2 ) ) ) ; } public static void Main ( ) { double d = 42 , n = 4 ; radius ( n , d ) ; } }


"Find the side of the squares which are lined in a row , and distance between the centers of first and last square is given | C # program to find side of the squares which are lined in a row and distance between the centers of first and last squares is given ; Driver code"
Solution in C#:

using System ; class GFG { static void radius ( int n , int d ) { Console . Write ( " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " + d / ( n - 1 ) ) ; } public static void Main ( ) { int d = 42 , n = 4 ; radius ( n , d ) ; } }


"Find the single digit sum of alphabetical values of a string | C # program for the above approach ; Traverse the given string ; If character is an alphabet ; Stores the sum of order of values ; Find the score ; Find the single digit sum ; Return the resultant sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int findTheSum ( string str ) { string alpha = " " ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) alpha += ( str [ i ] ) ; } int score = 0 , n = 0 ; for ( int i = 0 ; i < alpha . Length ; i ++ ) { if ( alpha [ i ] >= ' A ' && alpha [ i ] <= ' Z ' ) score += alpha [ i ] - ' A ' + 1 ; else score += alpha [ i ] - ' a ' + 1 ; } while ( score > 0 n > 9 ) { if ( score == 0 ) { score = n ; n = 0 ; } n += score % 10 ; score /= 10 ; } return n ; } public static void Main ( ) { string S = " GeeksforGeeks " ; Console . WriteLine ( findTheSum ( S ) ) ; } }


"Find the size of Largest Subset with positive Bitwise AND | C # program for the above approach ; Function to find the largest possible subset having Bitwise AND positive ; Stores the number of set bits at each bit position ; Traverse the given array arr [ ] ; Current bit position ; Loop till array element becomes zero ; If the last bit is set ; Increment frequency ; Divide array element by 2 ; Decrease the bit position ; Size of the largest possible subset ; Driver code"
Solution in C#:

using System ; class GFG { static void largestSubset ( int [ ] a , int N ) { int [ ] bit = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int x = 31 ; while ( a [ i ] > 0 ) { if ( ( int ) ( a [ i ] & 1 ) == ( int ) 1 ) { bit [ x ] ++ ; } a [ i ] = a [ i ] >> 1 ; x -- ; } } int max = Int32 . MinValue ; for ( int i = 0 ; i < 32 ; i ++ ) { max = Math . Max ( max , bit [ i ] ) ; } Console . WriteLine ( max ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 13 , 8 , 2 , 3 } ; int N = arr . Length ; largestSubset ( arr , N ) ; } }


"Find the size of largest group where groups are according to the xor of digits | C # implementation to Find the size of largest group , where groups are according to the xor of its digits . ; Function to find out xor of digit ; calculate xor digitwise ; return xor ; Function to find the size of largest group ; hash map for counting frequency ; counting freq of each element ; find the maximum ; Driver code ; initialise N"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int digit_xor ( int x ) { int xorr = 0 ; while ( x > 0 ) { xorr ^= x % 10 ; x = x / 10 ; } return xorr ; } static int find_count ( int n ) { Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( mpp . ContainsKey ( digit_xor ( i ) ) ) mpp [ digit_xor ( i ) ] = mpp [ digit_xor ( i ) ] + 1 ; else mpp . Add ( digit_xor ( i ) , 1 ) ; } int maxm = 0 ; foreach ( KeyValuePair < int , int > x in mpp ) { if ( x . Value > maxm ) maxm = x . Value ; } return maxm ; } public static void Main ( String [ ] args ) { int N = 13 ; Console . Write ( find_count ( N ) ) ; } }


"Find the size of largest subset of anagram words | C # Program to find the size of largest subset of anagram ; Utility function to find size of largest subset of anagram ; sort the String ; Increment the count of String ; Compute the maximum size of String ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int largestAnagramSet ( String [ ] arr , int n ) { int maxSize = 0 ; Dictionary < String , int > count = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { char [ ] temp = arr [ i ] . ToCharArray ( ) ; Array . Sort ( temp ) ; arr [ i ] = new String ( temp ) ; if ( count . ContainsKey ( arr [ i ] ) ) { count [ arr [ i ] ] = count [ arr [ i ] ] + 1 ; } else { count . Add ( arr [ i ] , 1 ) ; } maxSize = Math . Max ( maxSize , count [ arr [ i ] ] ) ; } return maxSize ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ant " , " magenta " , " magnate " , " tan " , " gnamate " } ; int n = arr . Length ; Console . WriteLine ( largestAnagramSet ( arr , n ) ) ; String [ ] arr1 = { " cars " , " bikes " , " arcs " , " steer " } ; n = arr1 . Length ; Console . WriteLine ( largestAnagramSet ( arr1 , n ) ) ; } }


"Find the slope of the given number | C # implementation to find slope of a number ; function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver code"
Solution in C#:

class GFG { static int slopeOfNum ( string num , int n ) { int slope = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( num [ i ] > num [ i - 1 ] && num [ i ] > num [ i + 1 ] ) slope ++ ; else if ( num [ i ] < num [ i - 1 ] && num [ i ] < num [ i + 1 ] ) slope ++ ; } return slope ; } public static void Main ( ) { string num = "1213321" ; int n = num . Length ; System . Console . WriteLine ( " Slope ▁ = ▁ " + slopeOfNum ( num , n ) ) ; } }


"Find the smallest after deleting given elements | C # program to find the smallest number from the array after n deletions ; Returns minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the smallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it smallestElement ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findSmallestAfterDel ( int [ ] arr , int m , int [ ] del , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] = mp [ del [ i ] ] + 1 ; } else { mp . Add ( del [ i ] , 1 ) ; } } int smallestElement = int . MaxValue ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] = mp [ arr [ i ] ] - 1 ; if ( mp [ arr [ i ] ] == 0 ) mp . Remove ( arr [ i ] ) ; } else smallestElement = Math . Min ( smallestElement , arr [ i ] ) ; } return smallestElement ; } public static void Main ( String [ ] args ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 4 , 56 , 5 } ; int n = del . Length ; Console . WriteLine ( findSmallestAfterDel ( array , m , del , n ) ) ; } }


"Find the smallest contiguous sum pair in an Array | C # program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Stores the contiguous pair ; Initialize minimum sum ; Checking for minimum value ; Add to pair ; Updating pair ; Driver code"
Solution in C#:

using System ; using System . Collections ; using System . Collections . Generic ; class GFG { public static ArrayList smallestSumpair ( int [ ] arr , int n ) { ArrayList pair = new ArrayList ( ) ; int min_sum = int . MaxValue , i ; for ( i = 1 ; i < n ; i ++ ) { if ( min_sum > ( arr [ i ] + arr [ i - 1 ] ) ) { min_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . Count == 0 ) { pair . Add ( arr [ i - 1 ] ) ; pair . Add ( arr [ i ] ) ; } else { pair [ 0 ] = arr [ i - 1 ] ; pair [ 1 ] = arr [ i ] ; } } } return pair ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 9 , - 3 , 2 , 0 } ; int N = arr . Length ; ArrayList pair = new ArrayList ( ) ; pair = smallestSumpair ( arr , N ) ; Console . Write ( pair [ 0 ] + " ▁ " + pair [ 1 ] ) ; } }


"Find the smallest number X such that X ! contains at least Y trailing zeros . | C # implementation of the approach ; Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code"
Solution in C#:

class GFG { static int countFactor ( int P , int X ) { if ( X < P ) return 0 ; return ( X / P + countFactor ( P , X / P ) ) ; } static int findSmallestX ( int Y ) { int low = 0 , high = 5 * Y ; int N = 0 ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( countFactor ( 5 , mid ) < Y ) { low = mid + 1 ; } else { N = mid ; high = mid - 1 ; } } return N ; } static void Main ( ) { int Y = 10 ; System . Console . WriteLine ( findSmallestX ( Y ) ) ; } }


"Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | C # program to find the smallest positive value that cannot be represented as sum of subsets of a given sorted array ; Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver code"
Solution in C#:

using System ; class GFG { static int findSmallest ( int [ ] arr , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } public static void Main ( ) { int [ ] arr1 = { 1 , 3 , 4 , 5 } ; int n1 = arr1 . Length ; Console . WriteLine ( findSmallest ( arr1 , n1 ) ) ; int [ ] arr2 = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = arr2 . Length ; Console . WriteLine ( findSmallest ( arr2 , n2 ) ) ; int [ ] arr3 = { 1 , 1 , 1 , 1 } ; int n3 = arr3 . Length ; Console . WriteLine ( findSmallest ( arr3 , n3 ) ) ; int [ ] arr4 = { 1 , 1 , 3 , 4 } ; int n4 = arr4 . Length ; Console . WriteLine ( findSmallest ( arr4 , n4 ) ) ; } }


"Find the smallest positive number missing from an unsorted array | Set 1 | C # program for the above approach ; Function for finding the first missing positive number ; Check if 1 is present in array or not ; Loop to check boundary condition and for swapping ; Finding which index has value less than n ; If array has values from 1 to n ; Driver Code"
Solution in C#:

using System ; public class GFG { static int firstMissingPositive ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( arr [ i ] >= 1 && arr [ i ] <= n && arr [ i ] != arr [ arr [ i ] - 1 ] ) { int temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != i + 1 ) return ( i + 1 ) ; return ( n + 1 ) ; } static public void Main ( ) { int [ ] arr = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . Length ; int ans = firstMissingPositive ( arr , n ) ; Console . WriteLine ( ans ) ; } }


"Find the smallest positive number missing from an unsorted array | Set 1 | C # program to find the smallest positive missing number ; Utility function that puts all non - positive ( 0 and negative ) numbers on left side of arr [ ] and return count of such numbers ; increment count of non - positive integers ; Find the smallest positive missing number in an array that contains all positive integers ; Mark arr [ i ] as visited by making arr [ arr [ i ] - 1 ] negative . Note that 1 is subtracted as index start from 0 and positive numbers start from 1 ; Return the first index value at which is positive ; 1 is added becuase indexes start from 0 ; Find the smallest positive missing number in array that contains both positive and negative integers ; First separate positive and negative numbers ; Shift the array and call findMissingPositive for positive part ; Driver code"
Solution in C#:

using System ; class main { static int segregate ( int [ ] arr , int size ) { int j = 0 , i ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] <= 0 ) { int temp ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } return j ; } static int findMissingPositive ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { if ( Math . Abs ( arr [ i ] ) - 1 < size && arr [ Math . Abs ( arr [ i ] ) - 1 ] > 0 ) arr [ Math . Abs ( arr [ i ] ) - 1 ] = - arr [ Math . Abs ( arr [ i ] ) - 1 ] ; } for ( i = 0 ; i < size ; i ++ ) if ( arr [ i ] > 0 ) return i + 1 ; return size + 1 ; } static int findMissing ( int [ ] arr , int size ) { int shift = segregate ( arr , size ) ; int [ ] arr2 = new int [ size - shift ] ; int j = 0 ; for ( int i = shift ; i < size ; i ++ ) { arr2 [ j ] = arr [ i ] ; j ++ ; } return findMissingPositive ( arr2 , j ) ; } public static void Main ( ) { int [ ] arr = { 0 , 10 , 2 , - 10 , - 20 } ; int arr_size = arr . Length ; int missing = findMissing ( arr , arr_size ) ; Console . WriteLine ( " The ▁ smallest ▁ positive ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }


"Find the smallest positive number missing from an unsorted array | Set 2 | C # program to find the smallest positive missing number ; Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code"
Solution in C#:

using System ; class GFG { static int findMissingNo ( int [ ] arr , int n ) { int val ; int nextval ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= 0 arr [ i ] > n ) continue ; val = arr [ i ] ; while ( arr [ val - 1 ] != val ) { nextval = arr [ val - 1 ] ; arr [ val - 1 ] = val ; val = nextval ; if ( val <= 0 val > n ) break ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 } ; int arr_size = arr . Length ; int missing = findMissingNo ( arr , arr_size ) ; Console . Write ( " The ▁ smallest ▁ positive " + " ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }


"Find the smallest positive number missing from an unsorted array | Set 3 | C # program to find the smallest positive missing number ; Function to find the smallest positive missing number ; Default smallest Positive Integer ; Store values in set which are greater than variable m ; Store value when m is less than current index of given array ; Increment m when it is equal to current element ; Increment m when it is one of the element of the set ; Return the required answer ; Driver code ; Function call"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int findMissingPositive ( int [ ] arr , int n ) { int m = 1 ; HashSet < int > x = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . Add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . Contains ( m ) ) { x . Remove ( m ) ; m = m + 1 ; } } } return m ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . Length ; Console . WriteLine ( findMissingPositive ( arr , n ) ) ; } }


"Find the smallest positive number which can not be represented by given digits | C # program to find the smallest positive number which can not be represented by given digits ; Function to find the smallest positive number which can not be represented by given digits ; Storing the count of 0 digit or store the value at 0 th index ; Calculates the min value in the array starting from 1 st index and also store it index . ; If its value at 0 th index is less than min value than either 10 , 100 , 1000 ... can 't be expressed ; If it value is greater than min value than iterate the loop upto first min value index and simply print it index value . ; Driver code ; Value of N is always 10 as we take digit from 0 to 9 ; Calling function"
Solution in C#:

using System ; class GFG { static void expressDigit ( int [ ] arr , int n ) { int min = 9 , index = 0 , temp = 0 ; temp = arr [ 0 ] ; for ( int i = 1 ; i < 10 ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; index = i ; } } if ( temp < min ) { Console . Write ( 1 ) ; for ( int i = 1 ; i <= temp + 1 ; i ++ ) Console . Write ( 0 ) ; } else { for ( int i = 0 ; i < min ; i ++ ) Console . Write ( index ) ; Console . Write ( index ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 } ; int N = 10 ; expressDigit ( arr , N ) ; } }


"Find the smallest twins in given range | C # program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the smallest twin in range ; Driver program"
Solution in C#:

using System ; public class GFG { static void printTwins ( int low , int high ) { bool [ ] prime = new bool [ high + 1 ] ; bool twin = false ; for ( int i = 0 ; i < prime . Length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= Math . Floor ( Math . Sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( int i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { int a = i + 2 ; Console . Write ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " + i + " , ▁ " + a + " ) " ) ; twin = true ; break ; } } if ( twin == false ) { Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } } public static void Main ( ) { printTwins ( 10 , 100 ) ; } }


"Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C # Program to implement the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Check if sum of first i natural number >= X ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X"
Solution in C#:

using System ; public class GFG { static bool isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { for ( int i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } return 0 ; } static public void Main ( ) { int X = 14 ; Console . Write ( minimumPossible ( X ) ) ; } }


"Find the smallest value of N such that sum of first N natural numbers is â ‰¥ X | C # program for the above approach ; Function to check if sum of first N natural numbers is >= X ; Finds minimum value of N such that sum of first N natural number >= X ; Binary Search ; Checks if sum of first ' mid ' natural numbers is greater than equal to X ; Update res ; Update high ; Update low ; Driver Code ; Input ; Finds minimum value of N such that sum of first N natural number >= X"
Solution in C#:

using System ; class GFG { static bool isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { int low = 1 , high = X , res = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( isGreaterEqual ( mid , X ) ) { res = mid ; high = mid - 1 ; } else low = mid + 1 ; } return res ; } static public void Main ( ) { int X = 14 ; Console . Write ( minimumPossible ( X ) ) ; } }


"Find the smallest window in a string containing all characters of another string | C # program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; Check if string ' s ▁ length ▁ is ▁ ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string Count of characters ; Count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ ▁ with ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver Method"
Solution in C#:

 using System ; class GFG { static int no_of_chars = 256 ; static String findSubString ( String str , String pat ) { int len1 = str . Length ; int len2 = pat . Length ; if ( len1 < len2 ) { Console . WriteLine ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } int [ ] hash_pat = new int [ no_of_chars ] ; int [ ] hash_str = new int [ no_of_chars ] ; for ( int i = 0 ; i < len2 ; i ++ ) hash_pat [ pat [ i ] ] ++ ; int start = 0 , start_index = - 1 , min_len = int . MaxValue ; int count = 0 ; for ( int j = 0 ; j < len1 ; j ++ ) { hash_str [ str [ j ] ] ++ ; if ( hash_str [ str [ j ] ] <= hash_pat [ str [ j ] ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] hash_pat [ str [ start ] ] == 0 ) { if ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] ) hash_str [ str [ start ] ] -- ; start ++ ; } int len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == - 1 ) { Console . WriteLine ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } return str . Substring ( start_index , min_len ) ; } public static void Main ( String [ ] args ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; String pat = " tist " ; Console . WriteLine ( " Smallest ▁ window ▁ is ▁ :
 ▁ " + findSubString ( str , pat ) ) ; } }


"Find the smallest window in a string containing all characters of another string | Function ; Answer Length of ans ; Starting index of ans ; Creating map ; References of Window ; Traversing the window ; Calculations ; Condition matching ; Sorting ans ; Sliding I Calculation for removing I ; Driver code"
Solution in C#:

using System ; class GFG { static String Minimum_Window ( char [ ] s , char [ ] t ) { int [ ] m = new int [ 256 ] ; int ans = int . MaxValue ; int start = 0 ; int count = 0 , i = 0 ; for ( i = 0 ; i < t . Length ; i ++ ) { if ( m [ t [ i ] ] == 0 ) count ++ ; m [ t [ i ] ] ++ ; } i = 0 ; int j = 0 ; while ( j < s . Length ) { m [ s [ j ] ] -- ; if ( m [ s [ j ] ] == 0 ) count -- ; if ( count == 0 ) { while ( count == 0 ) { if ( ans > j - i + 1 ) { ans = Math . Min ( ans , j - i + 1 ) ; start = i ; } m [ s [ i ] ] ++ ; if ( m [ s [ i ] ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != int . MaxValue ) return String . Join ( " " , s ) . Substring ( start , ans ) ; else return " - 1" ; } public static void Main ( String [ ] args ) { String s = " ADOBECODEBANC " ; String t = " ABC " ; Console . Write ( " - - > Smallest ▁ window ▁ that ▁ contain ▁ all ▁ character ▁ : ▁ " ) ; Console . Write ( Minimum_Window ( s . ToCharArray ( ) , t . ToCharArray ( ) ) ) ; } }


"Find the string present at the middle of a lexicographically increasing sequence of strings from S to T | C # Program for the above approach ; Function to print the string at the middle of lexicographically increasing sequence of strings from S to T ; Stores the base 26 digits after addition ; Iterete from right to left and add carry to next position ; Reduce the number to find the middle string by dividing each position by 2 ; If current value is odd , carry 26 to the next index value ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static void printMiddleString ( string S , string T , int N ) { int [ ] a1 = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = ( int ) S [ i ] - 97 + ( int ) T [ i ] - 97 ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += ( int ) a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( ( a1 [ i ] & 1 ) != 0 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] = ( int ) a1 [ i ] / 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { Console . Write ( Convert . ToChar ( a1 [ i ] + ' a ' ) ) ; } } public static void Main ( ) { int N = 5 ; string S = " afogk " ; string T = " asdji " ; printMiddleString ( S , T , N ) ; } }


"Find the subarray of size K with minimum XOR | C # implementation to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size ; Function Call"
Solution in C#:

 using System ; class GFG { static void findMinXORSubarray ( int [ ] arr , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } Console . Write ( min_xor + "
 " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . Length ; findMinXORSubarray ( arr , n , k ) ; } }


"Find the subarray with least average | A Simple C # program to find minimum average subarray ; Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver method to test the above function ; Subarray size"
Solution in C#:

using System ; class Test { static int [ ] arr = new int [ ] { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; static void findMinAvgSubarray ( int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } Console . Write ( " Subarray ▁ between ▁ [ " + res_index + " , ▁ " + ( res_index + k - 1 ) + " ] ▁ has ▁ minimum ▁ average " ) ; } public static void Main ( ) { int k = 3 ; findMinAvgSubarray ( arr . Length , k ) ; } }


"Find the sum of all Truncatable primes below N | C # implementation of the above approach . ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int N = 1000005 ; static Boolean [ ] prime = new Boolean [ N ] ; static void sieve ( ) { Array . Fill ( prime , true ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } static int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; Boolean flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } public static void Main ( String [ ] args ) { int n = 25 ; sieve ( ) ; Console . WriteLine ( sumTruncatablePrimes ( n ) ) ; } }


"Find the sum of all multiples of 2 and 5 below N | C # program to find the sum of all multiples of 2 and 5 below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 2 and 5 below N ; Since , we need the sum of multiples less than N ; Driver code"
Solution in C#:

using System ; public class GFG { static long sumAP ( long n , long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static long sumMultiples ( long n ) { n -- ; return sumAP ( n , 2 ) + sumAP ( n , 5 ) - sumAP ( n , 10 ) ; } static public void Main ( ) { long n = 20 ; Console . WriteLine ( sumMultiples ( n ) ) ; } }


"Find the sum of all possible pairs in an array of N elements | C # implementation of the approach ; Function to return the sum of the elements of all possible pairs from the array ; To store the required sum ; For every element of the array ; It appears ( 2 * n ) times ; Driver code"
Solution in C#:

using System ; class GFG { static int sumPairs ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] * ( 2 * n ) ) ; } return sum ; } static public void Main ( String [ ] arg ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( sumPairs ( arr , n ) ) ; } }


"Find the sum of all the terms in the n | C # implementation to find the sum of all the terms in the nth row of the given series ; method to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver method"
Solution in C#:

using System ; class Test { static int sumOfTermsInNthRow ( int n ) { int sum = ( int ) ( n * ( 2 * Math . Pow ( n , 2 ) + 1 ) ) ; return sum ; } public static void Main ( ) { int n = 4 ; Console . Write ( " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " + sumOfTermsInNthRow ( n ) ) ; } }


"Find the sum of digits of a number at even and odd places | C # implementation of the above approach ; Function to find the sum of the odd and even positioned digits in a number ; If n is odd then the last digit will be odd positioned ; To store the respective sums ; While there are digits left process ; If current digit is odd positioned ; Even positioned digit ; Invert state ; Remove last digit ; Driver code"
Solution in C#:

using System ; class GFG { static void getSum ( int n ) { bool isOdd = ( n % 2 == 1 ) ? true : false ; int sumOdd = 0 , sumEven = 0 ; while ( n != 0 ) { if ( isOdd ) sumOdd += n % 10 ; else sumEven += n % 10 ; isOdd = ! isOdd ; n /= 10 ; } Console . WriteLine ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; Console . Write ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } static public void Main ( ) { int n = 457892 ; getSum ( n ) ; } }


"Find the sum of digits of a number at even and odd places | C # implementation of the approach ; Function to find the sum of the odd and even positioned digits in a number ; To store the respective sum ; Converting integer to String ; Traversing the String ; Driver code"
Solution in C#:

using System ; class GFG { static void getSum ( int n ) { int sumOdd = 0 ; int sumEven = 0 ; String num = n . ToString ( ) ; for ( int i = 0 ; i < num . Length ; i ++ ) if ( i % 2 == 0 ) sumOdd = sumOdd + ( num [ i ] - '0' ) ; else sumEven = sumEven + ( num [ i ] - '0' ) ; Console . WriteLine ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; Console . WriteLine ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void Main ( ) { int n = 457892 ; getSum ( n ) ; } }


"Find the sum of digits of a number at even and odd places | C # implementation of the approach ; Function to return the reverse of a number ; Function to find the sum of the odd and even positioned digits in a number ; If c is even number then it means digit extracted is at even place ; Driver code"
Solution in C#:

using System ; class GFG { static int reverse ( int n ) { int rev = 0 ; while ( n != 0 ) { rev = ( rev * 10 ) + ( n % 10 ) ; n /= 10 ; } return rev ; } static void getSum ( int n ) { n = reverse ( n ) ; int sumOdd = 0 , sumEven = 0 , c = 1 ; while ( n != 0 ) { if ( c % 2 == 0 ) sumEven += n % 10 ; else sumOdd += n % 10 ; n /= 10 ; c ++ ; } Console . WriteLine ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; Console . WriteLine ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void Main ( ) { int n = 457892 ; getSum ( n ) ; } }


"Find the sum of elements of the Matrix generated by the given rules | C # implementation of the approach ; Function to return the required sum ; To store the sum ; For every row ; Update the sum as A appears i number of times in the current row ; Update A for the next row ; Return the sum ; Driver code"
Solution in C#:

using System ; class GFG { static int sum ( int A , int B , int R ) { int sum = 0 ; for ( int i = 1 ; i <= R ; i ++ ) { sum = sum + ( i * A ) ; A = A + B ; } return sum ; } public static void Main ( ) { int A = 5 , B = 3 , R = 3 ; Console . Write ( sum ( A , B , R ) ) ; } }


"Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | C # program to find sum of the first n terms of the given series ; Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver code"
Solution in C#:

using System ; class GFG { static int calSum ( int n ) { return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; } static public void Main ( ) { int n = 3 ; Console . WriteLine ( calSum ( n ) ) ; } }


"Find the sum of first N terms of the series 2 Ã — 3 + 4 Ã — 4 + 6 Ã — 5 + 8 Ã — 6 + ... | C # program to find sum upto N term of the series : 2 A 3 + 4 A 4 + 6 A 5 + 8 A 6 + ... ; calculate sum upto N term of series ; Driver code"
Solution in C#:

using System ; class GFG { static void Sum_upto_nth_Term ( int n ) { int r = n * ( n + 1 ) * ( 2 * n + 7 ) / 3 ; Console . Write ( r ) ; } public static void Main ( ) { int N = 5 ; Sum_upto_nth_Term ( N ) ; } }


"Find the sum of last n nodes of the given Linked List | C # implementation to find the sum of last ' n ' nodes of the Linked List ; A Linked list node ; function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list to the new node ; move the head to point to the new node ; utility function to find the sum of last ' n ' nodes ; if n == 0 ; calculate the length of the linked list ; count of first ( len - n ) nodes ; just traverse the 1 st ' c ' nodes ; move to next node ; now traverse the last ' n ' nodes and add them ; accumulate node 's data to sum ; move to next node ; required sum ; Driver code ; create linked list 10.6 .8 .4 .12"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head ; static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } static int sumOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; int sum = 0 , len = 0 ; Node temp = head ; while ( temp != null ) { len ++ ; temp = temp . next ; } int c = len - n ; temp = head ; while ( temp != null && c -- > 0 ) { temp = temp . next ; } while ( temp != null ) { sum += temp . data ; temp = temp . next ; } return sum ; } public static void Main ( String [ ] args ) { push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; int n = 2 ; Console . WriteLine ( " Sum ▁ of ▁ last ▁ " + n + " ▁ nodes ▁ = ▁ " + sumOfLastN_NodesUtil ( head , n ) ) ; } }


"Find the sum of last n nodes of the given Linked List | C # implementation to find the sum of last ' n ' nodes of the Linked List ; A Linked list node ; function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list to the new node ; move the head to point to the new node ; utility function to find the sum of last ' n ' nodes ; if n == 0 ; reverse the linked list ; traverse the 1 st ' n ' nodes of the reversed linked list and add them ; accumulate node ' s ▁ data ▁ to ▁ ' sum ' ; move to next node ; reverse back the linked list ; required sum ; Driver code ; create linked list 10 -> 6 -> 8 -> 4 -> 12"
Solution in C#:

using System ; class GFG { public class Node { public int data ; public Node next ; } ; static Node head ; static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } static void reverseList ( Node head_ref ) { Node current , prev , next ; current = head_ref ; prev = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; head = head_ref ; } static int sumOfLastN_NodesUtil ( int n ) { if ( n <= 0 ) return 0 ; reverseList ( head ) ; int sum = 0 ; Node current = head ; while ( current != null && n -- > 0 ) { sum += current . data ; current = current . next ; } reverseList ( head ) ; return sum ; } public static void Main ( String [ ] args ) { push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; int n = 2 ; Console . WriteLine ( " Sum ▁ of ▁ last ▁ " + n + " ▁ nodes ▁ = ▁ " + sumOfLastN_NodesUtil ( n ) ) ; } }


"Find the sum of last n nodes of the given Linked List | C # implementation to find the sum of last ' n ' nodes of the Linked List ; A Linked list node ; function to insert a node at the beginning of the linked list ; allocate node ; put in the data ; link the old list to the new node ; move the head to point to the new node ; utility function to find the sum of last ' n ' nodes ; if n == 0 ; traverses the list from left to right ; push the node ' s ▁ data ▁ onto ▁ the ▁ stack ▁ ' st ' ; move to next node ; pop ' n ' nodes from ' st ' and . Add them ; required sum ; Driver code ; create linked list 10.6 .8 .4 .12"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int sumOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; Stack < int > st = new Stack < int > ( ) ; int sum = 0 ; while ( head != null ) { st . Push ( head . data ) ; head = head . next ; } while ( n -- > 0 ) { sum += st . Peek ( ) ; st . Pop ( ) ; } return sum ; } public static void Main ( String [ ] args ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; int n = 2 ; Console . Write ( " Sum ▁ of ▁ last ▁ " + n + " ▁ nodes ▁ = ▁ " + sumOfLastN_NodesUtil ( head , n ) ) ; } }


"Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | C # program to find the sum of n terms ; Function ot calculate the sum ; Return total sum ; Driver code"
Solution in C#:

using System ; class GFG { static int calculateSum ( int n ) { return ( int ) Math . Pow ( n * ( n + 1 ) / 2 , 2 ) ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( calculateSum ( n ) ) ; } }


"Find the sum of numbers from 1 to n excluding those which are powers of K | C # implementation of the approach ; Function to return the sum of all the powers of k from the range [ 1 , n ] ; To store the sum of the series ; While current power of k <= n ; Add current power to the sum ; Next power of k ; Return the sum of the series ; Find to return the sum of the elements from the range [ 1 , n ] excluding those which are powers of k ; Sum of all the powers of k from [ 1 , n ] ; Sum of all the elements from [ 1 , n ] ; Return the required sum ; Driver code"
Solution in C#:

using System ; class GFG { static long sumPowersK ( long n , long k ) { long sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } static long getSum ( long n , long k ) { long pwrK = sumPowersK ( n , k ) ; long sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } public static void Main ( ) { long n = 10 , k = 3 ; Console . WriteLine ( getSum ( n , k ) ) ; } }


"Find the sum of power of bit count raised to the power B | C # program for the above approach ; Function to calculate a ^ b mod m using fast - exponentiation method ; Function to get no of set bits in binary representation of positive integer n ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code ."
Solution in C#:

using System ; class GFG { static int fastmod ( int baseval , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( baseval , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( baseval , exp - 1 , mod ) % mod * baseval % mod ) % mod ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int findPowerSum ( int n , int [ ] ar ) { int mod = ( int ) 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int baseval = countSetBits ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( baseval , exp , mod ) ; sum %= mod ; } return sum ; } public static void Main ( ) { int n = 3 ; int [ ] ar = { 1 , 2 , 3 } ; Console . WriteLine ( findPowerSum ( n , ar ) ) ; } }


"Find the sum of prime numbers in the Kth array | C # mplementation of the approach ; To store whether a number is prime or not ; Function for Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to return the sum of primes in the Kth array ; Update vector v to store all the prime numbers upto MAX ; To store the sum of primes in the kth array ; Count of primes which are in the arrays from 1 to k - 1 ; k is the number of primes in the kth array ; A prime has been added to the sum ; Driver code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int MAX = 1000000 ; static bool [ ] prime = new bool [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int sumPrime ( int k ) { SieveOfEratosthenes ( ) ; List < int > v = new List < int > ( ) ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) v . Add ( i ) ; } int sum = 0 ; int skip = ( k * ( k - 1 ) ) / 2 ; while ( k > 0 ) { sum += v [ skip ] ; skip ++ ; k -- ; } return sum ; } public static void Main ( String [ ] args ) { int k = 3 ; Console . WriteLine ( sumPrime ( k ) ) ; } }


"Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | C # program for sum of the series 0. x , 0. xx , 0. xxx , ... upto k terms ; function which return the sum of series ; Driver code"
Solution in C#:

using System ; class GFG { static float sumOfSeries ( int x , int k ) { float y = ( float ) ( ( ( float ) ( x ) / 81 ) * ( 9 * k - 1 + Math . Pow ( 10 , ( - 1 ) * k ) ) ) ; return y ; } public static void Main ( ) { int x = 9 ; int k = 20 ; Console . Write ( sumOfSeries ( x , k ) ) ; } }


"Find the sum of series 3 , 7 , 13 , 21 , 31. ... | C # program to find sum of n terms of the given series ; Function to calculate sum ; returning the final sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int calculateSum ( int n ) { return ( n * ( ( int ) Math . Pow ( n , 2 ) + 3 * n + 5 ) ) / 3 ; } public static void Main ( ) { int n = 25 ; Console . WriteLine ( calculateSum ( n ) ) ; } }


"Find the sum of series 3 , | calculate sum upto N term of series ; Driver code"
Solution in C#:

class GFG { static int Sum_upto_nth_Term ( int n ) { return ( 1 - ( int ) System . Math . Pow ( - 2 , n ) ) ; } public static void Main ( ) { int N = 5 ; System . Console . WriteLine ( Sum_upto_nth_Term ( N ) ) ; } }


"Find the sum of the ascii values of characters which are present at prime positions | C # implementation of the approach ; Function that returns true if n is prime ; Function to return the sum of the ascii values of the characters which are present at prime positions ; To store the sum ; For every character ; If current position is prime then add the ASCII value of the character at the current position ; Driver code"
Solution in C#:

using System ; class GFG { static bool isPrime ( int n ) { if ( n == 0 n == 1 ) { return false ; } for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static int sumAscii ( string str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i + 1 ) ) { sum += ( int ) ( str [ i ] ) ; } } return sum ; } public static void Main ( ) { string str = " geeksforgeeks " ; int n = str . Length ; Console . WriteLine ( sumAscii ( str , n ) ) ; } }


"Find the sum of the costs of all possible arrangements of the cells | C # implementation of the approach ; To store the factorials and factorial mod inverse of the numbers ; Function to return ( a ^ m1 ) % mod ; Function to find the factorials of all the numbers ; Function to find factorial mod inverse of all the numbers ; Function to return nCr ; Function to return the sum of the costs of all the possible arrangements of the cells ; For all possible X 's ; For all possible Y 's ; Driver code"
Solution in C#:

using System ; class GFG { static int N = 20 ; static int mod = 1000000007 ; static int [ ] factorial = new int [ N ] ; static int [ ] modinverse = new int [ N ] ; static int power ( int a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( a * a ) % mod ; else if ( ( m1 & 1 ) != 0 ) return ( a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = power ( factorial [ N - 1 ] , mod - 2 ) % mod ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static int binomial ( int n , int r ) { if ( r > n ) return 0 ; int a = ( factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( a * modinverse [ r ] ) % mod ; return a ; } static int arrange ( int n , int m , int k ) { factorialfun ( ) ; modinversefun ( ) ; int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) ans += ( i * ( n - i ) * m * m ) % mod ; ans = 8 ; for ( int i = 1 ; i < m ; i ++ ) ans += ( i * ( m - i ) * n * n ) % mod ; ans = ( ans * binomial ( n * m - 2 , k - 2 ) ) % mod + 8 ; return ans ; } public static void Main ( String [ ] args ) { int n = 2 , m = 2 , k = 2 ; Console . WriteLine ( arrange ( n , m , k ) ) ; } }


"Find the sum of the diagonal elements of the given N X N spiral matrix | C # implementation of the approach ; Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code"
Solution in C#:

class GFG { static int findSum ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } static void Main ( ) { int n = 4 ; System . Console . WriteLine ( findSum ( n ) ) ; } }


"Find the sum of the first N Centered Decagonal Numbers | C # program to find the sum of the first N centred decagonal number ; Function to find the N - th centred decagonal number ; Formula to calculate nth centered_decagonal number & return it into main function . ; Function to find the sum of the first N centered decagonal numbers ; Variable to store the sum ; Iterating through the range ; Driver code ; Display first Nth centered_decagonal number"
Solution in C#:

using System ; class GFG { static int Centered_decagonal_num ( int n ) { return ( 5 * n * n - 5 * n + 1 ) ; } static int sum_Centered_decagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_decagonal_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( sum_Centered_decagonal_num ( n ) ) ; } }


"Find the sum of the first N Centered Dodecagonal Number | C # program to find the sum of the first N centred dodecagonal number ; Function to find the N - th centered dodecagonal number ; Formula to calculate nth Centered_Dodecagonal number ; Function to find the sum of the first N Centered_Dodecagonal number ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code"
Solution in C#:

using System ; class GFG { static int Centered_Dodecagonal_num ( int n ) { return 6 * n * ( n - 1 ) + 1 ; } static int sum_Centered_Dodecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Dodecagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . Write ( sum_Centered_Dodecagonal_num ( n ) ) ; } }


"Find the sum of the first N Centered Octadecagonal Numbers | C # program to find the sum of the first N centered octadecagonal numbers ; Function to find the N - th centered octadecagonal number ; Formula to calculate nth centered octadecagonal number ; Function to find the sum of the first N centered octadecagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver Code"
Solution in C#:

using System ; class GFG { static int center_octadecagon_num ( int n ) { return ( 9 * n * n - 9 * n + 1 ) ; } static int sum_center_octadecagon_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_octadecagon_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( sum_center_octadecagon_num ( n ) ) ; } }


"Find the sum of the first N Centered Octagonal Number | C # program to find the sum of the first N centered octagonal number ; Function to find N - th centered octagonal number ; Formula to calculate nth centered octagonal number ; Function to find the sum of the first N centered octagonal numbers ; Variable to store the sum ; Iterating through the first N numbers ; Driver code"
Solution in C#:

using System ; class GFG { static int center_Octagonal_num ( int n ) { return ( 4 * n * n - 4 * n + 1 ) ; } static int sum_center_Octagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_Octagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum_center_Octagonal_num ( n ) ) ; } }


"Find the sum of the first N Centered Pentagonal Number | C # program to find the sum of the first N centered pentagonal numbers ; Function to find the Centered_Pentagonal number ; Formula to calculate nth Centered_Pentagonal number & return it into main function . ; Function to find the sum of the first N Centered_Pentagonal numbers ; To get the sum ; Iterating through the range 1 to N ; Driver code ; Display first Nth Centered_Pentagonal number"
Solution in C#:

using System ; class GFG { static int Centered_Pentagonal_num ( int n ) { return ( 5 * n * n - 5 * n + 2 ) / 2 ; } static int sum_Centered_Pentagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentagonal_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . Write ( ( sum_Centered_Pentagonal_num ( n ) ) ) ; } }


"Find the sum of the first N Centered heptagonal number | C # program to find the sum of the first N centered heptagonal numbers ; Function to find the N - th centered heptagonal number ; Formula to calculate nth centered heptagonal number ; Function to find the sum of the first N centered heptagonal numbers ; Variable to store the sum ; Iterating through the range 1 to N ; Driver Code"
Solution in C#:

using System ; class GFG { public static int center_heptagonal_num ( int n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } public static int sum_center_heptagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += center_heptagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . Write ( sum_center_heptagonal_num ( n ) ) ; } }


"Find the sum of the first N Dodecagonal Numbers | C # program to find the sum of the first N dodecagonal numbers ; Function to find the N - th dodecagonal number ; Formula to calculate N - th dodecagonal number ; Function to find the sum of the first N dodecagonal numbers ; Variable to get the sum ; Iterating through the first N numbers ; Compute the sum ; Driver Code ; Display first Nth centered_decagonal number"
Solution in C#:

using System ; class GFG { static int Dodecagonal_num ( int n ) { return ( 5 * n * n - 4 * n ) ; } static int sum_Dodecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Dodecagonal_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( sum_Dodecagonal_num ( n ) ) ; } }


"Find the sum of the first Nth Centered Hexadecagonal Number | C # program to find the sum of the first N centred hexadecagonal numbers ; Centered_Hexadecagonal number function ; Formula to calculate nth Centered_Hexadecagonal number & return it into main function . ; Function to find the sum of the first N centered hexadecagonal number ; Variable to store the sum ; Loop to iterate through the first N numbers ; Finding the sum ; Driver Code ; Display first Nth Centered_Hexadecagonal number"
Solution in C#:

using System ; class GFG { public static int Centered_Hexadecagonal_num ( int n ) { return ( 8 * n * n - 8 * n + 1 ) ; } public static int sum_Centered_Hexadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Hexadecagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . Write ( sum_Centered_Hexadecagonal_num ( n ) ) ; } }


"Find the sum of the first Nth Centered Pentadecagonal Number | C # program to find the sum of the first N centered pentadecagonal number ; Function to find the centered pentadecagonal number ; Formula to calculate N - th centered pentadecagonal number ; Function to find the sum of the first N centered pentadecagonal numbers ; Variable to store the sum ; Driver Code"
Solution in C#:

using System ; class GFG { static int Centered_Pentadecagonal_num ( int n ) { return ( 15 * n * n - 15 * n + 2 ) / 2 ; } static int sum_Centered_Pentadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += Centered_Pentadecagonal_num ( i ) ; } return summ ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( sum_Centered_Pentadecagonal_num ( n ) ) ; } }


"Find the sum of the first Nth Centered Tridecagonal Numbers | C # program to find the sum of the first Nth centered tridecagonal number ; Function to calculate the N - th centered tridecagonal number ; Formula to calculate Nth centered tridecagonal number & return it ; Function to find the sum of the first N centered tridecagonal numbers ; Variable to store the sum ; Loop to iterate and find the sum of first N centered tridecagonal numbers ; Driver code"
Solution in C#:

using System ; class GFG { public static int Centered_tridecagonal_num ( int n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } public static int sum_Centered_tridecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { summ += Centered_tridecagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum_Centered_tridecagonal_num ( n ) ) ; } }


"Find the sum of the first Nth Heptadecagonal Number | C # program to find the sum of the first N heptadecagonal numbers ; Function to find the N - th heptadecagonal number ; Formula to calculate nth heptadecagonal number ; Function to find the sum of the first N heptadecagonal numbers ; Variable to store the sum ; Iterating from 1 to N ; Finding the sum ; Driver code"
Solution in C#:

using System ; class GFG { public static int heptadecagonal_num ( int n ) { return ( ( 15 * n * n ) - 13 * n ) / 2 ; } public static int sum_heptadecagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { summ += heptadecagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum_heptadecagonal_num ( n ) ) ; } }


"Find the sum of the first Nth Icosagonal Numbers | C # program to find the sum of the first N icosagonal number ; Function to calculate the N - th icosagonal number ; Formula to calculate nth icosagonal number & return it ; Function to find the sum of the first N icosagonal numbers ; Variable to store the sum ; Loop to iterate through the first N values and find the sum of first N icosagonal numbers ; Function to get the Icosagonal_num ; Driver code ; Display the sum of first N icosagonal number"
Solution in C#:

using System ; class GFG { public static int Icosagonal_num ( int n ) { return ( 18 * n * n - 16 * n ) / 2 ; } public static int sum_Icosagonal_num ( int n ) { int summ = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { summ += Icosagonal_num ( i ) ; } return summ ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( sum_Icosagonal_num ( n ) ) ; } }


"Find the sum of the first half and second half elements of an array | C # program to count pairs whose sum divisible by ' K ' ; Function to find the sum of the first half elements and second half elements of an array ; Add elements in first half sum ; Add elements in the second half sum ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { public static void sum_of_elements ( int [ ] arr , int n ) { int sumfirst = 0 , sumsecond = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) { sumfirst += arr [ i ] ; } else { sumsecond += arr [ i ] ; } } Console . WriteLine ( " Sum ▁ of ▁ first ▁ half ▁ elements ▁ is ▁ " + sumfirst ) ; Console . WriteLine ( " Sum ▁ of ▁ second ▁ half ▁ elements ▁ is ▁ " + sumsecond ) ; } static public void Main ( ) { int [ ] arr = { 20 , 30 , 60 , 10 , 25 , 15 , 40 } ; int n = arr . Length ; sum_of_elements ( arr , n ) ; } }


"Find the sum of the number of divisors | C # code for above given approach ; To store the number of divisors ; Function to find the number of divisors of all numbers in the range 1 to n ; For every number 1 to n ; Increase divisors count for every number ; Function to find the sum of divisors ; To store sum ; Count the divisors ; Driver code ; Function call"
Solution in C#:

using System ; class GFG { static int N = 100005 ; static int mod = 1000000007 ; static int [ ] cnt = new int [ N ] ; static void Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * i < N ; j ++ ) { cnt [ i * j ] ++ ; } } } static int Sumofdivisors ( int A , int B , int C ) { int sum = 0 ; Divisors ( ) ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int x = i * j * k ; sum += cnt [ x ] ; if ( sum >= mod ) { sum -= mod ; } } } } return sum ; } public static void Main ( String [ ] args ) { int A = 5 , B = 6 , C = 7 ; Console . WriteLine ( Sumofdivisors ( A , B , C ) ) ; } }


"Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | C # program to find the sum of the series 1 + 11 + 111 + 1111 + ... . ; Function for finding summation ; Driver Code"
Solution in C#:

using System ; class GFG { static int summation ( int n ) { int sum ; sum = ( int ) ( Math . Pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } public static void Main ( ) { int n = 5 ; Console . WriteLine ( summation ( n ) ) ; } }


"Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | C # program to find the sum of series ; Function to return required sum ; sum of first series ; sum of second series ; Driver code ; function call to print sum"
Solution in C#:

using System ; class GFG { static int sum ( int x , int y , int n ) { int sum1 = ( int ) ( ( Math . Pow ( x , 2 ) * ( Math . Pow ( x , 2 * n ) - 1 ) ) / ( Math . Pow ( x , 2 ) - 1 ) ) ; int sum2 = ( int ) ( ( x * y * ( Math . Pow ( x , n ) * Math . Pow ( y , n ) - 1 ) ) / ( x * y - 1 ) ) ; return sum1 + sum2 ; } public static void Main ( ) { int x = 2 , y = 2 , n = 2 ; Console . Write ( sum ( x , y , n ) ) ; } }


"Find the super power of a given Number | C # for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver Code"
Solution in C#:

class GFG { static int MAX = 100000 ; static bool [ ] prime = new bool [ 100002 ] ; static void SieveOfEratosthenes ( ) { for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } static int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( ! prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } static void Main ( ) { int n = 256 ; System . Console . WriteLine ( superpower ( n ) ) ; } }


"Find the total Number of Digits in ( N ! ) N | C # program to find the total Number of Digits in ( N ! ) ^ N ; Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 equivalent to floor ( sum ) + 1 ; Driver code"
Solution in C#:

using System ; class GFG { public double CountDigits ( int n ) { if ( n == 1 ) return 1 ; double sum = 0 ; for ( int i = 2 ; i <= n ; ++ i ) { sum += ( ( double ) Math . Log ( i ) / ( double ) Math . Log ( 10 ) ) ; } sum *= n ; return Math . Ceiling ( sum ) ; } public static void Main ( ) { GFG g = new GFG ( ) ; int N = 5 ; Console . WriteLine ( g . CountDigits ( N ) ) ; } }


"Find the total count of numbers up to N digits in a given base B | C # implementation to find the count of natural numbers upto N digits ; Function to return the count of natural numbers upto N digits ; Loop to iterate from 1 to N and calculating number of natural numbers for every ' i ' th digit . ; Driver Code"
Solution in C#:

using System ; using System . Collections . Generic ; class GFG { static int count ( int N , int B ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( int ) ( ( B - 1 ) * Math . Pow ( B , i - 1 ) ) ; } return sum ; } public static void Main ( String [ ] args ) { int N = 2 , B = 10 ; Console . Write ( count ( N , B ) ) ; } }


"Find the total marks obtained according to given marking scheme | C # implementation of above approach ; Function that calculates marks . ; for not attempt score + 0 ; for each correct answer score + 3 ; for each wrong answer score - 1 ; calculate total marks ; Driver code"
Solution in C#:

using System ; class GFG { static int markingScheme ( int N , int [ ] answerKey , int [ ] studentAnswer ) { int positive = 0 , negative = 0 , notattempt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( studentAnswer [ i ] == 0 ) notattempt ++ ; else if ( answerKey [ i ] == studentAnswer [ i ] ) positive ++ ; else if ( answerKey [ i ] != studentAnswer [ i ] ) negative ++ ; } return ( positive * 3 ) + ( negative * - 1 ) ; } static public void Main ( ) { int [ ] answerKey = { 1 , 2 , 3 , 4 , 1 } ; int [ ] studentAnswer = { 1 , 2 , 3 , 4 , 0 } ; int N = answerKey . Length ; int marking_Scheme = markingScheme ( N , answerKey , studentAnswer ) ; Console . WriteLine ( marking_Scheme ) ; } }


"Find the total number of composite factor for a given number | C # implementation of the approach ; Function to return the count of prime factors of n ; Initialise array with 0 ; Stored i value into an array ; Every non - zero value at a [ i ] denotes that i is a factor of n ; Find if i is prime ; If i is a factor of n and i is not prime ; Driver code"
Solution in C#:

using System ; class GFG { static int composite_factors ( int n ) { int count = 0 ; int i , j ; int [ ] a = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = 0 ; } for ( i = 1 ; i <= n ; ++ i ) { if ( n % i == 0 ) { a [ i ] = i ; } } for ( i = 2 ; i <= n ; i ++ ) { j = 2 ; int p = 1 ; while ( j < a [ i ] ) { if ( a [ i ] % j == 0 ) { p = 0 ; break ; } j += 1 ; } if ( p == 0 && a [ i ] != 0 ) { count += 1 ; } } return count ; } public static void Main ( ) { int n = 100 ; Console . WriteLine ( composite_factors ( n ) ) ; } }


"Find the triplet from given Bitwise XOR and Bitwise AND values of all its pairs | C # code for above approach ; Function to find the triplet with given Bitwise XOR and Bitwise AND values of all possible pairs of the triplet ; Stores values of a triplet ; Stores a + b ; Stores a + c ; Stores b + c ; Calculate aSUMb ; Calculate aSUMc ; Calculate bSUMc ; Calculate a ; Calculate b ; Calculate c ; Print a ; Print b ; Print c ; Driver code"
Solution in C#:

using System ; public class GFG { static void findNumbers ( int aXORb , int aANDb , int aXORc , int aANDc , int bXORc , int bANDc ) { int a , b , c ; int aSUMb ; int aSUMc ; int bSUMc ; aSUMb = aXORb + aANDb * 2 ; aSUMc = aXORc + aANDc * 2 ; bSUMc = bXORc + bANDc * 2 ; a = ( aSUMb - bSUMc + aSUMc ) / 2 ; b = aSUMb - a ; c = aSUMc - a ; System . Console . Write ( " a ▁ = ▁ " + a ) ; System . Console . Write ( " , ▁ b ▁ = ▁ " + b ) ; System . Console . Write ( " , ▁ c ▁ = ▁ " + c ) ; } static public void Main ( ) { int aXORb = 30 , aANDb = 0 , aXORc = 20 , aANDc = 10 , bXORc = 10 , bANDc = 20 ; findNumbers ( aXORb , aANDb , aXORc , aANDc , bXORc , bANDc ) ; } }


"Find the two non | C # program for above approach ; This function sets the values of * x and * y to non - repeating elements in an array arr [ ] of size n ; Xor all the elements of the array all the elements occuring twice will cancel out each other remaining two unnique numbers will be xored ; Bitwise & the sum with it ' s ▁ 2' s Complement Bitwise & will give us the sum containing only the rightmost set bit ; sum1 and sum2 will contains 2 unique elements elements initialized with 0 box number xored with 0 is number itself ; Traversing the array again ; Bitwise & the arr [ i ] with the sum Two possibilities either result == 0 or result > 0 ; If result > 0 then arr [ i ] xored with the sum1 ; If result == 0 then arr [ i ] xored with sum2 ; Print the the two unique numbers ; Driver Code"
Solution in C#:

using System ; class GFG { static void UniqueNumbers2 ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = ( sum ^ arr [ i ] ) ; } sum = ( sum & - sum ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( arr [ i ] & sum ) > 0 ) { sum1 = ( sum1 ^ arr [ i ] ) ; } else { sum2 = ( sum2 ^ arr [ i ] ) ; } } Console . WriteLine ( " The ▁ non - repeating ▁ " + " elements ▁ are ▁ " + sum1 + " ▁ and ▁ " + sum2 ) ; } static public void Main ( ) { int [ ] arr = { 2 , 3 , 7 , 9 , 11 , 2 , 3 , 11 } ; int n = arr . Length ; UniqueNumbers2 ( arr , n ) ; } }


"Find the two repeating elements in a given array |  ; Print Repeating function ; driver code"
Solution in C#:

using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int i , j ; Console . Write ( " Repeated ▁ Elements ▁ are ▁ : " ) ; for ( i = 0 ; i < size ; i ++ ) { for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }


"Find the two repeating elements in a given array |  ; printRepeating function ; S is for sum of elements in arr [ ] ; P is for product of elements in arr [ ] ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr [ ] ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code"
Solution in C#:

using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int S = 0 ; int P = 1 ; int x , y ; int D ; int n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * ( n + 1 ) / 2 ; P = P / fact ( n ) ; D = ( int ) Math . Sqrt ( S * S - 4 * P ) ; x = ( D + S ) / 2 ; y = ( S - D ) / 2 ; Console . WriteLine ( " The ▁ two " + " ▁ repeating ▁ elements ▁ are ▁ : " ) ; Console . Write ( x + " ▁ " + y ) ; } static int fact ( int n ) { return ( n == 0 ) ? 1 : n * fact ( n - 1 ) ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }


"Find the two repeating elements in a given array | C # code for Find the two repeating elements in a given array ; Function to print repeating ; Driver program to test the above function"
Solution in C#:

using System ; class GFG { static void printRepeating ( int [ ] arr , int size ) { int i ; Console . Write ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ Math . Abs ( arr [ i ] ) ] > 0 ) arr [ Math . Abs ( arr [ i ] ) ] = - arr [ Math . Abs ( arr [ i ] ) ] ; else Console . Write ( Math . Abs ( arr [ i ] ) + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . Length ; printRepeating ( arr , arr_size ) ; } }