codeparrot/xlcost-text-to-code huggingface dataset:
Astonishing Numbers | C # implementation for the above approach ; Function to concatenate two integers into one ; Convert both the integers to String ; Concatenate both Strings ; Convert the concatenated String to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for Astonishing number ; Driver Code ; Given Number ; Function Call
using System ; class GFG { static int concat ( int a , int b ) { String s1 = a . ToString ( ) ; String s2 = b . ToString ( ) ; String s = s1 + s2 ; int c = Int32 . Parse ( s ) ; return c ; } static bool isAstonishing ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += j ; if ( sum == n ) { int concatenation = concat ( i , j ) ; if ( concatenation == n ) { return true ; } } } } return false ; } public static void Main ( String [ ] args ) { int n = 429 ; if ( isAstonishing ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Digitally balanced numbers | C # implementation to check if a number is a digitally balanced number ; Function to check if the digits in the number is the same number of digits ; Loop to iterate over the digits of the number N ; Loop to iterate over the map ; Driver Code ; Function to check
using System ; using System . Collections . Generic ; class GFG { static bool checkSame ( int n , int b ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; while ( n != 0 ) { int r = n % b ; n = n / b ; if ( m . ContainsKey ( r ) ) { m [ r ] = m [ r ] + 1 ; } else { m . Add ( r , 1 ) ; } } int last = - 1 ; foreach ( KeyValuePair < int , int > i in m ) { if ( last != - 1 && i . Value != last ) { return false ; } else { last = i . Value ; } } return true ; } public static void Main ( String [ ] args ) { int n = 9 ; int Base = 2 ; if ( checkSame ( n , Base ) ) Console . Write ( " Yes " ) ; else Console . Write ( " NO " ) ; } }
Sum of series formed by difference between product and sum of N natural numbers | C # program to implement the above approach ; Function to calculate the sum upto Nth term ; Stores the sum of the series ; Stores the product of natural numbers upto the current term ; Stores the sum of natural numbers upto the upto current term ; Generate the remaining terms and calculate sum ; Update the sum ; Return the sum ; Driver code
using System ; class GFG { static int seriesSum ( int n ) { int sum = 0 ; int currProd = 1 ; int currSum = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { currProd *= i ; currSum += i ; sum += currProd - currSum ; } return sum ; } public static void Main ( ) { int N = 5 ; Console . Write ( seriesSum ( N ) ) ; } }
Count of elements not divisible by any other elements of Array | C # program for the above approach ; Function to count the number of elements of array which are not divisible by any other element in the array [ ] arr ; Iterate over the array ; Check if the element is itself or not ; Check for divisibility ; Return the readonly result ; Driver Code ; Given array ; Function call
using System ; class GFG { static int count ( int [ ] a , int n ) { int countElements = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; if ( a [ i ] % a [ j ] == 0 ) { flag = false ; break ; } } if ( flag == true ) ++ countElements ; } return countElements ; } public static void Main ( String [ ] args ) { int [ ] arr = { 86 , 45 , 18 , 4 , 8 , 28 , 19 , 33 , 2 } ; int n = arr . Length ; Console . Write ( count ( arr , n ) ) ; } }
Smallest N digit number divisible by N | C # program for the above approach ; Function to find the smallest N - digit number divisible by N ; Return the smallest N - digit number calculated using above formula ; Driver Code ; Given N ; Function Call
using System ; class GFG { static int smallestNumber ( int N ) { return ( int ) ( N * Math . Ceiling ( Math . Pow ( 10 , ( N - 1 ) ) / N ) ) ; } public static void Main ( ) { int N = 2 ; Console . Write ( smallestNumber ( N ) ) ; } }
Count pairs in an array containing at least one even value | C # implementation to count pairs in an array such that each pair contains at least one even element ; Function to count the pairs in the array such as there is at least one even element in each pair ; Generate all possible pairs and increment then count if the condition is satisfied ; Driver code ; Function Call
using System ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] % 2 == 0 arr [ j ] % 2 == 0 ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 2 , 3 , 1 , 4 , 2 } ; int n = arr . Length ; Console . WriteLine ( CountPairs ( arr , n ) ) ; } }
Count pairs in an array containing at least one even value | C # implementation to Count pairs in an array such that each pair contains at least one even element ; Function to count the pairs in the array such as there is at least one even element in each pair ; Store count of even and odd elements ; Check element is even or odd ; Driver Code
using System ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return ( even * ( even - 1 ) ) / 2 + ( even * odd ) ; } public static void Main ( ) { int [ ] arr = { 8 , 2 , 3 , 1 , 4 , 2 } ; int n = arr . Length ; Console . Write ( CountPairs ( arr , n ) ) ; } }
Giuga Numbers | C # program for the above approach ; Function to check if n is a composite number ; Corner cases ; This is checked to skip middle 5 numbers ; Function to check if N is a Giuga Number ; N should be composite to be a Giuga Number ; Print the number of 2 s that divide n ; N must be odd at this point . So we can skip one element ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number > 2 ; Driver code ; Given Number N ; Function Call
using System ; class GFG { static bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static bool isGiugaNum ( int n ) { if ( ! ( isComposite ( n ) ) ) return false ; int N = n ; while ( n % 2 == 0 ) { if ( ( N / 2 - 1 ) % 2 != 0 ) return false ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( ( N / i - 1 ) % i != 0 ) return false ; n = n / i ; } } if ( n > 2 ) if ( ( N / n - 1 ) % n != 0 ) return false ; return true ; } static void Main ( ) { int N = 30 ; if ( isGiugaNum ( N ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Droll Numbers | C # program for the above approach ; Function to check droll numbers ; To store sum of even prime factors ; To store sum of odd prime factors ; Add the number of 2 s that divide n in sum_even ; N must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Condition to check droll number ; Driver code ; Given Number N ; Function Call
using System ; class GFG { static bool isDroll ( int n ) { if ( n == 1 ) return false ; int sum_even = 0 ; int sum_odd = 0 ; while ( n % 2 == 0 ) { sum_even += 2 ; n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { sum_odd += i ; n = n / i ; } } if ( n > 2 ) sum_odd += n ; return sum_even == sum_odd ; } public static void Main ( ) { int n = 72 ; if ( isDroll ( n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Count all pairs of divisors of a number N whose sum is coprime with N | C # program to count all pairs of divisors such that their sum is coprime with N ; Function to find gcd of a and b ; Function to count all valid pairs ; Initialize count ; Check if sum of pair and n are coprime ; Return the result ; Driver method
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int CountPairs ( int n ) { int cnt = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int div1 = i ; int div2 = n / i ; int sum = div1 + div2 ; if ( gcd ( sum , n ) == 1 ) cnt += 1 ; } } return cnt ; } public static void Main ( ) { int n = 24 ; Console . WriteLine ( CountPairs ( n ) ) ; } }
Check if A can be converted to B by reducing with a Prime number | C # implementation to find if it is possible to make a equal to b ; Function to find if it is possible to make A equal to B ; Driver Code ; Function Call
using System ; class GFG { static bool isPossible ( int A , int B ) { return ( A - B > 1 ) ; } public static void Main ( ) { int A = 10 , B = 4 ; if ( isPossible ( A , B ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Maximize sum of minimum difference of divisors of nodes in N | C # program to maximize the sum of minimum difference of divisors of nodes in an n - ary tree ; Array to store the result at each node ; Function to get minimum difference between the divisors of a number ; Iterate from square root of N to N ; return absolute difference ; DFS function to calculate the maximum sum ; Store the min difference ; Add the maximum of all children to sub [ u ] ; Return maximum sum of node ' u ' to its parent ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int [ ] sub = new int [ 100005 ] ; static int minDivisorDifference ( int n ) { int num1 = 0 ; int num2 = 0 ; for ( int i = ( int ) Math . Sqrt ( n ) ; i <= n ; i ++ ) { if ( n % i == 0 ) { num1 = i ; num2 = n / i ; break ; } } return Math . Abs ( num1 - num2 ) ; } static int dfs ( List < int > [ ] g , int u , int par ) { sub [ u ] = minDivisorDifference ( u ) ; int mx = 0 ; foreach ( int c in g [ u ] ) { if ( c != par ) { int ans = dfs ( g , c , u ) ; mx = Math . Max ( mx , ans ) ; } } sub [ u ] += mx ; return sub [ u ] ; } public static void Main ( String [ ] args ) { List < int > [ ] g = new List < int > [ 100005 ] ; for ( int i = 0 ; i < g . Length ; i ++ ) g [ i ] = new List < int > ( ) ; int edges = 6 ; g [ 18 ] . Add ( 7 ) ; g [ 7 ] . Add ( 18 ) ; g [ 18 ] . Add ( 15 ) ; g [ 15 ] . Add ( 18 ) ; g [ 15 ] . Add ( 2 ) ; g [ 2 ] . Add ( 15 ) ; g [ 7 ] . Add ( 4 ) ; g [ 4 ] . Add ( 7 ) ; g [ 7 ] . Add ( 12 ) ; g [ 12 ] . Add ( 7 ) ; g [ 12 ] . Add ( 9 ) ; g [ 9 ] . Add ( 12 ) ; int root = 18 ; Console . Write ( dfs ( g , root , - 1 ) ) ; } }
Program to check if N is a Centered Cubic Number | C # program to check if N is a centered cubic number ; Function to check if N is a centered cubic number ; Iterating from 1 ; Infinite loop ; Finding ith_term ; Checking if the number N is a centered cube number ; If ith_term > N then N is not a centered cube number ; Incrementing i ; Driver code ; Function call
using System ; class GFG { static Boolean isCenteredcube ( int N ) { int i = 1 ; while ( true ) { int ith_term = ( 2 * i + 1 ) * ( i * i + i + 1 ) ; if ( ith_term == N ) { return true ; } if ( ith_term > N ) { return false ; } i ++ ; } } public static void Main ( ) { int N = 9 ; if ( isCenteredcube ( N ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Product of N terms of a given Geometric series | C # program for the above approach ; Function to calculate product of geometric series ; Initialise final product with 1 ; Multiply product with each term stored in a ; Return the final product ; Driver Code ; Given first term and common ratio ; Number of terms ; Function Call
using System ; class GFG { static float productOfGP ( float a , float r , int n ) { float product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = product * a ; a = a * r ; } return product ; } public static void Main ( ) { float a = 1 , r = 2 ; int N = 4 ; Console . Write ( productOfGP ( a , r , N ) ) ; } }
Sum of given N fractions in reduced form | C # program for the above approach ; Function to find GCD of a & b using Euclid Lemma ; Base case ; Function to find the LCM of all elements in arr [ ] ; Initialize result ; Iterate arr [ ] to find LCM ; Return the final LCM ; Function to find the sum of N fraction in reduced form ; To store the sum of all final numerators ; Find the LCM of all denominator ; Find the sum of all N numerators & denominators ; Add each fraction one by one ; Find GCD of final numerator and denominator ; Convert into reduced form by dividing from GCD ; Print the final fraction ; Driver code ; Given N ; Given numerator ; Given denominator ; Function call
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static int findlcm ( int [ ] arr , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } static void addReduce ( int n , int [ ] num , int [ ] den ) { int final_numerator = 0 ; int final_denominator = findlcm ( den , n ) ; for ( int i = 0 ; i < n ; i ++ ) { final_numerator = final_numerator + ( num [ i ] ) * ( final_denominator / den [ i ] ) ; } int GCD = gcd ( final_numerator , final_denominator ) ; final_numerator /= GCD ; final_denominator /= GCD ; Console . Write ( final_numerator + " / " + final_denominator ) ; } public static void Main ( string [ ] args ) { int N = 3 ; int [ ] arr1 = { 1 , 2 , 5 } ; int [ ] arr2 = { 2 , 1 , 6 } ; addReduce ( N , arr1 , arr2 ) ; } }
Minimum LCM of all pairs in a given array | C # program to find minimum possible lcm from any pair ; Function to compute GCD of two numbers ; Function that return minimum possible lcm from any pair ; Fix the ith element and iterate over all the array to find minimum LCM ; 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 minLCM ( int [ ] arr , int n ) { int ans = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int g = gcd ( arr [ i ] , arr [ j ] ) ; int lcm = arr [ i ] / g * arr [ j ] ; ans = Math . Min ( ans , lcm ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . Length ; Console . Write ( minLCM ( arr , n ) ) ; } }
Find two numbers whose difference of fourth power is equal to N | C # implementation to find the values of x and y for the given equation with integer N ; Function which find required x & y ; Upper limit of x & y , if such x & y exists ; num1 stores x ^ 4 ; num2 stores y ^ 4 ; If condition is satisfied the print and return ; If no such pair exists ; Driver code
using System ; class GFG { static void solve ( int n ) { int upper_limit = ( int ) ( Math . Ceiling ( Math . Pow ( n , 1.0 / 4 ) ) ) ; for ( int x = 0 ; x <= upper_limit ; x ++ ) { for ( int y = 0 ; y <= upper_limit ; y ++ ) { int num1 = x * x * x * x ; int num2 = y * y * y * y ; if ( num1 - num2 == n ) { Console . Write ( " x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; return ; } } } Console . Write ( - 1 ) ; } public static void Main ( String [ ] args ) { int n = 15 ; solve ( n ) ; } }
Check if count of even divisors of N is equal to count of odd divisors | C # code for the above program ; Function to check if count of even and odd divisors are equal ; To store the count of even factors and odd factors ; Loop till [ 1 , sqrt ( N ) ] ; If divisors are equal add only one ; Check for even divisor ; Odd divisor ; Check for both divisor i . e . , i and N / i ; Check if i is odd or even ; Check if N / i is odd or even ; Return true if count of even_div and odd_div are equals ; Driver code ; Given number ; Function call
using System ; class GFG { static bool divisorsSame ( int n ) { int even_div = 0 , odd_div = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } else { if ( i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } if ( n / i % 2 == 0 ) { even_div ++ ; } else { odd_div ++ ; } } } } return ( even_div == odd_div ) ; } public static void Main ( ) { int N = 6 ; if ( divisorsSame ( N ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Check if N is a Balanced Prime number or not | C # program to check if a given number is Balanced prime ; Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a Balanced prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a weak prime ; Driver code
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static bool isBalancedPrime ( int n ) { if ( ! isPrime ( n ) n == 2 ) return false ; int previous_prime = n - 1 ; int next_prime = n + 1 ; while ( ! isPrime ( next_prime ) ) next_prime ++ ; while ( ! isPrime ( previous_prime ) ) previous_prime -- ; int mean = ( previous_prime + next_prime ) / 2 ; if ( n == mean ) return true ; else return false ; } public static void Main ( ) { int n = 53 ; if ( isBalancedPrime ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Count of nodes having odd divisors in the given subtree for Q queries | C # implementation to count the number of nodes having odd number of divisors for each query ; Adjacency list for tree . ; Array for values and answer at ith node . ; Function to check whether N has odd divisors or not ; DFS function to pre - compute the answers ; Initialize the count ; Repeat for every child ; Increase the count if current node has odd number of divisors ; Driver Code ; Adjacency List ; Function call
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 100001 ; static List < int > [ ] adj = new List < int > [ N ] ; static int [ ] a = new int [ N ] ; static int [ ] ans = new int [ N ] ; static bool hasOddNumberOfDivisors ( int n ) { if ( ( double ) Math . Sqrt ( n ) == ( int ) Math . Sqrt ( n ) ) return true ; return false ; } static int dfs ( int node , int parent ) { int count = 0 ; foreach ( int i in adj [ node ] ) { if ( i != parent ) { count += dfs ( i , node ) ; } } if ( hasOddNumberOfDivisors ( a [ node ] ) ) ++ count ; ans [ node ] = count ; return count ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] q = { 4 , 1 , 5 , 3 } ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 2 ) ; adj [ 3 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 3 ) ; adj [ 1 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 1 ) ; a [ 1 ] = 4 ; a [ 2 ] = 9 ; a [ 3 ] = 14 ; a [ 4 ] = 100 ; a [ 5 ] = 5 ; dfs ( 1 , - 1 ) ; for ( int i = 0 ; i < q . Length ; i ++ ) { Console . Write ( ans [ q [ i ] ] + " ▁ " ) ; } } }
Minimum Cost to make all array elements equal using given operations | C # implementation to find the minimum cost to make all array elements equal ; Checks if the value is less than middle element of the array ; Function that returns the cost of making all elements equal to current element ; Compute the lower bound of current element ; Calculate the requirement of add operation ; Calculate the requirement of subtract operation ; Compute minimum of left and right ; Computing the total cost of add and subtract operations ; Function that prints minimum cost of making all elements equal ; Sort the given array ; Calculate minimum from a + r and m ; Compute prefix sum and store in pref array ; Find the minimum cost from the given elements ; Finding the minimum cost from the other cases where minimum cost can occur ; Printing the minimum cost of making all elements equal ; Driver Code ; Function Call
using System ; class GFG { public static int lowerBound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static int costCalculation ( int current , int [ ] arr , int n , int [ ] pref , int a , int r , int minimum ) { int index = lowerBound ( arr , arr . Length , current ) ; int left = index * current - pref [ index ] ; int right = pref [ n ] - pref [ index ] - ( n - index ) * current ; int res = Math . Min ( left , right ) ; left -= res ; right -= res ; int total = res * minimum ; total += left * a ; total += right * r ; return total ; } public static void solve ( int [ ] arr , int n , int a , int r , int m ) { Array . Sort ( arr ) ; int minimum = Math . Min ( a + r , m ) ; int [ ] pref = new int [ n + 1 ] ; Array . Fill ( pref , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref [ i + 1 ] = pref [ i ] + arr [ i ] ; int ans = 10000 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . Min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) ) ; ans = Math . Min ( ans , costCalculation ( pref [ n ] / n , arr , n , pref , a , r , minimum ) ) ; ans = Math . Min ( ans , costCalculation ( pref [ n ] / n + 1 , arr , n , pref , a , r , minimum ) ) ; Console . WriteLine ( ans ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 5 , 5 , 3 , 6 , 5 } ; int A = 1 , R = 2 , M = 4 ; int size = arr . Length ; solve ( arr , size , A , R , M ) ; } }
Count of integers up to N which represent a Binary number | C # program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code
using System ; class GFG { static int countBinaries ( int N ) { int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += ( int ) Math . Pow ( 2 , ctr - 1 ) ; } else if ( N % 10 > 1 ) { ans = ( int ) ( Math . Pow ( 2 , ctr ) - 1 ) ; } ctr ++ ; N /= 10 ; } return ans ; } public static void Main ( String [ ] args ) { int N = 20 ; Console . Write ( countBinaries ( N ) ) ; } }
Count of integers up to N which represent a Binary number | C # program to count the number of integers upto N which are of the form of binary representations ; Function to return the count ; PreCompute and store the powers of 2 ; If the current last digit is 1 ; Add 2 ^ ( ctr - 1 ) possible integers to the answer ; If the current digit exceeds 1 ; Set answer as 2 ^ ctr - 1 as all possible binary integers with ctr number of digits can be obtained ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countBinaries ( int N ) { List < int > powersOfTwo = new List < int > ( ) ; powersOfTwo . Add ( 1 ) ; for ( int i = 1 ; i < 11 ; i ++ ) { powersOfTwo . Add ( powersOfTwo [ i - 1 ] * 2 ) ; } int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += powersOfTwo [ ctr - 1 ] ; } else if ( N % 10 > 1 ) { ans = powersOfTwo [ ctr ] - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } static public void Main ( ) { int N = 20 ; Console . Write ( countBinaries ( N ) ) ; } }