codeparrot/xlcost-text-to-code huggingface dataset:
Multiplicative Congruence method for generating Pseudo Random Numbers | Java implementation of the above approach ; Function to generate random numbers ; Initialize the seed state ; Traverse to generate required numbers of random numbers ; Follow the multiplicative congruential method ; Driver code ; Seed value ; Modulus parameter ; Multiplier term ; Number of Random numbers to be generated ; To store random numbers ; Function Call ; Print the generated random numbers
import java . util . * ; class GFG { static void multiplicativeCongruentialMethod ( int Xo , int m , int a , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m ; } } public static void main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int a = 7 ; int noOfRandomNums = 10 ; int [ ] randomNums = new int [ noOfRandomNums ] ; multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }
Product of divisors of a number from a given list of its prime factors | Java Program to implement the above approach ; Function to calculate ( a ^ b ) % m ; Function to calculate and return the product of divisors ; Stores the frequencies of prime divisors ; Iterate over the prime divisors ; Update the product ; Update the count of divisors ; Driver Code
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int power ( int a , int b , int m ) { a %= m ; int res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( ( res % m ) * ( a % m ) ) % m ; a = ( ( a % m ) * ( a % m ) ) % m ; b >>= 1 ; } return res % m ; } static int productOfDivisors ( int p [ ] , int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . containsKey ( p [ i ] ) ) prime . put ( p [ i ] , prime . get ( p [ i ] ) + 1 ) ; else prime . put ( p [ i ] , 1 ) ; } int product = 1 , d = 1 ; for ( Map . Entry < Integer , Integer > itr : prime . entrySet ( ) ) { int val = power ( itr . getKey ( ) , ( itr . getValue ( ) ) * ( itr . getValue ( ) + 1 ) / 2 , MOD ) ; product = ( power ( product , itr . getValue ( ) + 1 , MOD ) * power ( val , d , MOD ) ) % MOD ; d = ( d * ( itr . getValue ( ) + 1 ) ) % ( MOD - 1 ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 11 } ; int n = arr . length ; System . out . println ( productOfDivisors ( arr , n ) ) ; } }
Print all proper fractions with denominators less than equal to N | Java program to implement the above approach ; Function to print all proper fractions ; If the numerator and the denominator are coprime ; Driver code
class GFG { static void printFractions ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( __gcd ( i , j ) == 1 ) { String a = String . valueOf ( i ) ; String b = String . valueOf ( j ) ; System . out . print ( a + " / " + b + " , ▁ " ) ; } } } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 3 ; printFractions ( n ) ; } }
Maximum number of objects that can be created as per given conditions | Java program for the above problem ; Function for finding the maximum number of objects from N type - 1 and M type - 2 items ; Storing minimum of N and M ; Storing maximum number of objects from given items ; Driver Code
class GFG { static int numberOfObjects ( int N , int M ) { int initial = Math . min ( N , M ) ; int last = ( N + M ) / 3 ; return Math . min ( initial , last ) ; } public static void main ( String [ ] args ) { int N = 8 ; int M = 7 ; System . out . println ( numberOfObjects ( N , M ) ) ; } }
Square root of a number by Repeated Subtraction method | Java implementation of the above approach ; Function to return the square root of the given number ; Subtract n - th odd number ; Return the result ; Driver code
class GFG { public static int SquareRoot ( int num ) { int count = 0 ; for ( int n = 1 ; n <= num ; n += 2 ) { num = num - n ; count += 1 ; if ( num == 0 ) break ; } return count ; } public static void main ( String [ ] args ) { int N = 81 ; System . out . println ( SquareRoot ( N ) ) ; } }
Maximize count of distinct elements possible in an Array from the given operation | Java program to find the maximum possible distinct elements that can be obtained from the array by performing the given operations ; Function to find the gcd of two numbers ; Function to calculate and return the count of maximum possible distinct elements in the array ; Find the maximum element ; Base Case ; Finding the gcd of first two element ; Calculate Gcd of the array ; Return the total count of distinct elements ; Driver code
import java . util . * ; class GFG { static int gcd ( int x , int y ) { if ( x == 0 ) return y ; return gcd ( y % x , x ) ; } static int findDistinct ( int arr [ ] , int n ) { int maximum = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; if ( n == 1 ) return 1 ; if ( n == 2 ) { return ( maximum / gcd ( arr [ 0 ] , arr [ 1 ] ) ) ; } int k = gcd ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { k = gcd ( k , arr [ i ] ) ; } return ( maximum / k ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 } ; int n = arr . length ; System . out . println ( findDistinct ( arr , n ) ) ; } }
Find indices of all local maxima and local minima in an Array | Java program for the above approach ; Function to find all the local maxima and minima in the given array arr [ ] ; Empty vector to store points of local maxima and minima ; Checking whether the first point is local maxima or minima or none ; Iterating over all points to check local maxima and local minima ; Condition for local minima ; Condition for local maxima ; Checking whether the last point is local maxima or minima or none ; Print all the local maxima and local minima indexes stored ; Driver code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { public static void findLocalMaximaMinima ( int n , int [ ] arr ) { Vector < Integer > mx = new Vector < Integer > ( ) ; Vector < Integer > mn = new Vector < Integer > ( ) ; if ( arr [ 0 ] > arr [ 1 ] ) mx . add ( 0 ) ; else if ( arr [ 0 ] < arr [ 1 ] ) mn . add ( 0 ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] > arr [ i ] ) && ( arr [ i ] < arr [ i + 1 ] ) ) mn . add ( i ) ; else if ( ( arr [ i - 1 ] < arr [ i ] ) && ( arr [ i ] > arr [ i + 1 ] ) ) mx . add ( i ) ; } if ( arr [ n - 1 ] > arr [ n - 2 ] ) mx . add ( n - 1 ) ; else if ( arr [ n - 1 ] < arr [ n - 2 ] ) mn . add ( n - 1 ) ; if ( mx . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " maxima ▁ are ▁ : ▁ " ) ; for ( Integer a : mx ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ " + " of ▁ Local ▁ Maxima ▁ " ) ; if ( mn . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " minima ▁ are ▁ : ▁ " ) ; for ( Integer a : mn ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ of ▁ " + " Local ▁ Maxima ▁ " ) ; } public static void main ( String [ ] args ) { int N = 9 ; int arr [ ] = { 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 } ; findLocalMaximaMinima ( N , arr ) ; } }
Smallest N digit number with none of its digits as its divisor | Java program for the given problem ; Function to calculate power ; Function to check if the N - digit number satisfies the given condition or not ; Getting the last digit ; Every number is divisible by 1 and dividing a number by 0 isn 't possible. Thus  numbers with 0 as a digit  must be discarded. ; If any digit divides the number , return false ; If no digit divides the number , return true ; ; Function to find the smallest number not divisible by any of its digits . ; Get the lower range of n digit number ; Get the high range of n digit number ; Check all the N - digit numbers ; If the first number to satify the constraints is found print it , set the flag value and break out of the loop . ; If no such digit found , return - 1 as per problem statement ; Driver code
import java . util . * ; class GFG { static int power ( int a , int b ) { if ( b == 0 ) return 1 ; if ( b == 1 ) return a ; int tmp = power ( a , b / 2 ) ; int result = tmp * tmp ; if ( b % 2 == 1 ) result *= a ; return result ; } static boolean check ( int n ) { int temp = n ; while ( temp > 0 ) { int last_digit = temp % 10 ; if ( last_digit == 0 last_digit == 1 ) return false ; if ( n % last_digit == 0 ) return false ; temp = temp / 10 ; } return true ; } static void solve ( int n ) { int L = power ( 10 , n - 1 ) ; int R = power ( 10 , n ) - 1 ; int flag = 0 ; for ( int i = L ; i <= R ; i ++ ) { boolean answer = check ( i ) ; if ( answer == true ) { System . out . println ( i ) ; flag ++ ; break ; } } if ( flag == 0 ) System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 4 ; solve ( N ) ; } }
Cost of creating smallest subsequence with sum of difference between adjacent elements maximum |  ; Initializing cost = 0 ; To store the removed element ; This will store the sum of the subsequence ; Checking all the element of the vector ; Storing the value of arr [ i ] in temp variable ; If the situation like arr [ i - 1 ] < arr [ i ] < arr [ i + 1 ] or arr [ i - 1 ] > arr [ i ] > arr [ i + 1 ] occur remove arr [ i ] i . e , temp from sequence ; Insert the element in the set removedElements ; Storing the value of arr [ i ] in temp ; Taking the element not in removedElements ; Adding the value of elements of subsequence ; If we have to remove the element then we need to add the cost associated with the element ; Printing the sum of the subsequence with minimum length possible ; Printing the cost incurred in creating subsequence ; Driver code ; Calling the function
import java . util . * ; class GFG { public static void costOfSubsequence ( int N , int [ ] arr , int [ ] costArray ) { int i , temp ; int cost = 0 ; Set < Integer > removedElements = new HashSet < Integer > ( ) ; int ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . add ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! ( removedElements . contains ( temp ) ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } System . out . print ( ans + " , ▁ " ) ; System . out . print ( cost ) ; } public static void main ( String [ ] args ) { int N ; N = 4 ; int [ ] arr = { 1 , 3 , 4 , 2 } ; int [ ] costArray = { 0 , 1 , 0 , 0 } ; costOfSubsequence ( N , arr , costArray ) ; } }
Count of natural numbers in range [ L , R ] which are relatively prime with N | Java code to count of natural numbers in range [ L , R ] which are relatively prime with N ; Container of all the primes up to sqrt ( n ) ; Function to calculate prime factors of n ; Run the sieve of Eratosthenes ; 0 ( false ) means prime , 1 ( true ) means not prime ; No even number is prime except for 2 ; All the multiples of each each prime numbers are non - prime ; Get all the primes in prime vector ; Count the number of numbers up to m which are divisible by given prime numbers ; Run from i = 000. . 0 to i = 111. . 1 or check all possible subsets of the array ; If the number of set bits is odd , then add to the number of multiples ; Function calculates all number not greater than ' m ' which are relatively prime with n . ; If square of the prime number is greater than ' n ' , it can ' t ▁ ▁ be ▁ a ▁ factor ▁ of ▁ ' n ' ; If prime is a factor of n then increment count ; Driver code
import java . util . * ; class GFG { static int maxN = 100000000 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void sieve ( int n ) { boolean [ ] check = new boolean [ 1000007 ] ; for ( int i = 0 ; i < 1000007 ; i ++ ) check [ i ] = false ; int i , j ; check [ 0 ] = false ; check [ 1 ] = true ; check [ 2 ] = false ; for ( i = 4 ; i <= n ; i += 2 ) check [ i ] = true ; for ( i = 3 ; i * i <= n ; i += 2 ) if ( ! check [ i ] ) { for ( j = i * i ; j <= n ; j += 2 * i ) check [ j ] = true ; } prime . add ( 2 ) ; for ( i = 3 ; i <= n ; i += 2 ) if ( ! check [ i ] ) prime . add ( i ) ; return ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int count ( int a [ ] , int n , int m ) { int [ ] parity = new int [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) parity [ i ] = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { int mult = 1 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) mult *= a [ j ] ; parity [ countSetBits ( i ) & 1 ] += ( m / mult ) ; } return parity [ 1 ] - parity [ 0 ] ; } static int countRelPrime ( int n , int m ) { int [ ] a = new int [ 20 ] ; int i = 0 , j = 0 ; int pz = prime . size ( ) ; while ( n != 1 && i < pz ) { if ( ( int ) prime . get ( i ) * ( int ) prime . get ( i ) > n ) break ; if ( n % prime . get ( i ) == 0 ) { a [ j ] = ( int ) prime . get ( i ) ; j ++ ; } while ( n % prime . get ( i ) == 0 ) n /= prime . get ( i ) ; i ++ ; } if ( n != 1 ) { a [ j ] = n ; j ++ ; } return m - count ( a , j , m ) ; } static void countRelPrimeInRange ( int n , int l , int r ) { sieve ( ( int ) Math . sqrt ( maxN ) ) ; int result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { int N = 7 , L = 3 , R = 9 ; countRelPrimeInRange ( N , L , R ) ; } }
XOR of a subarray ( range of elements ) | Set 2 | Java program to find XOR in a range from L to R ; Function to find XOR in a range from L to R ; Compute xor from arr [ 0 ] to arr [ i ] ; Process every query in constant time ; If L == 0 ; Driver Code ; query [ ]
class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_Xor ( int arr [ ] , pair query [ ] , int N , int Q ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } int ans = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] . first == 0 ) ans = arr [ query [ i ] . second ] ; else ans = arr [ query [ i ] . first - 1 ] ^ arr [ query [ i ] . second ] ; System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 } ; int N = 8 ; int Q = 2 ; pair query [ ] = { new pair ( 1 , 4 ) , new pair ( 3 , 7 ) } ; find_Xor ( arr , query , N , Q ) ; } }
Generate an alternate increasing and decreasing Array | Java program to generate an increasing and decreasing array ; Function that returns generated array ; Dynamically allocate array ; START = 0 , END = N ; Iterate over array ; if Str [ i ] == ' I ' assign arr [ i ] as START and increment START ; if str [ i ] == ' D ' assign arr [ i ] as END and decrement END ; Assign A [ N ] as START ; Return starting address of array A ; Driver code
class GFG { static int [ ] DiStirngMatch ( String Str ) { int N = Str . length ( ) ; int [ ] arr = new int [ N + 1 ] ; int START = 0 , END = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( Str . charAt ( i ) == ' I ' ) arr [ i ] = START ++ ; if ( Str . charAt ( i ) == ' D ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } public static void main ( String [ ] args ) { String Str = " IDID " ; int N = Str . length ( ) ; int [ ] ptr = DiStirngMatch ( Str ) ; for ( int i = 0 ; i <= N ; i ++ ) System . out . print ( ptr [ i ] + " ▁ " ) ; } }
Count of square free divisors of a given number | Java program to find the square free divisors of a given number ; The function to check if a number is prime or not ; If the number is even then its not prime ; Driver code ; Stores the count of distinct prime factors ; Print the number of square - free divisors
import java . util . * ; class GFG { static boolean IsPrime ( int i ) { if ( i % 2 == 0 && i != 2 ) return false ; else { for ( int j = 3 ; j <= Math . sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) return false ; } return true ; } } public static void main ( String [ ] args ) { int c = 0 ; int N = 72 ; for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( IsPrime ( i ) ) { if ( N % i == 0 ) { c ++ ; if ( IsPrime ( N / i ) && i != ( N / i ) ) c ++ ; } } } System . out . print ( Math . pow ( 2 , c ) - 1 ) ; } }
Generate Array whose difference of each element with its left yields the given Array | Java implementation of the above approach ; Function to find the sequence ; Initializing 1 st element ; Creating sequence in terms of x ; Finding min element ; Finding value of x ; Creating original sequence ; Output original sequence ; Driver code
class GFG { static void find_seq ( int arr [ ] , int m , int n ) { int b [ ] = new int [ n ] ; int x = 0 ; b [ 0 ] = x ; for ( int i = 0 ; i < n - 1 ; i ++ ) { b [ i + 1 ] = x + arr [ i ] + b [ i ] ; } int mn = n ; for ( int i = 0 ; i < n ; i ++ ) { mn = Math . min ( mn , b [ i ] ) ; } x = 1 - mn ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] += x ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( b [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = new int [ ] { - 2 , 1 } ; int M = arr . length ; find_seq ( arr , M , N ) ; } }
Check whether count of odd and even factors of a number are equal | Java solution for the above approach ; Function to check condition ; Initialize even_count and od_count ; Loop runs till square root ; Driver Code
class GFG { static void isEqualFactors ( int N ) { int ev_count = 0 , od_count = 0 ; for ( int i = 1 ; i <= Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count == od_count ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 10 ; isEqualFactors ( N ) ; } }
Check whether count of odd and even factors of a number are equal | JAVA code for the above approach ; Function to check condition ; Driver code
public class GFG { static void isEqualFactors ( int N ) { if ( ( N % 2 == 0 ) && ( N % 4 != 0 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int N = 10 ; isEqualFactors ( N ) ; N = 125 ; isEqualFactors ( N ) ; } }
Count of all values of N in [ L , R ] such that count of primes upto N is also prime | Java implementation of the approach ; Function to count the number of crazy primes in the given range [ L , R ] ; Stores all primes ; Stores count of primes ; Stores if frequency of primes is a prime or not upto each index ; Sieve of Eratosthenes ; Count primes ; If i is a prime ; Stores frequency of primes ; If the frequency of primes is a prime ; Increase count of required numbers ; Return the required count ; Driver code ; Given range ; Function call
class GFG { static int count_crazy_primes ( int L , int R ) { int prime [ ] = new int [ R + 1 ] ; int countPrime [ ] = new int [ R + 1 ] ; int freqPrime [ ] = new int [ R + 1 ] ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( int p = 2 ; p * p <= R ; p ++ ) { if ( prime [ p ] == 0 ) { for ( int i = p * p ; i <= R ; i += p ) prime [ i ] = 1 ; } } for ( int i = 1 ; i <= R ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] ; if ( prime [ i ] != 0 ) { countPrime [ i ] ++ ; } } for ( int i = 1 ; i <= R ; i ++ ) { freqPrime [ i ] = freqPrime [ i - 1 ] ; if ( prime [ countPrime [ i ] ] != 0 ) { freqPrime [ i ] ++ ; } } return ( freqPrime [ R ] - freqPrime [ L - 1 ] ) ; } public static void main ( String [ ] args ) { int L = 4 , R = 12 ; System . out . println ( count_crazy_primes ( L , R ) ) ; } }
Largest N digit number in Base B | Java program for the approach ; Function to print the largest N - digit numbers of base b ; Find the largest N digit number in base b using the formula B ^ N - 1 ; Print the largest number ; Driver Code ; Given Number and Base ; Function Call
import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double largest = Math . pow ( b , n ) - 1 ; System . out . println ( largest ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }
Generate an Array such with elements maximized through swapping bits | Java implementation to find maximum sequence by swapping bits ; Function to generate the maximized array elements ; Traverse the array ; Iterate to count set and unset bits ; Count of unset bits ; Count of set bits ; Bitwise right shift ; Shifting all 1 ' s ▁ to ▁ MSB ▁ ▁ and ▁ 0' s to LSB ; Driver Code
class GFG { public static void maximizedArray ( int arr [ ] , int N ) { int num , i = 0 ; for ( int l = N ; l > 0 ; l -- ) { num = arr [ i ] ; int one = 0 ; int zero = 0 ; while ( num != 0 ) { if ( num % 2 == 0 ) { zero ++ ; } else { one ++ ; } num = num >> 1 ; } for ( int j = zero ; j < ( one + zero ) ; j ++ ) { num += ( 1 << j ) ; } System . out . print ( num ) ; i ++ ; if ( N > 0 ) System . out . print ( " , ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 15 , 9 , 10 , 14 } ; int N = arr . length ; maximizedArray ( arr , N ) ; } }
Find the ratio of LCM to GCD of a given Array | Java Program to implement above approach ; Function to calculate and return GCD of the given array ; Initialise GCD ; Once GCD is 1 , it will always be 1 with all other elements ; Return GCD ; Function to calculate and return LCM of the given array ; Initialise LCM ; LCM of two numbers is evaluated as [ ( a * b ) / gcd ( a , b ) ] ; Return LCM ; Function to print the ratio of LCM to GCD of the given array ; Driver Code
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findGCD ( int arr [ ] , int n ) { int gcd = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( arr [ i ] , gcd ) ; if ( gcd == 1 ) { return 1 ; } } return gcd ; } static int findLCM ( int arr [ ] , int n ) { int lcm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { lcm = ( ( ( arr [ i ] * lcm ) ) / ( __gcd ( arr [ i ] , lcm ) ) ) ; } return lcm ; } static void findRatio ( int arr [ ] , int n ) { int gcd = findGCD ( arr , n ) ; int lcm = findLCM ( arr , n ) ; System . out . print ( ( lcm / gcd ) ) ; System . out . print ( " : 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 6 , 12 , 36 } ; int N = 3 ; findRatio ( arr , N ) ; } }
Minimum operations required to make all Array elements divisible by K | Java implementation to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Function to find the minimum number of moves required to update the array such that each of its element is divisible by K ; Initialize Map data structure ; Iterate for all the elements of the array ; Calculate the value to be added ; Check if the value equals to 0 then simply continue ; Check if the value to be added is present in the map ; Update the answer ; Print the required result We need to add 1 to maxX because we cant ignore the first move where initially X = 0 and we need to increase it by 1 to make some changes in array ; Driver code
import java . util . * ; class GFG { static void compute ( int a [ ] , int N , int K ) { Map < Long , Long > eqVal = new HashMap < Long , Long > ( ) ; long maxX = 0 ; for ( int i = 0 ; i < N ; i ++ ) { long val = a [ i ] % K ; val = ( val == 0 ? 0 : K - val ) ; if ( val == 0 ) continue ; if ( eqVal . containsKey ( val ) ) { long numVal = eqVal . get ( val ) ; maxX = Math . max ( maxX , val + ( K * numVal ) ) ; eqVal . put ( val , eqVal . getOrDefault ( val , 0l ) + 1l ) ; } else { eqVal . put ( val , 1l ) ; maxX = Math . max ( maxX , val ) ; } } System . out . println ( maxX == 0 ? 0 : maxX + 1 ) ; } public static void main ( String [ ] args ) { int K = 3 ; int a [ ] = { 1 , 2 , 2 , 18 } ; int N = a . length ; compute ( a , N , K ) ; } }