codeparrot/xlcost-text-to-code huggingface dataset:
Probability that a random pair chosen from an array ( a [ i ] , a [ j ] ) has the maximum sum | C # implementation of above approach ; Function to return the probability of getting the maximum pair sum when a random pair is chosen from the given array ; Initialize the maximum sum , its count and the count of total pairs ; For every single pair ; Get the sum of the current pair ; If the sum is equal to the current maximum sum so far ; Increment its count ; If the sum is greater than the current maximum ; Update the current maximum and re - initialize the count to 1 ; Find the required probability ; Driver code
using System ; class GFG { static float findProb ( int [ ] arr , int n ) { long maxSum = int . MinValue , maxCount = 0 , totalPairs = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( sum == maxSum ) { maxCount ++ ; } else if ( sum > maxSum ) { maxSum = sum ; maxCount = 1 ; } totalPairs ++ ; } } float prob = ( float ) maxCount / ( float ) totalPairs ; return prob ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = arr . Length ; Console . WriteLine ( findProb ( arr , n ) ) ; } }
Maximum count of common divisors of A and B such that all are co | C # implementation of the above approach ; Function to return the count of common factors of a and b such that all the elements are co - prime to one another ; GCD of a and b ; Include 1 initially ; Find all the prime factors of the gcd ; If gcd is prime ; Return the required answer ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxCommonFactors ( int a , int b ) { int __gcd = gcd ( a , b ) ; int ans = 1 ; for ( int i = 2 ; i * i <= __gcd ; i ++ ) { if ( __gcd % i == 0 ) { ans ++ ; while ( __gcd % i == 0 ) __gcd /= i ; } } if ( __gcd != 1 ) ans ++ ; return ans ; } public static void Main ( String [ ] args ) { int a = 12 , b = 18 ; Console . WriteLine ( maxCommonFactors ( a , b ) ) ; } }
Find the day number in the current year for the given date | C # implementation of the approach ; Function to return the day number of the year for the given date ; Extract the year , month and the day from the date string ; If current year is a leap year and the date given is after the 28 th of February then it must include the 29 th February ; Add the days in the previous months ; Driver code
using System ; class GFG { static int [ ] days = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; static int dayOfYear ( string date ) { int year = Int32 . Parse ( date . Substring ( 0 , 4 ) ) ; int month = Int32 . Parse ( date . Substring ( 5 , 2 ) ) ; int day = Int32 . Parse ( date . Substring ( 8 ) ) ; if ( month > 2 && year % 4 == 0 && ( year % 100 != 0 year % 400 == 0 ) ) { ++ day ; } while ( -- month > 0 ) { day = day + days [ month - 1 ] ; } return day ; } public static void Main ( ) { String date = "2019-01-09" ; Console . WriteLine ( dayOfYear ( date ) ) ; } }
Find the number of cells in the table contains X | C # program to find number of cells in the table contains X ; Function to find number of cells in the table contains X ; Driver code ; Function call
using System ; class GFG { static int Cells ( int n , int x ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( x % i == 0 && x / i <= n ) ans ++ ; return ans ; } public static void Main ( ) { int n = 6 , x = 12 ; Console . WriteLine ( Cells ( n , x ) ) ; } }
Smallest power of 4 greater than or equal to N | C # implementation of above approach ; Function to return the smallest power of 4 greater than or equal to n ; If n is itself is a power of 4 then return n ; Driver code
using System ; class GFG { static int nextPowerOfFour ( int n ) { int x = ( int ) Math . Floor ( Math . Sqrt ( Math . Sqrt ( n ) ) ) ; if ( Math . Pow ( x , 4 ) == n ) return n ; else { x = x + 1 ; return ( int ) Math . Pow ( x , 4 ) ; } } public static void Main ( ) { int n = 122 ; Console . WriteLine ( nextPowerOfFour ( n ) ) ; } }
Minimum operations required to convert X to Y by multiplying X with the given co | C # implementation of the approach ; Function to return the minimum operations required ; Not possible ; To store the greatest power of p that divides d ; While divible by p ; To store the greatest power of q that divides d ; While divible by q ; If d > 1 ; Since , d = p ^ a * q ^ b ; Driver code
using System ; class GFG { static int minOperations ( int x , int y , int p , int q ) { if ( y % x != 0 ) return - 1 ; int d = y / x ; int a = 0 ; while ( d % p == 0 ) { d /= p ; a ++ ; } int b = 0 ; while ( d % q == 0 ) { d /= q ; b ++ ; } if ( d != 1 ) return - 1 ; return ( a + b ) ; } public static void Main ( ) { int x = 12 , y = 2592 , p = 2 , q = 3 ; Console . Write ( minOperations ( x , y , p , q ) ) ; } }
Number of Quadruples with GCD equal to K | C # implementation of the approach ; Function to calculate NC4 ; Base case to calculate NC4 ; Function to return the count of required quadruples using Inclusion Exclusion ; Effective N ; Iterate over 2 to M ; Number of divisors of i till M ; Count stores the number of prime divisors occurring exactly once ; To prevent repetition of prime divisors ; If repetition of prime divisors present ignore this number ; If prime divisor count is odd subtract it from answer else add ; Driver code
using System ; class GFG { static int nCr ( int n ) { if ( n < 4 ) return 0 ; int answer = n * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ; answer /= 24 ; return answer ; } static int countQuadruples ( int N , int K ) { int M = N / K ; int answer = nCr ( M ) ; for ( int i = 2 ; i < M ; i ++ ) { int j = i ; int temp2 = M / i ; int count = 0 ; int check = 0 ; int temp = j ; while ( j % 2 == 0 ) { count ++ ; j /= 2 ; if ( count >= 2 ) break ; } if ( count >= 2 ) { check = 1 ; } for ( int k = 3 ; k <= Math . Sqrt ( temp ) ; k += 2 ) { int cnt = 0 ; while ( j % k == 0 ) { cnt ++ ; j /= k ; if ( cnt >= 2 ) break ; } if ( cnt >= 2 ) { check = 1 ; break ; } else if ( cnt == 1 ) count ++ ; } if ( j > 2 ) { count ++ ; } if ( check == 1 ) continue ; else { if ( count % 2 == 1 ) { answer -= nCr ( temp2 ) ; } else { answer += nCr ( temp2 ) ; } } } return answer ; } public static void Main ( String [ ] args ) { int N = 10 , K = 2 ; Console . WriteLine ( countQuadruples ( N , K ) ) ; } }
Find the number which when added to the given ratio a : b , the ratio changes to c : d | C # implementation of the approach ; Function to return the required number X ; Driver code
using System ; class GFG { static int getX ( int a , int b , int c , int d ) { int X = ( b * c - a * d ) / ( d - c ) ; return X ; } static public void Main ( ) { int a = 2 , b = 3 , c = 4 , d = 5 ; Console . Write ( getX ( a , b , c , d ) ) ; } }
Number of ways to arrange a word such that no vowels occur together | C # code for above approach ; Function to check if a character is vowel or consonent ; Function to calculate factorial of a number ; Calculating no of ways for arranging vowels ; Iterate the map and count the number of vowels and calculate no of ways to arrange vowels ; Calculating no of ways to arrange the given word such that all vowels come together ; Calculate no of ways to arrange vowels ; To store denominator of fraction ; Count of consonents ; Calculate the number of ways to arrange the word such that all vowels come together ; To calculate total number of permutations ; To store length of the given word ; Denominator of fraction ; Return total number of permutations of the given word ; Function to calculate number of permutations such that no vowels come together ; To store frequency of character ; Count frequency of all characters ; Calculate total number of permutations ; Calculate total number of permutations such that all vowels come together ; Subtract vwl_tgthr from total to get the result ; Return the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } static long fact ( long n ) { if ( n < 2 ) { return 1 ; } return n * fact ( n - 1 ) ; } static long only_vowels ( Dictionary < char , int > freq ) { long denom = 1 ; long cnt_vwl = 0 ; foreach ( KeyValuePair < char , int > itr in freq ) { if ( isVowel ( itr . Key ) ) { denom *= fact ( itr . Value ) ; cnt_vwl += itr . Value ; } } return fact ( cnt_vwl ) / denom ; } static long all_vowels_together ( Dictionary < char , int > freq ) { long vow = only_vowels ( freq ) ; long denom = 1 ; long cnt_cnst = 0 ; foreach ( KeyValuePair < char , int > itr in freq ) { if ( ! isVowel ( itr . Key ) ) { denom *= fact ( itr . Value ) ; cnt_cnst += itr . Value ; } } long ans = fact ( cnt_cnst + 1 ) / denom ; return ( ans * vow ) ; } static long total_permutations ( Dictionary < char , int > freq ) { long cnt = 0 ; long denom = 1 ; foreach ( KeyValuePair < char , int > itr in freq ) { denom *= fact ( itr . Value ) ; cnt += itr . Value ; } return fact ( cnt ) / denom ; } static long no_vowels_together ( string word ) { Dictionary < char , int > freq = new Dictionary < char , int > ( ) ; for ( int i = 0 ; i < word . Length ; i ++ ) { char ch = Char . ToLower ( word [ i ] ) ; if ( freq . ContainsKey ( ch ) ) { freq [ ch ] ++ ; } else { freq [ ch ] = 1 ; } } long total = total_permutations ( freq ) ; long vwl_tgthr = all_vowels_together ( freq ) ; long res = total - vwl_tgthr ; return res ; } static void Main ( ) { string word = " allahabad " ; long ans = no_vowels_together ( word ) ; Console . WriteLine ( ans ) ; word = " geeksforgeeks " ; ans = no_vowels_together ( word ) ; Console . WriteLine ( ans ) ; word = " abcd " ; ans = no_vowels_together ( word ) ; Console . WriteLine ( ans ) ; } }
Program to find the number of men initially | C # implementation of the approach ; Function to return the number of men initially ; Driver code
using System ; class GFG { static int numberOfMen ( int D , int m , int d ) { int Men = ( m * ( D - d ) ) / d ; return Men ; } public static void Main ( ) { int D = 5 , m = 4 , d = 4 ; Console . WriteLine ( numberOfMen ( D , m , d ) ) ; } }
Area of triangle formed by the axes of co | C # program area of triangle formed by the axes of co - ordinates and a given straight line ; Function to find area ; Driver code
using System ; class GFG { static double area ( double a , double b , double c ) { double d = Math . Abs ( ( c * c ) / ( 2 * a * b ) ) ; return d ; } static public void Main ( ) { double a = - 2 , b = 4 , c = 3 ; Console . WriteLine ( area ( a , b , c ) ) ; } }
Sum of two numbers where one number is represented as array of digits | C # implementation of the approach ; Function to return the vector containing the answer ; ArrayList v is to store each digits sum and ArrayList ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer
using System ; using System . Collections ; class GFG { static ArrayList addToArrayForm ( ArrayList A , int K ) { ArrayList v = new ArrayList ( ) ; ArrayList ans = new ArrayList ( ) ; int rem = 0 ; int i = 0 ; for ( i = A . Count - 1 ; i >= 0 ; i -- ) { int my = ( int ) A [ i ] + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . Add ( my % 10 ) ; } else { v . Add ( my ) ; rem = 0 ; } K = K / 10 ; } while ( K > 0 ) { int my = K % 10 + rem ; v . Add ( my % 10 ) ; if ( my / 10 > 0 ) rem = 1 ; else rem = 0 ; K = K / 10 ; } if ( rem > 0 ) v . Add ( rem ) ; for ( int j = v . Count - 1 ; j >= 0 ; j -- ) ans . Add ( ( int ) v [ j ] ) ; return ans ; } static void Main ( ) { ArrayList A = new ArrayList ( ) ; A . Add ( 2 ) ; A . Add ( 7 ) ; A . Add ( 4 ) ; int K = 181 ; ArrayList ans = addToArrayForm ( A , K ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . Write ( ( int ) ans [ i ] ) ; } }
Compute maximum of the function efficiently over all sub | C # implementation of the approach ; Function to return maximum sum of a sub - array ; Function to return maximum value of function F ; Compute arrays B [ ] and C [ ] ; Find maximum sum sub - array of both of the arrays and take maximum among them ; Driver code
using System ; class GFG { static int MAX = 100005 ; static int kadaneAlgorithm ( int [ ] ar , int n ) { int sum = 0 , maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; if ( sum < 0 ) sum = 0 ; maxSum = Math . Max ( maxSum , sum ) ; } return maxSum ; } static int maxFunction ( int [ ] arr , int n ) { int [ ] b = new int [ MAX ] ; int [ ] c = new int [ MAX ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 1 ) { b [ i ] = Math . Abs ( arr [ i + 1 ] - arr [ i ] ) ; c [ i ] = - b [ i ] ; } else { c [ i ] = Math . Abs ( arr [ i + 1 ] - arr [ i ] ) ; b [ i ] = - c [ i ] ; } } int ans = kadaneAlgorithm ( b , n - 1 ) ; ans = Math . Max ( ans , kadaneAlgorithm ( c , n - 1 ) ) ; return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 4 , 7 } ; int n = arr . Length ; Console . WriteLine ( maxFunction ( arr , n ) ) ; } }
Third last digit in 5 ^ N for given N | C # implementation of the above approach ; Function to find the element ; if n < 3 ; If n is even return 6 If n is odd return 1 ; Driver code
using System ; class GFG { static int findThirdDigit ( int n ) { if ( n < 3 ) return 0 ; return ( n & 1 ) > 0 ? 1 : 6 ; } static void Main ( ) { int n = 7 ; Console . WriteLine ( findThirdDigit ( n ) ) ; } }
Probability of A winning the match when individual probabilities of hitting the target given | C # implementation of the approach ; Function to return the probability of A winning ; p and q store the values of fractions a / b and c / d ; To store the winning probability of A ; Driver code
using System ; class GFG { public static double getProbability ( int a , int b , int c , int d ) { double p = ( double ) a / ( double ) b ; double q = ( double ) c / ( double ) d ; double ans = p * ( 1 / ( 1 - ( 1 - q ) * ( 1 - p ) ) ) ; return ans ; } public static void Main ( string [ ] args ) { int a = 1 , b = 2 , c = 10 , d = 11 ; Console . Write ( " { 0 : F5 } " , getProbability ( a , b , c , d ) ) ; } }
Largest palindromic number in an array | C # implementation of above approach ; Function to check if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digits are not same then return false ; Removing the leading and trailing digits from the number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function to find the largest palindromic number ; If a palindrome larger than the currentMax is found ; Return the largest palindromic number from the array ; Driver program ; print required answer
using System ; class GFG { static bool isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static int largestPalindrome ( int [ ] A , int n ) { int currentMax = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] > currentMax && isPalindrome ( A [ i ] ) ) currentMax = A [ i ] ; } return currentMax ; } public static void Main ( ) { int [ ] A = { 1 , 232 , 54545 , 999991 } ; int n = A . Length ; Console . WriteLine ( largestPalindrome ( A , n ) ) ; } }
Reduce the array to a single element with the given operation | C # implementation of the approach ; Function to return the final element ; Driver code
using System ; public class GFG { public static long getFinalElement ( long n ) { long finalNum ; for ( finalNum = 2 ; finalNum * 2 <= n ; finalNum *= 2 ) ; return finalNum ; } static public void Main ( ) { int N = 12 ; Console . WriteLine ( getFinalElement ( N ) ) ; } }
Sum of elements in an array having prime frequency | C # program to find sum of elements in an array having prime frequency ; Function to create Sieve to check primes ; False here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the sum of elements in an array having prime frequency ; Map is used to store element frequencies ; Traverse the map ; Count the number of elements having prime frequencies ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void SieveOfEratosthenes ( bool [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int sumOfElements ( int [ ] arr , int n ) { bool [ ] prime = new bool [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , n + 1 ) ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int sum = 0 ; foreach ( KeyValuePair < int , int > entry in m ) { int key = entry . Key ; int value = entry . Value ; if ( prime [ value ] ) { sum += ( key ) ; } } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 6 , 5 , 4 , 6 } ; int n = arr . Length ; Console . WriteLine ( sumOfElements ( arr , n ) ) ; } }
Sum of all odd length palindromic numbers within the range [ L , R ] | C # program to find the sum of all odd length palindromic numbers within the given range ; Function that returns true if the given number is a palindrome ; Here we are generating a new number ( reverse_num ) * by reversing the digits of original input number ; If the original input number ( num ) is equal to * to its reverse ( reverse_num ) then its palindrome * else it is not . ; Function that returns true if the given number is of odd length ; Function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; Driver code
using System ; public class GFG { static bool isPalindrome ( int num ) { int reverse_num = 0 , remainder , temp ; temp = num ; while ( temp != 0 ) { remainder = temp % 10 ; reverse_num = reverse_num * 10 + remainder ; temp /= 10 ; } if ( reverse_num == num ) { return true ; } return false ; } static bool isOddLength ( int num ) { int count = 0 ; while ( num > 0 ) { num /= 10 ; count ++ ; } if ( count % 2 != 0 ) { return true ; } return false ; } static long sumOfAllPalindrome ( int L , int R ) { long sum = 0 ; if ( L <= R ) for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) && isOddLength ( i ) ) { sum += i ; } } return sum ; } public static void Main ( String [ ] args ) { int L = 110 , R = 1130 ; Console . WriteLine ( sumOfAllPalindrome ( L , R ) ) ; } }
Number of ways to arrange a word such that all vowels occur together | C # program to calculate the no . of ways to arrange the word having vowels together ; Factorial of a number ; Calculating ways for arranging consonants ; Ignore vowels ; Calculating ways for arranging vowels ; Function to count total no . of ways ; Count vowels and consonant ; Total no . of ways ; Driver code
using System . Collections . Generic ; using System ; class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f = f * i ; return f ; } static int waysOfConsonants ( int size1 , int [ ] freq ) { int ans = fact ( size1 ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) continue ; else ans = ans / fact ( freq [ i ] ) ; } return ans ; } static int waysOfVowels ( int size2 , int [ ] freq ) { return fact ( size2 ) / ( fact ( freq [ 0 ] ) * fact ( freq [ 4 ] ) * fact ( freq [ 8 ] ) * fact ( freq [ 14 ] ) * fact ( freq [ 20 ] ) ) ; } static int countWays ( string str ) { int [ ] freq = new int [ 200 ] ; for ( int i = 0 ; i < 200 ; i ++ ) freq [ i ] = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; int vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] != ' a ' && str [ i ] != ' e ' && str [ i ] != ' i ' && str [ i ] != ' o ' && str [ i ] != ' u ' ) consonant ++ ; else vowel ++ ; } return waysOfConsonants ( consonant + 1 , freq ) * waysOfVowels ( vowel , freq ) ; } public static void Main ( ) { string str = " geeksforgeeks " ; Console . WriteLine ( countWays ( str ) ) ; } }
Sum of Fibonacci Numbers with alternate negatives | C # Program to find alternate sum of Fibonacci numbers ; Computes value of first fibonacci numbers and stores their alternate sum ; Initialize result ; Add remaining terms ; For even terms ; For odd terms ; Return the alternating sum ; Driver code ; Get n ; Find the alternating sum
using System ; class GFG { static double calculateAlternateSum ( int n ) { if ( n <= 0 ) return 0 ; int [ ] fibo = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; double sum = Math . Pow ( fibo [ 0 ] , 2 ) + Math . Pow ( fibo [ 1 ] , 2 ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum -= fibo [ i ] ; else sum += fibo [ i ] ; } return sum ; } public static void Main ( ) { int n = 8 ; Console . WriteLine ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateAlternateSum ( n ) ) ; } }
Find nth Term of the Series 1 2 2 4 4 4 4 8 8 8 8 8 8 8 8 ... | C # Program to find Nth term ; Function that will return nth term ; Driver Code ; Get n ; Get the value ; Get n ; Get the value
using System ; class GFG { static int getValue ( int n ) { int i = 0 , k = 1 ; while ( i < n ) { i = i + k ; k = k * 2 ; } return k / 2 ; } public static void Main ( ) { int n = 9 ; Console . WriteLine ( getValue ( n ) ) ; n = 1025 ; Console . WriteLine ( getValue ( n ) ) ; } }
Construct a frequency array of digits of the values obtained from x ^ 1 , x ^ 2 , ... ... . . , x ^ n | C # implementation of above approach ; Function that traverses digits in a number and modifies frequency count array ; Array to keep count of digits ; Traversing through x ^ 1 to x ^ n ; For power function , both its parameters are to be in double ; calling countDigits function on x ^ i ; Printing count of digits 0 - 9 ; Driver code
using System ; class GFG { static void countDigits ( double val , long [ ] arr ) { while ( ( long ) val > 0 ) { long digit = ( long ) val % 10 ; arr [ ( int ) digit ] ++ ; val = ( long ) val / 10 ; } return ; } static void countFrequency ( int x , int n ) { long [ ] freq_count = new long [ 10 ] ; for ( int i = 1 ; i <= n ; i ++ ) { double val = Math . Pow ( ( double ) x , ( double ) i ) ; countDigits ( val , freq_count ) ; } for ( int i = 0 ; i <= 9 ; i ++ ) { Console . Write ( freq_count [ i ] + " ▁ " ) ; } } public static void Main ( ) { int x = 15 , n = 3 ; countFrequency ( x , n ) ; } }
Number of values of b such that a = b + ( a ^ b ) | C # program to find the number of values of b such that a = b + ( a ^ b ) ; function to return the number of solutions ; check for every possible value ; Driver Code
using System ; class GFG { static int countSolutions ( int a ) { int count = 0 ; for ( int i = 0 ; i <= a ; i ++ ) { if ( a == ( i + ( a ^ i ) ) ) count ++ ; } return count ; } public static void Main ( ) { int a = 3 ; Console . WriteLine ( countSolutions ( a ) ) ; } }
Number of values of b such that a = b + ( a ^ b ) | C # program to find the number of values of b such that a = b + ( a ^ b ) ; function to return the number of solutions ; Driver Code
class GFG { static int countSolutions ( int a ) { int count = bitCount ( a ) ; count = ( int ) System . Math . Pow ( 2 , count ) ; return count ; } static int bitCount ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static void Main ( ) { int a = 3 ; System . Console . WriteLine ( countSolutions ( a ) ) ; } }