codeparrot/xlcost-text-to-code huggingface dataset:
Program to find sum of the given sequence | C # program to find the sum of the given sequence ; function to find moudulo inverse under 10 ^ 9 + 7 ; Function to find the sum of the given sequence ; Driver code
using System ; class gfg { public long MOD = 1000000007 ; public long modInv ( long x ) { long n = MOD - 2 ; long result = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) result = result * x % MOD ; x = x * x % MOD ; n = n / 2 ; } return result ; } public long getSum ( long n , long k ) { long ans = 1 ; for ( long i = n + 1 ; i > n - k ; i -- ) ans = ans * i % MOD ; ans = ans * modInv ( k + 1 ) % MOD ; return ans ; } } class geek { public static int Main ( ) { gfg g = new gfg ( ) ; long n = 3 , k = 2 ; Console . WriteLine ( g . getSum ( n , k ) ) ; return 0 ; } }
Triplet with no element divisible by 3 and sum N | C # program to print a , b and c such that a + b + c = N ; Function to print a , b and c ; check if n - 2 is divisible by 3 or not ; Driver Code
class GFG { static void printCombination ( int n ) { System . Console . Write ( 1 + " ▁ " ) ; if ( ( n - 2 ) % 3 == 0 ) System . Console . Write ( 2 + " ▁ " + ( n - 3 ) ) ; else System . Console . Write ( 1 + " ▁ " + ( n - 2 ) ) ; } static void Main ( ) { int n = 233 ; printCombination ( n ) ; } }
Pairs with GCD equal to one in the given range | C # program to print all pairs ; Function to print all pairs ; check if even ; We can print all adjacent pairs for ( int i = l ; i < r ; i += 2 ) { System . out . print ( " { " + i + " , ▁ " + i + 1 + " } , ▁ " ) ; } ; Driver Code
using System ; class GFG { static bool checkPairs ( int l , int r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } static public void Main ( ) { int l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Check if a number with even number of digits is palindrome or not | C # program to find number is palindrome or not without using any extra space ; Function to check if the number is palindrome ; if divisible by 11 then true ; if not divisible by 11 ; Driver code
using System ; class GFG { static bool isPalindrome ( int n ) { if ( n % 11 == 0 ) { return true ; } return false ; } public static void Main ( ) { Console . Write ( isPalindrome ( 123321 ) ? " Palindrome " : " Not ▁ Palindrome " ) ; } }
Count number of triplets with product equal to given number with duplicates allowed | C # program for above implementation ; The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets
using System ; class GFG { public static void Main ( ) { int target = 93 ; int [ ] arr = { 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 } ; int length = arr . Length ; int totalCount = 0 ; for ( int i = 0 ; i < length - 2 ; i ++ ) { if ( target % arr [ i ] == 0 ) { for ( int j = i + 1 ; j < length - 1 ; j ++ ) { if ( target % ( arr [ i ] * arr [ j ] ) == 0 ) { int toFind = target / ( arr [ i ] * arr [ j ] ) ; for ( int k = j + 1 ; k < length ; k ++ ) { if ( arr [ k ] == toFind ) { totalCount ++ ; } } } } } } Console . Write ( " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " + totalCount ) ; } }
Number of Permutations such that no Three Terms forms Increasing Subsequence | C # program to find the nth catalan number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code
using System ; class GFG { static long binomialCoeff ( long n , long k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( long n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . WriteLine ( catalan ( n ) ) ; } }
Fascinating Number | C # program to implement fascinating number ; function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Driver code ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not
using System ; class GFG { public static bool isFascinating ( int num ) { int [ ] freq = new int [ 10 ] ; String val = " " + num . ToString ( ) + ( num * 2 ) . ToString ( ) + ( num * 3 ) . ToString ( ) ; for ( int i = 0 ; i < val . Length ; i ++ ) { int digit = val [ i ] - '0' ; if ( freq [ digit ] && digit != 0 > 0 ) return false ; else freq [ digit ] ++ ; } for ( int i = 1 ; i < freq . Length ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } static void Main ( ) { int num = 192 ; if ( num < 100 ) Console . WriteLine ( " No " ) ; else { bool ans = isFascinating ( num ) ; if ( ans ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } } }
Count ways to distribute m items among n people | C # code for calculating number of ways to distribute m mangoes amongst n people where all mangoes and people are identical ; function used to generate binomial coefficient time complexity O ( m ) ; helper function for generating no of ways to distribute m mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; Driver Code ; m represents number of mangoes n represents number of people
using System ; class GFG { public static int binomial_coefficient ( int n , int m ) { int res = 1 ; if ( m > n - m ) m = n - m ; for ( int i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static int calculate_ways ( int m , int n ) { if ( m < n ) { return 0 ; } int ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } public static void Main ( ) { int m = 7 , n = 5 ; int result = calculate_ways ( m , n ) ; Console . WriteLine ( result . ToString ( ) ) ; } }
Queries to count the number of unordered co | C # program to find number of unordered coprime pairs of integers from 1 to N ; to store euler 's totient function ; to store required answer ; Computes and prints totient of all numbers smaller than or equal to N . ; Initialise the phi [ ] with 1 ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; Driver code ; function call
using System ; class GFG { static int N = 100005 ; static int [ ] phi ; static int [ ] S ; static void computeTotient ( ) { for ( int i = 1 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void CoPrimes ( ) { computeTotient ( ) ; for ( int i = 1 ; i < N ; i ++ ) S [ i ] = S [ i - 1 ] + phi [ i ] ; } public static void Main ( ) { phi = new int [ N ] ; S = new int [ N ] ; CoPrimes ( ) ; int [ ] q = { 3 , 4 } ; int n = q . Length ; for ( int i = 0 ; i < n ; i ++ ) Console . WriteLine ( " Number ▁ of ▁ unordered ▁ coprime STRNEWLINE " + " pairs ▁ of ▁ integers ▁ from ▁ 1 ▁ to ▁ " + q [ i ] + " ▁ are ▁ " + S [ q [ i ] ] ) ; } }
N | C # program to find the N - th term in 1 , 11 , 55 , 239 , 991 , ... . ; Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code
using System ; class GFG { static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base_ = 1 ; int len = num . Length ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base_ ; base_ = base_ * 2 ; } return dec_value ; } static int numberSequence ( int n ) { if ( n == 1 ) return 1 ; String s = " " ; for ( int i = 1 ; i < n ; i ++ ) s += '1' ; s += '0' ; for ( int i = 1 ; i <= n ; i ++ ) s += '1' ; int num = binaryToDecimal ( s ) ; return num ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( numberSequence ( n ) ) ; } }
N | C # program to find the N - th term in 1 , 11 , 55 , 239 , 991 , ... . ; Function to find the N - th term ; calculates the N - th term ; Driver Code
using System ; class GFG { static int numberSequence ( int n ) { int num = ( int ) ( Math . Pow ( 4 , n ) - Math . Pow ( 2 , n ) ) - 1 ; return num ; } public static void Main ( ) { int n = 4 ; Console . WriteLine ( numberSequence ( n ) ) ; } }
Alternate Primes till N | C # program to print all primes smaller than or equal to n using Naive approach . ; Function for checking number is prime or not ; if flag = 0 then number is prime and return 1 otherwise return 0 ; Function for printing alternate prime number ; counter is initialize with 0 ; looping through 2 to n - 1 ; function calling along with if condition ; if counter is multiple of 2 then only print prime number ; Driver code ; Function calling
using System ; class GFG { static int prime ( int num ) { int i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } static void print_alternate_prime ( int n ) { int counter = 0 ; for ( int num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) Console . Write ( num + " ▁ " ) ; counter ++ ; } } } public static void Main ( ) { int n = 15 ; Console . Write ( " Following ▁ are ▁ the ▁ alternate ▁ " + " prime ▁ number ▁ smaller ▁ than ▁ " + " or ▁ equal ▁ to ▁ " + n + " STRNEWLINE " ) ; print_alternate_prime ( n ) ; } }
Alternate Primes till N | C # program to print all primes smaller than or equal to n using Sieve of Eratosthenes ; Create a bool 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 ; Print all prime numbers ; for next prime to get printed ; Driver Code
using System ; class GFG { static void SieveOfEratosthenes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < prime . Length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } bool flag = true ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { if ( flag ) { Console . Write ( p + " ▁ " ) ; flag = false ; } else { flag = true ; } } } } public static void Main ( ) { int n = 15 ; Console . Write ( " Following ▁ are ▁ the ▁ alternate " + " ▁ prime ▁ numbers ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + n + " STRNEWLINE " ) ; SieveOfEratosthenes ( n ) ; } }
Find maximum among x ^ ( y ^ 2 ) or y ^ ( x ^ 2 ) where x and y are given | C # program to find the greater value ; Function to find maximum ; Case 1 ; Case 2 ; Driver Code
using System ; class GFG { static bool findGreater ( int x , int y ) { if ( x > y ) { return false ; } else { return true ; } } public static void Main ( ) { int x = 4 ; int y = 9 ; if ( findGreater ( x , y ) ) Console . WriteLine ( "1" ) ; else Console . WriteLine ( "2" ) ; } }
Maximum profit after buying and selling stocks with transaction fees | C # implementation of above approach ; b [ 0 ] will contain the maximum profit ; b [ 1 ] will contain the day on which we are getting the maximum profit ; here finding the max profit ; if we get less then or equal to zero it means we are not getting the profit ; check if sum is greater then maximum then store the new maximum ; Driver code
using System ; class GFG { static int max_profit ( int [ ] a , int [ ] b , int n , int fee ) { int i , j , profit ; int l , r , diff_day = 1 , sum = 0 ; b [ 0 ] = 0 ; b [ 1 ] = diff_day ; for ( i = 1 ; i < n ; i ++ ) { l = 0 ; r = diff_day ; sum = 0 ; for ( j = n - 1 ; j >= i ; j -- ) { profit = ( a [ r ] - a [ l ] ) - fee ; if ( profit > 0 ) { sum = sum + profit ; } l ++ ; r ++ ; } if ( b [ 0 ] < sum ) { b [ 0 ] = sum ; b [ 1 ] = diff_day ; } diff_day ++ ; } return 0 ; } static public void Main ( ) { int [ ] arr = { 6 , 1 , 7 , 2 , 8 , 4 } ; int n = arr . Length ; int [ ] b = new int [ 2 ] ; int tranFee = 2 ; max_profit ( arr , b , n , tranFee ) ; Console . WriteLine ( b [ 0 ] + " , ▁ " + b [ 1 ] ) ; } }
Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | C # program to find minimum number of trials in worst case . ; Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . If the sum becomes more than K ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver Code
using System ; class GFG { static int binomialCoeff ( int x , int n , int k ) { int sum = 0 , term = 1 ; for ( int i = 1 ; i <= n && sum < k ; ++ i ) { term *= x - i + 1 ; term /= i ; sum += term ; } return sum ; } static int minTrials ( int n , int k ) { int low = 1 , high = k ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( binomialCoeff ( mid , n , k ) < k ) low = mid + 1 ; else high = mid ; } return low ; } public static void Main ( ) { Console . WriteLine ( minTrials ( 2 , 10 ) ) ; } }
Find next palindrome prime | C # program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code
using System ; using System . Text ; using System . Collections ; class Geeks { static bool isPrime ( int num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( int i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } static int primePalindrome ( int N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( int x = 1 ; x < 100000 ; ++ x ) { string s = x . ToString ( ) ; char [ ] buffer = s . ToCharArray ( ) ; Array . Reverse ( buffer ) ; int y = Int32 . Parse ( s + new string ( buffer ) . Substring ( 1 ) ) ; if ( y >= N && isPrime ( y ) == true ) return y ; } return - 1 ; } public static void Main ( ) { Console . WriteLine ( primePalindrome ( 112 ) ) ; } }
Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | C # program to find the numbers of values that satisfy the equation ; This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible
using System ; using System . Collections . Generic ; class GFG { static int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } static void value ( int a , int b , int c ) { int no , x = 0 , q = 0 , w = 0 ; List < int > v = new List < int > ( ) ; for ( int i = 1 ; i < 82 ; i ++ ) { no = ( int ) Math . Pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . Add ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . Count ; i ++ ) { Console . Write ( v [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ; } }
Cunningham chain | C # program for cunningham chain Function to print the series of second kind ; Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code
using System ; class GFG { static void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( true ) { flag = 1 ; x = ( int ) ( Math . Pow ( 2 , i ) ) ; p1 = x * p0 - ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; Console . Write ( p1 + " ▁ " ) ; i ++ ; } } static void Main ( ) { int p0 = 19 ; print ( p0 ) ; } }
Count pairs with Bitwise AND as ODD number | C # program to count pairs with Odd AND ; Count total odd numbers in ; return count of even pair ; Driver Code ; calling function findOddPair and print number of odd pair
using System ; class GFG { public static int findOddPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 == 1 ) ) { count ++ ; } } return count * ( count - 1 ) / 2 ; } public static void Main ( string [ ] args ) { int [ ] a = new int [ ] { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findOddPair ( a , n ) ) ; } }
Surd number | C # program to find if a number is Surds or not ; Returns true if x is Surd number ; Try all powers of i ; Driver Code
using System ; class GFG { static bool isSurd ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int j = i ; while ( j < n ) j = j * i ; if ( j == n ) return false ; } return true ; } public static void Main ( ) { int n = 15 ; if ( isSurd ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Program to find last two digits of 2 ^ n | C # code to find last 2 digits of 2 ^ n ; Find the first digit ; Get the last digit from the number ; Remove last digit from number ; Get the last digit from the number ( last second of num ) ; Take last digit to ten 's position i.e.  last second digit ; Add the value of ones and tens to make it complete 2 digit number ; return the first digit ; Driver code ; pow function used
using System ; class GFG { static long LastTwoDigit ( long num ) { long one = num % 10 ; num /= 10 ; long tens = num % 10 ; tens *= 10 ; num = tens + one ; return num ; } public static void Main ( String [ ] args ) { int n = 10 ; long num = 1 ; num = ( long ) Math . Pow ( 2 , n ) ; Console . WriteLine ( " Last ▁ 2 ▁ digits ▁ of ▁ 2 ^ 10 ▁ = ▁ " + LastTwoDigit ( num ) ) ; } }
Program to find last two digits of 2 ^ n | C # code to find last 2 digits of 2 ^ n ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; C # function to calculate number of digits in x ; C # function to print last 2 digits of 2 ^ n ; Generating 10 ^ 2 ; Calling modular exponentiation ; Printing leftmost zeros . Since ( 2 ^ n ) % 2 can have digits less then 2. In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Code
using System ; class GFG { static int power ( long x , long y , long p ) { while ( y > 0 ) { long r = y & 1 ; if ( r == 1 ) res = ( res * ( int ) x ) % ( int ) p ; x = ( x * x ) % p ; } return res ; } static int numberOfDigits ( int x ) { int i = 0 ; while ( x != 0 ) { x /= 10 ; i ++ ; } return i ; } static void LastTwoDigit ( int n ) { Console . Write ( " Last ▁ " + 2 + " ▁ digits ▁ of ▁ " + 2 + " ^ " ) ; Console . Write ( n + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( int i = 0 ; i < ( 2 - numberOfDigits ( temp ) ) ; i ++ ) Console . Write ( 0 + " ▁ " ) ; if ( temp != 0 ) Console . Write ( temp ) ; } public static void Main ( ) { int n = 72 ; LastTwoDigit ( n ) ; } }
Find gcd ( a ^ n , c ) where a , n and c can vary from 1 to 10 ^ 9 | C # program to find GCD of a ^ n and b . ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; x = x % p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Finds GCD of a and b ; Finds GCD of a ^ n and c ; check if c is a divisor of a ; First compute ( a ^ n ) % c ; Now simply return GCD of modulo power and c . ; Driver code
using System ; class GFG { static long modPower ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long gcdPow ( long a , long n , long c ) { if ( a % c == 0 ) return c ; long modexpo = modPower ( a , n , c ) ; return gcd ( modexpo , c ) ; } public static void Main ( ) { long a = 10248585 , n = 1000000 , c = 12564 ; Console . Write ( gcdPow ( a , n , c ) ) ; } }
Number of sub arrays with odd sum | C # code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code
using System ; class GFG { static int countOddSum ( int [ ] ar , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int val = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { val = val + ar [ j ] ; if ( val % 2 != 0 ) result ++ ; } } return ( result ) ; } public static void Main ( ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ odd ▁ sum ▁ is ▁ " ) ; Console . WriteLine ( countOddSum ( ar , n ) ) ; } }
Number of sub arrays with odd sum | C # code to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by an even - odd pair ; Driver code
using System ; class GFG { static int countOddSum ( int [ ] ar , int n ) { int [ ] temp = { 1 , 0 } ; int result = 0 , val = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { val = ( ( val + ar [ i ] ) % 2 + 2 ) % 2 ; temp [ val ] ++ ; } result = temp [ 0 ] * temp [ 1 ] ; return ( result ) ; } public static void Main ( ) { int [ ] ar = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . Length ; Console . Write ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ odd ▁ sum ▁ is ▁ " + countOddSum ( ar , n ) ) ; } }
Program to print factors of a number in pairs | C # program to print prime factors in pairs . ; Driver code
using System ; public class GEE { static void printPFsInPairs ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 ) Console . Write ( i + " * " + n / i + " STRNEWLINE " ) ; } public static void Main ( ) { int n = 24 ; printPFsInPairs ( n ) ; } }
Sum of elements in range L | C # program to find the sum between L - R range by creating the array Naive Approach ; Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code
using System ; class GFG { static int rangesum ( int n , int l , int r ) { int [ ] arr = new int [ n ] ; int c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } int sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void Main ( ) { int n = 12 ; int l = 1 , r = 11 ; Console . WriteLine ( rangesum ( n , l , r ) ) ; } }