codeparrot/xlcost-text-to-code huggingface dataset:
Product of factors of number | C # program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver Code
using System ; class GFG { public static long M = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } static int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } static long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * ( int ) Math . Sqrt ( n ) ) % M ; return product ; } public static void Main ( ) { int n = 12 ; Console . Write ( multiplyFactors ( n ) ) ; } }
Decimal representation of given binary string is divisible by 10 or not | C # implementation to check whether decimal representation of given binary number is divisible by 10 or not ; function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position  from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver program to test above function
using System ; class GFG { static bool isDivisibleBy10 ( String bin ) { int n = bin . Length ; if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } public static void Main ( ) { String bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Tribonacci Numbers | A space optimized based C # program to print first n Tribinocci numbers . ; Initialize first three numbers ; Loop to add previous three numbers for each number starting from 3 and then assign first , second , third to second , third , and curr to third respectively ; Driver code
using System ; class GFG { static void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; Console . Write ( first + " ▁ " ) ; if ( n > 1 ) Console . Write ( second + " ▁ " ) ; if ( n > 2 ) Console . Write ( second + " ▁ " ) ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; Console . Write ( curr + " ▁ " ) ; } } public static void Main ( ) { int n = 10 ; printTrib ( n ) ; } }
Prime Number of Set Bits in Binary Representation | Set 2 | C # code to find count of numbers having prime number of set bits in their binary representation in the range [ L , R ] ; Function to create an array of prime numbers upto number ' n ' ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as false . A value in prime [ i ] will finally be true if i is Not a prime , else false . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself .
using System ; using System . Linq ; using System . Collections ; class GFG { static ArrayList SieveOfEratosthenes ( int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) for ( int i = p * 2 ; i < n + 1 ; i += p ) prime [ i ] = true ; } ArrayList lis = new ArrayList ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) lis . Add ( p ) ; return lis ; } static int setBits ( int n ) { return ( int ) Convert . ToString ( n , 2 ) . Count ( c => c == '1' ) ; } public static void Main ( ) { int x = 4 , y = 8 ; int count = 0 ; ArrayList primeArr = new ArrayList ( ) ; primeArr = SieveOfEratosthenes ( Convert . ToInt32 ( Math . Ceiling ( Math . Log ( y , 2.0 ) ) ) ) ; for ( int i = x ; i < y + 1 ; i ++ ) { int temp = setBits ( i ) ; if ( primeArr . Contains ( temp ) ) count += 1 ; } Console . WriteLine ( count ) ; } }
Count trailing zeroes present in binary representation of a given number using XOR | C # implementation of the above approach ; Function to print count of trailing zeroes present in binary representation of N ; Stores XOR of N and ( N - 1 ) ; Return count of set bits in res ; Driver Code ; Function call to print the count of trailing zeroes in the binary representation of N
using System ; public class GFG { public static int countTrailingZeroes ( int N ) { int res = ( int ) Math . Log ( N ^ ( N - 1 ) , 2.0 ) ; if ( res >= 0 ) return res ; else return 0 ; } static public void Main ( ) { int N = 12 ; Console . WriteLine ( countTrailingZeroes ( N ) ) ; } }
Maximum sum of Bitwise XOR of elements with their respective positions in a permutation of size N | C # program for the above approach ; Function to calculate the score ; Stores the possible score for the current permutation ; Traverse the permutation array ; Return the readonly score ; Function to generate all the possible permutation and get the max score ; If [ ] arr length is equal to N process the permutation ; Generating the permutations ; If the current element is chosen ; Mark the current element as true ; Recursively call for next possible permutation ; Backtracking ; Return the ans ; Driver Code ; Stores the permutation ; To display the result
using System ; using System . Collections . Generic ; public class GFG { static int calcScr ( List < int > arr ) { int ans = 0 ; for ( int i = 0 ; i < arr . Count ; i ++ ) ans += ( i ^ arr [ i ] ) ; return ans ; } static int getMax ( List < int > arr , int ans , List < Boolean > chosen , int N ) { if ( arr . Count == N ) { ans = Math . Max ( ans , calcScr ( arr ) ) ; return ans ; } for ( int i = 0 ; i < N ; i ++ ) { if ( chosen [ i ] ) continue ; chosen [ i ] = true ; arr . Add ( i ) ; ans = getMax ( arr , ans , chosen , N ) ; chosen [ i ] = false ; arr . Remove ( arr . Count - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int N = 2 ; List < int > arr = new List < int > ( ) ; int ans = - 1 ; List < bool > chosen = new List < bool > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) chosen . Add ( false ) ; ans = getMax ( arr , ans , chosen , N ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Additive Congruence method for generating Pseudo Random Numbers | C # implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the additive congruential method ; Driver Code ; Seed value ; Modulus parameter ; Increment term ; Number of Random numbers to be generated ; To store random numbers ; Function call ; Print the generated random numbers
using System ; class GFG { static void additiveCongruentialMethod ( int Xo , int m , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } public static void Main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; int [ ] randomNums = new int [ noOfRandomNums ] ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { Console . Write ( randomNums [ i ] + " ▁ " ) ; } } }
Second decagonal numbers | C # program for the above approach ; Function to find N - th term in the series ; Driver code
using System ; class GFG { static void findNthTerm ( int n ) { Console . WriteLine ( n * ( 4 * n + 3 ) ) ; } public static void Main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
65537 | C # program for above approach ; Function to find the nth 65537 - gon Number ; Driver code
using System ; class GFG { static int gonNum65537 ( int n ) { return ( 65535 * n * n - 65533 * n ) / 2 ; } public static void Main ( String [ ] args ) { int n = 3 ; Console . Write ( gonNum65537 ( n ) ) ; } }
Hexacontatetragon numbers | C # program to find N - th Hexacontatetragon number ; Function to find the nth Hexacontatetragon number ; Driver code
using System ; class GFG { static int HexacontatetragonNum ( int n ) { return ( 62 * n * n - 60 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( HexacontatetragonNum ( n ) ) ; } }
Icosikaipentagon Number | C # program for the above approach ; Finding the nth chiliagon number ; Driver code
using System ; class GFG { static int Icosikaipentagon ( int n ) { return ( 23 * n * n - 21 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( "3rd ▁ Icosikaipentagon ▁ Number ▁ is ▁ = ▁ " + Icosikaipentagon ( n ) ) ; } }
Chiliagon Number | C # program for the above approach ; Finding the nth chiliagon number ; Driver code
using System ; class GFG { static int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } public static void Main ( ) { int n = 3 ; Console . Write ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " + chiliagonNum ( n ) ) ; } }
Pentacontagon number | C # program for above approach ; Finding the nth pentacontagon number ; Driver code
using System ; class GFG { static int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } public static void Main ( string [ ] args ) { int n = 3 ; Console . Write ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " + pentacontagonNum ( n ) ) ; } }
Array value by repeatedly replacing max 2 elements with their absolute difference | C # program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; Function that return last value of array ; Build a binary max_heap ; For max 2 elements ; Iterate until queue is not empty ; If only 1 element is left ; Return the last remaining value ; Check that difference is non zero ; Finally return 0 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int lastElement ( int [ ] arr ) { Queue < int > pq = new Queue < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) pq . Enqueue ( arr [ i ] ) ; int m1 , m2 ; while ( pq . Contains ( 0 ) ) { if ( pq . Count == 1 ) { return pq . Peek ( ) ; } m1 = pq . Dequeue ( ) ; m2 = pq . Peek ( ) ; if ( m1 != m2 ) pq . Enqueue ( m1 - m2 ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 4 , 1 , 8 , 1 , 1 } ; Console . WriteLine ( lastElement ( arr ) ) ; } }
Number formed by adding product of its max and min digit K times | C # program for the above approach ; Function to find the formed number ; K -- ; M ( 1 ) = N ; Check if minimum digit is 0 ; Function that returns the product of maximum and minimum digit of N number . ; Finds the last digit . ; Moves to next digit ; Driver code
using System ; class GFG { public static int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K != 0 ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } static int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . Max ( r , largest ) ; smallest = Math . Min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } public static void Main ( String [ ] args ) { int N = 487 , K = 100000000 ; Console . WriteLine ( formed_no ( N , K ) ) ; } }
Logarithm tricks for Competitive Programming | C # implementation count the number of digits in a number ; Function to count the number of digits in a number ; Driver Code
using System ; class GFG { static int countDigit ( double n ) { return ( ( int ) Math . Floor ( Math . Log10 ( n ) + 1 ) ) ; } public static void Main ( ) { double N = 80 ; Console . Write ( countDigit ( N ) ) ; } }
Program to find the sum of the series 1 + x + x ^ 2 + x ^ 3 + . . + x ^ n | C # implementation to find the sum of series 1 + x ^ 2 + x ^ 3 + ... . + x ^ n ; C # code to print the sum of the given series ; First Term ; Loop to print the N terms of the series and compute their sum ; Driver Code
using System ; class GFG { static double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; Console . Write ( "1 ▁ " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; Console . Write ( multi ) ; Console . Write ( " ▁ " ) ; multi = multi * x ; } Console . WriteLine ( ) ; return total ; } public static void Main ( String [ ] args ) { int x = 2 ; int n = 5 ; Console . Write ( " { 0 : F2 } " , sum ( x , 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
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 all Autobiographical Numbers with given number of digits | C # implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . Pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code
using System ; class autobio { public static bool isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = num . ToString ( ) ; for ( i = 0 ; i < autoStr . Length ; i ++ ) { index = Int32 . Parse ( autoStr [ i ] + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . Length ; j ++ ) { number = Int32 . Parse ( autoStr [ j ] + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . Pow ( 10.0 , n - 1 ) ; high = Math . Pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; Console . Write ( i + " , ▁ " ) ; } if ( flag == 0 ) Console . WriteLine ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void Main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }
Check whether the number can be made palindromic after adding K | C # program to check whether the number can be made palindrome number after adding K ; Function to check whether a number is a palindrome or not ; Convert num to string ; Comparing kth character from the beginning and N - kth character from the end . If all the characters match , then the number is a palindrome ; If all the above conditions satisfy , it means that the number is a palindrome ; Driver code
using System ; class GFG { static void checkPalindrome ( int num ) { String str = num . ToString ( ) ; int l = 0 , r = str . Length - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { Console . Write ( " No " ) ; return ; } l ++ ; r -- ; } Console . Write ( " Yes " ) ; return ; } public static void Main ( String [ ] args ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; } }
Count of subsets with sum equal to X using Recursion | C # program to print the count of subsets with sum equal to the given value X ; Recursive function to return the count of subsets with sum equal to the given value ; The recursion is stopped at N - th level where all the subsets of the given array have been checked ; Incrementing the count if sum is equal to 0 and returning the count ; Recursively calling the function for two cases Either the element can be counted in the subset If the element is counted , then the remaining sum to be checked is sum - the selected element If the element is not included , then the remaining sum to be checked is the total sum ; Driver code
using System ; class GFG { static int subsetSum ( int [ ] arr , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . Length ; Console . Write ( subsetSum ( arr , n , 0 , sum , 0 ) ) ; } }
Distinct Prime Factors of an Array | C # implementation of the above approach ; Function to return an array of prime numbers upto n using Sieve of Eratosthenes ; Function to return distinct prime factors from the given array ; Creating an empty array to store distinct prime factors ; Iterating through all the prime numbers and check if any of the prime numbers is a factor of the given input array ; Driver code ; Finding prime numbers upto 10000 using Sieve of Eratosthenes
using System ; using System . Collections . Generic ; class GFG { static List < int > sieve ( int n ) { List < int > prime = new List < int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime . Add ( 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime [ p ] == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime [ i ] = 1 ; } p += 1 ; } List < int > allPrimes = new List < int > ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] == 0 ) allPrimes . Add ( i ) ; } return allPrimes ; } static List < int > distPrime ( List < int > arr , List < int > allPrimes ) { List < int > list1 = new List < int > ( ) ; for ( int i = 0 ; i < allPrimes . Count ; i ++ ) { for ( int j = 0 ; j < arr . Count ; j ++ ) { if ( arr [ j ] % allPrimes [ i ] == 0 ) { list1 . Add ( allPrimes [ i ] ) ; break ; } } } return list1 ; } public static void Main ( string [ ] args ) { List < int > allPrimes = new List < int > ( sieve ( 10000 ) ) ; List < int > arr = new List < int > ( ) ; arr . Add ( 15 ) ; arr . Add ( 30 ) ; arr . Add ( 60 ) ; List < int > ans = new List < int > ( distPrime ( arr , allPrimes ) ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; Console . Write ( " ] " ) ; } }
Sum of the count of number of adjacent squares in an M X N grid | C # implementation of the above approach ; function to calculate the sum of all cells adjacent value ; Driver Code
using System ; class GFG { static int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } public static void Main ( String [ ] args ) { int m = 3 , n = 2 ; Console . WriteLine ( sum ( m , n ) ) ; } }
Count pairs in an array such that the absolute difference between them is ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¡¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â € š ¬ Ã …¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒ ¢ Ã ¢ â ‚¬ Å ¾¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬ Ãƒ ¢ Ã ¢ â € š ¬ Ã ¢ â € ž ¢¢ ÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¦¡¬°¥ K | C # implementation of the approach ; Function to return the count of required pairs ; Sort the given array ; To store the required count ; Update j such that it is always > i ; Find the first element arr [ j ] such that ( arr [ j ] - arr [ i ] ) >= K This is because after this element , all the elements will have absolute difference with arr [ i ] >= k and the count of valid pairs will be ( n - j ) ; Update the count of valid pairs ; Get to the next element to repeat the steps ; Return the count ; Driver code
using System ; class GFG { static int count ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; Console . Write ( count ( arr , n , k ) ) ; } }
Find the volume of rectangular right wedge | C # implementation of the approach ; Function to return the volume of the rectangular right wedge ; Driver code
using System ; class GFG { static double volumeRec ( double a , double b , double e , double h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } public static void Main ( ) { double a = 2 , b = 5 , e = 5 , h = 6 ; Console . WriteLine ( " Volume ▁ = ▁ " + volumeRec ( a , b , e , h ) ) ; } }
Count squares with odd side length in Chessboard | C # implementation of the approach ; Function to return the count of odd length squares possible ; To store the required count ; For all odd values of i ; Add the count of possible squares of length i ; Return the required count ; Driver code
using System ; class GFG { static int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } public static void Main ( ) { int N = 8 ; Console . WriteLine ( count_square ( N ) ) ; } }
Count of elements whose absolute difference with the sum of all the other elements is greater than k | C # implementation of the approach ; Function to return the number of anomalies ; To store the count of anomalies ; To store the sum of the array elements ; Find the sum of the array elements ; Count the anomalies ; Driver code
using System ; class GFG { static int countAnomalies ( int [ ] arr , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . Abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 } ; int n = arr . Length ; int k = 1 ; Console . WriteLine ( countAnomalies ( arr , n , k ) ) ; } }
Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | C # implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Driver code ; Function call ; Required answer
using System ; class GFG { static int N = 100005 ; static int [ ] d = new int [ N ] ; static int [ ] pre = new int [ N ] ; static void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } public static void Main ( String [ ] args ) { Positive_Divisors ( ) ; int n = 15 ; Console . WriteLine ( pre [ n ] ) ; } }