codeparrot/xlcost-text-to-code huggingface dataset:
Count number of integers less than or equal to N which has exactly 9 divisors | Java implementation of above approach ; Function to count numbers having exactly 9 divisors ; Sieve array ; initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; Check if it can be expressed as p ^ 8 ; Driver Code
public class GFG { static int countNumbers ( int n ) { int c = 0 ; int limit = ( int ) Math . sqrt ( n ) ; int prime [ ] = new int [ limit + 1 ] ; for ( int i = 1 ; i <= limit ; i ++ ) { prime [ i ] = i ; } for ( int i = 2 ; i * i <= limit ; i ++ ) { if ( prime [ i ] == i ) { for ( int j = i * i ; j <= limit ; j += i ) { if ( prime [ j ] == j ) { prime [ j ] = i ; } } } } for ( int i = 2 ; i <= limit ; i ++ ) { int p = prime [ i ] ; int q = prime [ i / prime [ i ] ] ; if ( p * q == i && q != 1 && p != q ) { c += 1 ; } else if ( prime [ i ] == i ) { if ( Math . pow ( i , 8 ) <= n ) { c += 1 ; } } } return c ; } public static void main ( String [ ] args ) { int n = 1000 ; System . out . println ( countNumbers ( n ) ) ; } }
Interprime | JAVA program to check if a number is interprime or not ; 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 to check if the given number is interprime or not ; Smallest Interprime is 4 So the number less than 4 can not be a Interprime ; Calculate first prime number < n ; Calculate first prime number > n ; check if next_prime and prev_prime have the same average ; 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 = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean isInterprime ( int n ) { if ( n < 4 ) return false ; int prev_prime = n ; int next_prime = n ; while ( ! isPrime ( prev_prime ) ) { prev_prime -- ; } while ( ! isPrime ( next_prime ) ) { next_prime ++ ; } if ( ( prev_prime + next_prime ) == 2 * n ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 9 ; if ( isInterprime ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
Find the unit place digit of sum of N factorials | Java program to find the unit place digit of the first N natural numbers factorials ; Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 else ( N >= 4 ) ; Driver code
import java . io . * ; class GFG { static int get_unit_digit ( int N ) { if ( N == 0 N == 1 ) return 1 ; else if ( N == 2 ) return 3 ; else if ( N == 3 ) return 9 ; return 3 ; } public static void main ( String [ ] args ) { int N = 1 ; for ( N = 0 ; N <= 10 ; N ++ ) System . out . println ( " For ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + get_unit_digit ( N ) ) ; } }
Sum of squares of Fibonacci numbers | Java Program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code
class gfg { static int [ ] f = new int [ 1000 ] ; public static int fib ( int n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] > 0 ) { return f [ n ] ; } int k = ( ( n & 1 ) > 0 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( ( n & 1 ) > 0 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } public static int calculateSumOfSquares ( int n ) { return fib ( n ) * fib ( n + 1 ) ; } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; int n = 6 ; System . out . println ( " Sum ▁ of ▁ Squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + g . calculateSumOfSquares ( n ) ) ; } }
Number of solutions for the equation x + y + z <= n | Java program to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; Driver code
import java . io . * ; class GFG { static int NumberOfSolutions ( int x , int y , int z , int n ) { int ans = 0 ; for ( int i = 0 ; i <= x ; i ++ ) { for ( int j = 0 ; j <= y ; j ++ ) { int temp = n - i - j ; if ( temp >= 0 ) { temp = Math . min ( temp , z ) ; ans += temp + 1 ; } } } return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 2 , z = 3 , n = 4 ; System . out . println ( NumberOfSolutions ( x , y , z , n ) ) ; } }
Program to find the Nth term of series 5 , 12 , 21 , 32 , 45. ... . . | Java program to find the N - th term of the series : 5 , 12 , 21 , 32 , 45. . ... . ; calculate Nth term of series ; Driver code
import java . io . * ; class GFG { static int nthTerm ( int n ) { return ( int ) Math . pow ( n , 2 ) + 4 * n ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
Numbers less than N which are product of exactly two distinct prime numbers | Java program to find numbers that are product of exactly two distinct prime numbers ; Function to check whether a number is a PerfectSquare or not ; Function to check if a number is a product of exactly two distinct primes ; Function to find numbers that are product of exactly two distinct prime numbers . ; Vector to store such numbers ; insert in the vector ; Print all numbers till n from the vector ; Driver function
import java . util . * ; class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean isProduct ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } static void findNumbers ( int N ) { Vector < Integer > vec = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( isProduct ( i ) && ! isPerfectSquare ( i ) ) { vec . add ( i ) ; } } Iterator < Integer > itr = vec . iterator ( ) ; while ( itr . hasNext ( ) ) { System . out . print ( itr . next ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 30 ; findNumbers ( N ) ; } }
Program to find the Nth term of the series 3 , 20 , 63 , 144 , 230 , â €¦ â €¦ | Java program to find N - th term of the series : 3 , 20 , 63 , 144 , 230 . ... . ; calculate Nth term of series ; return final sum ; Driver code
import java . util . * ; class solution { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 3 ) + ( int ) Math . pow ( n , 2 ) ; } public static void main ( String arr [ ] ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }
Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | Java program to find N - th term of the series : ; function to return nth term of the series ; Driver Code ; Taking n as 4 ; Printing the nth term
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static long NthTerm ( long n ) { long x = ( 2 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } public static void main ( String args [ ] ) { long N = 4 ; System . out . println ( NthTerm ( N ) ) ; } }
Program to print the Sum of series | Java program to find SUM upto N - th term of the series : - 1 , 2 , 11 , 26 , 47 , 74 , ... . . ; calculate Nth term of series ; driver function ; Get the value of N ; Get the sum of the series
import java . util . * ; class solution { static int findSum ( int N ) { return ( N * ( N + 1 ) * ( 2 * N - 5 ) + 4 * N ) / 2 ; } public static void main ( String arr [ ] ) { int N = 3 ; System . out . println ( findSum ( N ) ) ; } }
Program to find the Nth term of series | Java program to find N - th term of the series : 9 , 23 , 45 , 75 , 113 , 159. . ... . ; calculate Nth term of series ; Driver code ; Get the value of N ; Find the Nth term and print it
class GFG { static int nthTerm ( int N ) { return ( ( 3 * N * N ) - ( 6 * N ) + 2 ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }
Program to find the value of tan ( nÎ ˜ ) | Java program to find the value of cos ( n - theta ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; store required answer ; use to toggle sign in sequence . ; calculate numerator ; calculate denominator ; Driver code
public class GFG { private static final int MAX = 16 ; static long nCr [ ] [ ] = new long [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findTanNTheta ( double tanTheta , int n ) { double ans = 0 , numerator = 0 , denominator = 0 ; long toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * Math . pow ( tanTheta , i ) * toggle ; toggle = toggle * - 1 ; } denominator = 1 ; toggle = - 1 ; for ( int i = 2 ; i <= n ; i += 2 ) { numerator = numerator + nCr [ n ] [ i ] * Math . pow ( tanTheta , i ) * toggle ; toggle = toggle * - 1 ; } ans = numerator / denominator ; return ans ; } public static void main ( String args [ ] ) { binomial ( ) ; double tanTheta = 0.3 ; int n = 10 ; System . out . println ( findTanNTheta ( tanTheta , n ) ) ; } }
Check if any large number is divisible by 19 or not | Java Program to validate the above logic ; Function to check if the number is divisible by 19 or not ; Extracting the last digit ; Truncating the number ; Subtracting the five times the last digit from the remaining number ; Return n is divisible by 19 ; Driver code
import java . io . * ; class GFG { static boolean isDivisible ( long n ) { while ( n / 100 > 0 ) { long d = n % 10 ; n /= 10 ; n += d * 2 ; } return ( n % 19 == 0 ) ; } public static void main ( String [ ] args ) { long n = 101156 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Program to find Sum of the series 1 * 3 + 3 * 5 + ... . | Java program to find sum of first n terms ; Sn = n * ( 4 * n * n + 6 * n - 1 ) / 3 ; Driver Code ; number of terms to be included in the sum ; find the Sn
class GFG { static int calculateSum ( int n ) { return ( n * ( 4 * n * n + 6 * n - 1 ) / 3 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
Triplet with no element divisible by 3 and sum N | Java program to print a , b and c such that a + b + c = N ; Function to print a , b and c ; first loop ; check for 1 st number ; second loop ; check for 2 nd number ; third loop ; Check for 3 rd number ; Driver Code
import java . io . * ; class GFG { static void printCombination ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( i % 3 != 0 ) { for ( int j = 1 ; j < n ; j ++ ) { if ( j % 3 != 0 ) { for ( int k = 1 ; k < n ; k ++ ) { if ( k % 3 != 0 && ( i + j + k ) == n ) { System . out . println ( i + " ▁ " + j + " ▁ " + k ) ; return ; } } } } } } } public static void main ( String [ ] args ) { int n = 233 ; printCombination ( n ) ; } }
Program to find the percentage of difference between two numbers | Java program to calculate the percentage ; Function to calculate the percentage ; Driver Code ; Function calling
class GFG { static int percent ( int a , int b ) { float result = 0 ; result = ( ( b - a ) * 100 ) / a ; return ( int ) result ; } public static void main ( String [ ] args ) { int a = 20 , b = 25 ; System . out . println ( percent ( a , b ) + " % " ) ; } }
Numbers with sum of digits equal to the sum of digits of its all prime factor | Java program to Find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum . ; maximum size of number ; array to store smallest prime factor of number ; array to store sum of digits of a number ; boolean array to check given number is countable for required answer or not . ; prefix array to store answer ; Calculating SPF ( SmallestPrime Factor ) for every number till MAXN . ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; Function to find sum of digits in a number ; find sum of digits of all numbers up to MAXN ; add sum of digits of least prime factor and n / spf [ n ] ; if it is valid make isValid true ; prefix sum to compute answer ; Driver code ; declaration ; print answer for required range ; print answer for required range
import java . io . * ; class GFG { static int MAXN = 100005 ; static int spf [ ] = new int [ MAXN ] ; static int sum_digits [ ] = new int [ MAXN ] ; static boolean isValid [ ] = new boolean [ MAXN ] ; static int ans [ ] = new int [ MAXN ] ; static void Smallest_prime_factor ( ) { for ( int i = 1 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i <= MAXN ; i += 2 ) if ( spf [ i ] == i ) for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } static int Digit_Sum ( int copy ) { int d = 0 ; while ( copy > 0 ) { d += copy % 10 ; copy /= 10 ; } return d ; } static void Sum_Of_All_Digits ( ) { for ( int n = 2 ; n < MAXN ; n ++ ) { sum_digits [ n ] = sum_digits [ n / spf [ n ] ] + Digit_Sum ( spf [ n ] ) ; if ( Digit_Sum ( n ) == sum_digits [ n ] ) isValid [ n ] = true ; } for ( int n = 2 ; n < MAXN ; n ++ ) { if ( isValid [ n ] ) ans [ n ] = 1 ; ans [ n ] += ans [ n - 1 ] ; } } public static void main ( String [ ] args ) { Smallest_prime_factor ( ) ; Sum_Of_All_Digits ( ) ; int l , r ; l = 2 ; r = 3 ; System . out . println ( " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " + l + " ▁ " + r + " ▁ are ▁ " + ( ans [ r ] - ans [ l - 1 ] ) ) ; l = 2 ; r = 10 ; System . out . println ( " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " + l + " ▁ " + r + " ▁ are ▁ " + ( ans [ r ] - ans [ l - 1 ] ) ) ; } }
Count numbers which can be represented as sum of same parity primes | Java program to Count numbers which can be represented as sum of same parity primes ; Function to calculate count ; Driver code
import java . util . * ; class GFG { public static int calculate ( int ar [ ] , int size ) { int count = 0 ; for ( int i = 0 ; i < size ; i ++ ) if ( ar [ i ] % 2 == 0 && ar [ i ] != 0 && ar [ i ] != 2 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 4 , 6 } ; int size = a . length ; System . out . print ( calculate ( a , size ) ) ; } }
N | Java program to answer queries for N - th prime factor of a number ; 2 - D vector that stores prime factors ; Function to pre - store prime factors of all numbers till 10 ^ 6 ; calculate unique prime factors for every number till 10 ^ 6 ; find prime factors ; store if prime factor ; Function that returns answer for every query ; Driver Code ; Function to pre - store unique prime factors ; 1 st query ; 2 nd query ; 3 rd query ; 4 th query
import java . util . * ; class GFG { static int N = 1000001 ; static Vector < Integer > [ ] v = new Vector [ N ] ; static void preprocess ( ) { for ( int i = 1 ; i < N ; i ++ ) { int num = i ; for ( int j = 2 ; j <= Math . sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . add ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . add ( num ) ; } } static int query ( int number , int n ) { return v [ number ] . get ( n - 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; preprocess ( ) ; int number = 6 , n = 1 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 210 ; n = 3 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 210 ; n = 2 ; System . out . print ( query ( number , n ) + "NEW_LINE"); number = 60 ; n = 2 ; System . out . print ( query ( number , n ) + "NEW_LINE"); } }
Program to find HCF ( Highest Common Factor ) of 2 Numbers | Java program to find GCD of two numbers ; Recursive function to return gcd of a and b ; Driver method
class Test { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }
Largest number with maximum trailing nines which is less than N and greater than N | Java code to implement above function ; It 's better to use long to handle big integers function to count no. of digits ; function to implement above approach ; if difference between power and n doesn 't exceed d ; loop to build a number from the appropriate no of digits containing only 9 ; if the build number is same as original number ( n ) ; observation ; Driver Code ; variable that stores no of digits in n
import java . io . * ; class GFG { static long dig ( long a ) { long count = 0 ; while ( a > 0 ) { a /= 10 ; count ++ ; } return count ; } static void required_number ( long num , long n , long d ) { long i , j , power = 1 , a , flag = 0 ; for ( i = num ; i >= 1 ; i -- ) { power = ( long ) Math . pow ( 10 , i ) ; a = n % power ; if ( d > a ) { flag = 1 ; break ; } } if ( flag > 0 ) { long t = 0 ; for ( j = 0 ; j < i ; j ++ ) { t += 9 * Math . pow ( 10 , j ) ; } if ( n % power == t ) System . out . print ( n ) ; else { System . out . print ( n - ( n % power ) - 1 ) ; } } else System . out . print ( n ) ; } public static void main ( String [ ] args ) { long n = 1029 , d = 102 ; long num = dig ( n ) ; required_number ( num , n , d ) ; } }
Egg Dropping Puzzle with 2 Eggs and K Floors | Java program to find optimal number of trials for k floors and 2 eggs . ; Driver code
import java . io . * ; class GFG { static int twoEggDrop ( int k ) { return ( int ) Math . ceil ( ( - 1.0 + Math . sqrt ( 1 + 8 * k ) ) / 2.0 ) ; } public static void main ( String [ ] args ) { int k = 100 ; System . out . println ( twoEggDrop ( k ) ) ; } }
Program to find the Area and Volume of Icosahedron | Java program to find the Area and volume of Icosahedron ; Function to find area of Icosahedron ; Formula to calculating area ; Function to find volume of Icosahedron ; Formula to calculating volume ; Driver code ; Function call to find area of Icosahedron . ; Function call to find volume of Icosahedron .
import java . io . * ; class GFG { static float findArea ( float a ) { float area ; area = ( float ) ( 5 * Math . sqrt ( 3 ) * a * a ) ; return area ; } static float findVolume ( float a ) { float volume ; volume = ( float ) ( ( ( float ) 5 / 12 ) * ( 3 + Math . sqrt ( 5 ) ) * a * a * a ) ; return volume ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( " Area : ▁ " + findArea ( a ) ) ; System . out . println ( " Volume : ▁ " + findVolume ( a ) ) ; } }
Total number of ways to place X and Y at n places such that no two X are together | Java program to find the number of ways Calculate total ways to place ' x ' and ' y ' at n places such that no two ' x ' are together ; Function to return number of ways ; for n = 1 ; for n = 2 ; iterate to find Fibonacci term ; total number of places
public class GFG { static int ways ( int n ) { int first = 2 ; int second = 3 ; int res = 0 ; for ( int i = 3 ; i <= n ; i ++ ) { res = first + second ; first = second ; second = res ; } return res ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( " Total ▁ ways ▁ are : ▁ " + ways ( n ) ) ; } }
Number of digits in N factorial to the power N | Java program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code
import java . io . * ; class GFG { static int countDigits ( int n ) { double ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += Math . log10 ( i ) ; ans = ans * n ; return 1 + ( int ) Math . floor ( ans ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countDigits ( n ) + "NEW_LINE"); } }
Program to convert centimeter into meter and kilometer | Java program to convert centimeter into meter and kilometer ; Driver Code ; Converting centimeter into meter and kilometer
import java . io . * ; class GFG { public static void main ( String [ ] args ) { double cm , meter , kilometer ; cm = 1000 ; meter = cm / 100.0 ; kilometer = cm / 100000.0 ; System . out . println ( " Length ▁ in ▁ meter ▁ = ▁ " + meter + " m " ) ; System . out . println ( " Length ▁ in ▁ Kilometer ▁ = ▁ " + kilometer + " km " ) ; } }
Check if two people starting from different points ever meet | Java program to find if two people starting from different positions ever meet or not . ; If speed of a person at a position before other person is smaller , then return false . ; Making sure that x1 is greater ; checking if relative speed is a factor of relative distance or not ;  ; Driver code
public class GFG { static boolean everMeet ( int x1 , int x2 , int v1 , int v2 ) { if ( x1 < x2 && v1 <= v2 ) { return false ; } if ( x1 > x2 && v1 >= v2 ) { return false ; } if ( x1 < x2 ) { swap ( x1 , x2 ) ; swap ( v1 , v2 ) ; } return ( ( x1 - x2 ) % ( v1 - v2 ) == 0 ) ; } static void swap ( int a , int b ) { int t = a ; a = b ; b = t ; } public static void main ( String [ ] args ) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if ( everMeet ( x1 , x2 , v1 , v2 ) ) { System . out . printf ( " Yes " ) ; } else { System . out . printf ( " No " ) ; } } }
Count of distinct GCDs among all the non | Java program for the above approach ;  ; Function to calculate the number of distinct GCDs among all non - empty subsequences of an array ; Variables to store the largest element in array and the required count ; Map to store whether a number is present in A ; Calculate largest number in A and mapping A to Mp ; Iterate over all possible values of GCD ; Variable to check current GCD ; Iterate over all multiples of i ; If j is present in A ; Calculate gcd of all encountered multiples of i ; Current GCD is possible ; Return answer ; Driver code ; Input ; Function calling
import java . util . * ; class GFG { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int distinctGCDs ( int [ ] arr , int N ) { int M = - 1 , ans = 0 ; HashMap < Integer , Integer > Mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . max ( M , arr [ i ] ) ; if ( Mp . containsKey ( arr [ i ] ) ) Mp . put ( arr [ i ] , 1 ) ; else Mp . put ( arr [ i ] , 0 ) ; } for ( int i = 1 ; i <= M ; i ++ ) { int currGcd = 0 ; for ( int j = i ; j <= M ; j += i ) { if ( Mp . containsKey ( j ) ) { currGcd = gcd ( currGcd , j ) ; if ( currGcd == i ) { ans ++ ; break ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 11 , 14 , 6 , 12 } ; int N = arr . length ; System . out . println ( distinctGCDs ( arr , N ) ) ; } }
Find ceil of a / b without using ceil ( ) function | Java program to find ceil ( a / b ) without using ceil ( ) function ; Driver Code ; taking input 1 ; example of perfect division taking input 2
class GFG { public static void main ( String args [ ] ) { int a = 4 ; int b = 3 ; int val = ( a + b - 1 ) / b ; System . out . println ( " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; val = ( a + b - 1 ) / b ; System . out . println ( " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }