codeparrot/xlcost-text-to-code huggingface dataset:
Astonishing Numbers | Java 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
import java . io . * ; class GFG { static int concat ( int a , int b ) { String s1 = Integer . toString ( a ) ; String s2 = Integer . toString ( b ) ; String s = s1 + s2 ; int c = Integer . parseInt ( s ) ; return c ; } static boolean 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 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Digitally balanced numbers | Java 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
import java . util . * ; class GFG { static boolean checkSame ( int n , int b ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; while ( n != 0 ) { int r = n % b ; n = n / b ; if ( m . containsKey ( r ) ) { m . put ( r , m . get ( r ) + 1 ) ; } else { m . put ( r , 1 ) ; } } int last = - 1 ; for ( Map . Entry < Integer , Integer > i : m . entrySet ( ) ) { if ( last != - 1 && i . getValue ( ) != last ) { return false ; } else { last = i . getValue ( ) ; } } return true ; } public static void main ( String [ ] args ) { int n = 9 ; int base = 2 ; if ( checkSame ( n , base ) ) System . out . print ( " Yes " ) ; else System . out . print ( " NO " ) ; } }
Sum of series formed by difference between product and sum of N natural numbers | Java 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
import java . lang . * ; 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 ( String [ ] args ) { int N = 5 ; System . out . print ( seriesSum ( N ) ) ; } }
Count of elements not divisible by any other elements of Array | Java 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 final result ; Driver Code ; Given array ; Function call
class GFG { static int count ( int a [ ] , int n ) { int countElements = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean 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 ; System . out . print ( count ( arr , n ) ) ; } }
Smallest N digit number divisible by N | Java 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
import java . util . * ; class GFG { static int smallestNumber ( int N ) { return ( int ) ( N * Math . ceil ( Math . pow ( 10 , ( N - 1 ) ) / N ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( smallestNumber ( N ) ) ; } }
Count pairs in an array containing at least one even value | Java 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
import java . util . * ; 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 ; System . out . println ( CountPairs ( arr , n ) ) ; } }
Count pairs in an array containing at least one even value | Java 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
import java . util . * ; 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 ( String [ ] args ) { int [ ] arr = { 8 , 2 , 3 , 1 , 4 , 2 } ; int n = arr . length ; System . out . println ( CountPairs ( arr , n ) ) ; } }
Giuga Numbers | Java 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
class GFG { static boolean 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 boolean 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 ; } public static void main ( String [ ] args ) { int n = 30 ; if ( isGiugaNum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Droll Numbers | Java 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
class GFG { static boolean 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 ( String [ ] args ) { int n = 72 ; if ( isDroll ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count all pairs of divisors of a number N whose sum is coprime with N | Java program to count all pairs of divisors such that their sum is coprime with N ; Function to calculate GCD ; Function to count all valid pairs ; Initialize count ; Check if sum of pair and n are coprime ; Return the result ; Driver code
import java . util . * ; class GFG { public static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } public 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 ( String [ ] args ) { int n = 24 ; System . out . println ( CountPairs ( n ) ) ; } }
Check if A can be converted to B by reducing with a Prime number | Java 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
class GFG { static boolean isPossible ( int A , int B ) { return ( A - B > 1 ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 4 ; if ( isPossible ( A , B ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Maximize sum of minimum difference of divisors of nodes in N | Java 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
import java . util . Vector ; 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 ( Vector < Integer > g [ ] , int u , int par ) { sub [ u ] = minDivisorDifference ( u ) ; int mx = 0 ; for ( int c : 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 ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] g = new Vector [ 100005 ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; 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 ; System . out . print ( dfs ( g , root , - 1 ) ) ; } }
Program to check if N is a Centered Cubic Number | Java 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
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 ( String [ ] args ) { int N = 9 ; if ( isCenteredcube ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Product of N terms of a given Geometric series | Java 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
import java . util . * ; 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 ( String args [ ] ) { float a = 1 , r = 2 ; int N = 4 ; System . out . print ( productOfGP ( a , r , N ) ) ; } }
Sum of given N fractions in reduced form | Java 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
import java . util . * ; 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 ; System . out . println ( 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 | Java 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
import java . io . * ; import java . util . * ; 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 = Integer . MAX_VALUE ; 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 ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . length ; System . out . println ( minLCM ( arr , n ) ) ; } }
Find two numbers whose difference of fourth power is equal to N | Java 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
import java . util . * ; class GFG { static void solve ( int n ) { int upper_limit = ( int ) ( Math . ceil ( 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 ) { System . out . print ( " x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; return ; } } } System . out . print ( - 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 | Java 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
import java . util . * ; class GFG { static boolean 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 ( String [ ] args ) { int N = 6 ; if ( divisorsSame ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Check if N is a Balanced Prime number or not | Java 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
class GFG { static boolean 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 += 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean 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 ( String [ ] args ) { int n = 53 ; if ( isBalancedPrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Count of nodes having odd divisors in the given subtree for Q queries | Java 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
import java . util . * ; class GFG { static final int N = 100001 ; static Vector < Integer > [ ] adj = new Vector [ N ] ; static int [ ] a = new int [ N ] ; static int [ ] ans = new int [ N ] ; static boolean 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 ; for ( int i : 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 Vector < Integer > ( ) ; 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 ++ ) { System . out . print ( ans [ q [ i ] ] + " ▁ " ) ; } } }
Minimum Cost to make all array elements equal using given operations | Java 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
import java . lang . * ; import java . util . * ; class GFG { public static int lowerBound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { final 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 ) { Arrays . sort ( arr ) ; int minimum = Math . min ( a + r , m ) ; int [ ] pref = new int [ n + 1 ] ; Arrays . 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 ) ) ; System . out . println ( 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 | Java 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
import java . util . * ; class GFG { static int countBinaries ( int N ) { int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += 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 ; System . out . print ( countBinaries ( N ) ) ; } }
Count of integers up to N which represent a Binary number | Java 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
import java . util . * ; class GFG { static int countBinaries ( int N ) { Vector < Integer > powersOfTwo = new Vector < Integer > ( 11 ) ; powersOfTwo . add ( 1 ) ; for ( int i = 1 ; i < 11 ; i ++ ) { powersOfTwo . add ( powersOfTwo . get ( i - 1 ) * 2 ) ; } int ctr = 1 ; int ans = 0 ; while ( N > 0 ) { if ( N % 10 == 1 ) { ans += powersOfTwo . get ( ctr - 1 ) ; } else if ( N % 10 > 1 ) { ans = powersOfTwo . get ( ctr ) - 1 ; } ctr ++ ; N /= 10 ; } return ans ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . print ( countBinaries ( N ) ) ; } }