codeparrot/xlcost-text-to-code huggingface dataset:
Ramanujan Prime | Java program to find Ramanujan numbers ; FUnction to return a vector of primes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Print all prime numbers ; Function to find number of primes less than or equal to x ; Binary search to find out number of primes less than or equal to x ; Function to find the nth ramanujan prime ; For n >= 1 , a ( n ) < 4 * n * log ( 4 n ) ; We start from upperbound and find where pi ( i ) - pi ( i / 2 ) < n the previous number being the nth ramanujan prime ; Function to find first n Ramanujan numbers ; Get the prime numbers ; Driver code
import java . util . * ; class GFG { static int MAX = 1000000 ; static Vector < Integer > addPrimes ( ) { int n = MAX ; boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) ans . add ( p ) ; return ans ; } static int pi ( int x , Vector < Integer > v ) { int l = 0 , r = v . size ( ) - 1 , m , in = - 1 ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( v . get ( m ) <= x ) { in = m ; l = m + 1 ; } else { r = m - 1 ; } } return in + 1 ; } static int Ramanujan ( int n , Vector < Integer > v ) { int upperbound = ( int ) ( 4 * n * ( Math . log ( 4 * n ) / Math . log ( 2 ) ) ) ; for ( int i = upperbound ; ; i -- ) { if ( pi ( i , v ) - pi ( i / 2 , v ) < n ) return 1 + i ; } } static void Ramanujan_Numbers ( int n ) { int c = 1 ; Vector < Integer > v = addPrimes ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( Ramanujan ( i , v ) ) ; if ( i != n ) System . out . print ( " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; Ramanujan_Numbers ( n ) ; } }
Quadruplet pair with XOR zero in the given Array | Java implementation of the approach ; Function that returns true if the array contains a valid quadruplet pair ; We can always find a valid quadruplet pair for array size greater than MAX ; For smaller size arrays , perform brute force ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MAX = 130 ; static boolean validQuadruple ( int arr [ ] , int n ) { if ( n >= MAX ) return true ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) for ( int l = k + 1 ; l < n ; l ++ ) { if ( ( arr [ i ] ^ arr [ j ] ^ arr [ k ] ^ arr [ l ] ) == 0 ) { return true ; } } return false ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 1 , 0 , 2 , 3 , 7 } ; int n = arr . length ; if ( validQuadruple ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Quadruplet pair with XOR zero in the given Array | Java implementation of the approach ; Driver code
import java . util . HashMap ; import java . util . LinkedList ; import java . util . List ; import java . util . Map ; public class QuadrapuleXor { static boolean check ( int arr [ ] ) { int n = arr . length ; if ( n < 4 ) return false ; if ( n >= 130 ) return true ; Map < Integer , List < Integer > > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int k = arr [ i ] ^ arr [ j ] ; if ( ! map . containsKey ( k ) ) map . put ( k , new LinkedList < > ( ) ) ; List < Integer > data = map . get ( k ) ; if ( ! data . contains ( i ) && ! data . contains ( j ) ) { data . add ( i ) ; data . add ( j ) ; if ( data . size ( ) >= 4 ) return true ; map . put ( k , data ) ; } } } return false ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 1 , 0 , 2 , 3 , 7 } ; if ( check ( arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants | Java program to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Function to returns factorial of n ; Function to find nCr ; Function to find the number of words of X vowels and Y consonants can be formed from M vowels and N consonants ; Driver code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int NumberOfWays ( int X , int Y , int M , int N ) { return fact ( X + Y ) * nCr ( M , X ) * nCr ( N , Y ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int X = 2 , Y = 2 , M = 3 , N = 3 ; System . out . println ( NumberOfWays ( X , Y , M , N ) ) ; } }
Program for sum of cosh ( x ) series upto Nth term | Java program for the sum of cosh ( x ) series ; function to return the factorial of a number ; function to return the sum of the series ; Driver code
import java . util . * ; class GFG { static int fact ( int n ) { int i = 1 , fac = 1 ; for ( i = 1 ; i <= n ; i ++ ) fac = fac * i ; return fac ; } static double log_Expansion ( double x , int n ) { double sum = 0 ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + Math . pow ( x , 2 * i ) / fact ( 2 * i ) ; } return sum ; } public static void main ( String [ ] args ) { double x = 1 ; int n = 10 ; System . out . println ( log_Expansion ( x , n ) ) ; } }
Find prime numbers in the first half and second half of an array | Java program to print the prime numbers in the first half and second half of an array ; Function to check if a number is prime or not ; Function to find whether elements are prime or not ; Traverse in the given range ; Check if a number is prime or not ; Function to print the prime numbers in the first half and second half of an array ; Driver Code ; Function call
import java . util . * ; class GFG { static boolean prime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void prime_range ( int start , int end , int [ ] a ) { for ( int i = start ; i < end ; i ++ ) { if ( prime ( a [ i ] ) ) System . out . print ( a [ i ] + " ▁ " ) ; } } static void Print ( int arr [ ] , int n ) { System . out . print ( " Prime ▁ numbers ▁ in ▁ the ▁ first ▁ half ▁ are ▁ " ) ; prime_range ( 0 , n / 2 , arr ) ; System . out . println ( ) ; System . out . print ( " Prime ▁ numbers ▁ in ▁ the ▁ second ▁ half ▁ are ▁ " ) ; prime_range ( n / 2 , n , arr ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 10 , 15 , 17 , 21 , 23 } ; int n = arr . length ; Print ( arr , n ) ; } }
Count of elements that can be deleted without disturbing the mean of the initial array | Java implementation of the approach ; Function to find the elements which do not change the mean on removal ; To store the sum of the array elements ; To store the initial mean ; to store the count of required elements ; Iterate over the array ; Finding the new mean ; If the new mean equals to the initial mean ; Driver code
class GFG { static int countElements ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; float mean = ( float ) sum / n ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float newMean = ( float ) ( sum - arr [ i ] ) / ( n - 1 ) ; if ( newMean == mean ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countElements ( arr , n ) ) ; } }
Find maximum xor of k elements in an array | Java implementation of the approach ; Function to return the maximum xor for a subset of size j from the given array ; If the subset is complete then return the xor value of the selected elements ; Return if already calculated for some mask and j at the i 'th index ; Initialize answer to 0 ; If we can still include elements in our subset include the i 'th element ; Exclude the i 'th element  ans store the max of both operations ; Driver code
import java . util . * ; class GFG { static int MAX = 10000 ; static int MAX_ELEMENT = 50 ; static int [ ] [ ] [ ] dp = new int [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; static int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != - 1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = Math . max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; for ( int i = 0 ; i < MAX_ELEMENT ; i ++ ) { for ( int j = 0 ; j < MAX_ELEMENT ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } System . out . println ( Max_Xor ( arr , 0 , k , 0 , n ) ) ; } }
Find the prime P using given four integers | Java program to possible prime number ; Function to check if a number is prime or not ; Function to find possible prime number ; Find a possible prime number ; Last condition ; Driver code ; Function call
import java . util . * ; class GFG { static boolean Prime ( int n ) { for ( int j = 2 ; j <= Math . sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } static int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= Math . max ( Math . sqrt ( n ) , Math . sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; System . out . println ( find_prime ( x , xsqmodp , y , ysqmodp ) ) ; } }
Time taken per hour for stoppage of Car | Java implementation of the approach ; Function to return the time taken per hour for stoppage ; Driver code
import java . util . * ; class GFG { static int numberOfMinutes ( int S , int S1 ) { int Min = 0 ; Min = ( int ) ( ( ( S - S1 ) / Math . floor ( S ) ) * 60 ) ; return Min ; } public static void main ( String [ ] args ) { int S = 30 , S1 = 10 ; System . out . println ( numberOfMinutes ( S , S1 ) + " ▁ min " ) ; } }
Removing a number from array without changing its arithmetic mean | Java program to remove a number from the array without changing its arithmetic mean ; Function to remove a number from the array without changing its arithmetic mean ; Find sum of all elements ; If mean is an integer ; Check if mean is present in the array or not ; Driver code
import java . io . * ; class GFG { static int FindElement ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + a [ i ] ; if ( sum % n == 0 ) { int m = sum / n ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == m ) return m ; } return - 1 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; System . out . print ( FindElement ( a , n ) ) ; } }
Sum of the Tan ( x ) expansion upto N terms | Java program to find tan ( x ) expansion ; Function to find factorial of a number ; To store factorial of a number ; Return the factorial of a number ; Function to find tan ( x ) upto n terms ; To store value of the expansion ; This loops here calculate Bernoulli number which is further used to get the coefficient in the expansion of tan x ; Print the value of expansion ; Driver code ; Function call
class GFG { static int fac ( int num ) { if ( num == 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } static void Tanx_expansion ( int terms , int x ) { double sum = 0 ; for ( int i = 1 ; i <= terms ; i += 1 ) { double B = 0 ; int Bn = 2 * i ; for ( int k = 0 ; k <= Bn ; k ++ ) { double temp = 0 ; for ( int r = 0 ; r <= k ; r ++ ) temp = temp + Math . pow ( - 1 , r ) * fac ( k ) * Math . pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( ( double ) ( k + 1 ) ) ; } sum = sum + Math . pow ( - 4 , i ) * ( 1 - Math . pow ( 4 , i ) ) * B * Math . pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } System . out . printf ( " % .9f " , sum ) ; } public static void main ( String [ ] args ) { int n = 6 , x = 1 ; Tanx_expansion ( n , x ) ; } }
Minimum elements to be inserted in Array to make adjacent differences equal | Java program for the above approach ; Function to find gcd of two numbers ; Function to calculate minimum numbers to be inserted to make equal differences between two consecutive elements ; Check if there is only one element in the array then answer will be 0 ; Calculate difference between first and second element of array ; If there is only two elements in the array then gcd of differences of consecutive elements of array will be equal to difference of first and second element of the array ; Loop to calculate the gcd of the differences between consecutive elements of the array ; Loop to calculate the elements to be inserted ; Return the answer ; Driver code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int minimum_elements ( int n , int arr [ ] ) { if ( n < 3 ) return 0 ; int g , ans = 0 , diff , cnt ; diff = arr [ 1 ] - arr [ 0 ] ; g = diff ; for ( int i = 2 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; g = gcd ( g , diff ) ; } for ( int i = 1 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; cnt = diff / g ; ans += ( cnt - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 10 , 12 , 16 } ; int n = arr . length ; System . out . println ( minimum_elements ( n , arr ) ) ; } }
Check if a number is Fermat Pseudoprime | Java program to check if N is Fermat pseudoprime to the base A or not ; Function to check if the given number is composite ; Check if there is any divisor of n less than sqrt ( n ) ; Effectively calculate ( x ^ y ) modulo mod ; Initialize result ; If power is odd , then update the answer ; Square the number and reduce the power to its half ; Return the result ; Function to check for Fermat Pseudoprime ; If it is composite and satisfy Fermat criterion ; Else return 0 ; Driver Code ; Function call
class GFG { static boolean checkcomposite ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return true ; } } return false ; } static int power ( int x , int y , int mod ) { int res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) { res = ( res * x ) % mod ; } y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int Check ( int n , int a ) { if ( a > 1 && checkcomposite ( n ) && power ( a , n - 1 , n ) == 1 ) { return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 645 ; int a = 2 ; System . out . println ( Check ( N , a ) ) ; } }
Largest and smallest digit of a number | Java program to largest and smallest digit of a number ; Function to the largest and smallest digit of a number ; Find the largest digit ; Find the smallest digit ; Driver code ; Function call
import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static void Digits ( int n ) { int largest = 0 ; int smallest = 9 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } System . out . println ( largest + " ▁ " + smallest ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 2346 ; Digits ( n ) ; } }
Probability of distributing M items among X bags such that first bag contains N items | Java program to find probability of first bag to contain N items such that M items are distributed among X bags ; Function to find factorial of a number ; Function to find nCr ; Function to find probability of first bag to contain N items such that M items are distributed among X bags ; Driver code ; Function call
class GFG { public static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } public static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } public static float Probability ( int M , int N , int X ) { return ( float ) ( nCr ( M - N - 1 , X - 2 ) / ( nCr ( M - 1 , X - 1 ) * 1.0 ) ) ; } public static void main ( String [ ] args ) { int M = 9 , X = 3 , N = 4 ; System . out . println ( Probability ( M , N , X ) ) ; } }
Count of N digit numbers possible which satisfy the given conditions | Java implementation of the approach ; Function to return the factorial of n ; Function to return the count of numbers possible ; Driver code
import java . io . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int Count_number ( int N ) { return ( N * fact ( N ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( Count_number ( N ) ) ; } }
Find the count of M character words which have at least one character repeated | Java implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nPr ; Function to return the total number of M length words which have at least a single character repeated more than once ; Driver code
class GFG { static int fact ( int n ) { if ( n <= 1 ) return 1 ; return n * fact ( n - 1 ) ; } static int nPr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countWords ( int N , int M ) { return ( int ) Math . pow ( N , M ) - nPr ( N , M ) ; } public static void main ( String [ ] args ) { int N = 10 , M = 5 ; System . out . print ( countWords ( N , M ) ) ; } }
Minimum and maximum possible length of the third side of a triangle | Java implementation of the approach ; Function to find the minimum and the maximum possible length of the third side of the given triangle ; Not a valid triangle ; Not a valid triangle ; Driver code
import java . io . * ; class GFG { static void find_length ( int s1 , int s2 ) { if ( s1 <= 0 s2 <= 0 ) { System . out . print ( - 1 ) ; return ; } int max_length = s1 + s2 - 1 ; int min_length = Math . max ( s1 , s2 ) - Math . min ( s1 , s2 ) + 1 ; if ( min_length > max_length ) { System . out . print ( - 1 ) ; return ; } System . out . println ( " Max ▁ = ▁ " + max_length ) ; System . out . print ( " Min ▁ = ▁ " + min_length ) ; } public static void main ( String [ ] args ) { int s1 = 8 , s2 = 5 ; find_length ( s1 , s2 ) ; } }
Number formed by the rightmost set bit in N | Java implementation of the approach ; Function to return the integer formed by taking the rightmost set bit in n ; n & ( n - 1 ) unsets the first set bit from the right in n ; Take xor with the original number The position of the ' changed ▁ bit ' will be set and rest will be unset ; Driver Code
class geeks { public static int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( firstSetBit ( n ) ) ; } }
Number of N length sequences whose product is M | Java implementation of the above approach ; Function to calculate the value of ncr effectively ; Initializing the result ; Multiply and divide simultaneously to avoid overflow ; Return the answer ; Function to return the number of sequences of length N such that their product is M ; Hashmap to store the prime factors of M ; Calculate the prime factors of M ; If i divides M it means it is a factor Divide M by i till it could be divided to store the exponent ; Increase the exponent count ; If the number is a prime number greater than sqrt ( M ) ; Initializing the ans ; Multiply the answer for every prime factor ; entry . getValue ( ) represents the exponent of every prime factor ; Return the result ; Driver code
import java . util . HashMap ; class geeks { public static int nCr ( int n , int r ) { int res = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { res *= ( n - r + i ) ; res /= i ; } return res ; } public static int NoofSequences ( int N , int M ) { HashMap < Integer , Integer > prime = new HashMap < > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( M ) ; i ++ ) { while ( M % i == 0 ) { if ( prime . get ( i ) == null ) prime . put ( i , 1 ) ; else { int x = prime . get ( i ) ; prime . put ( i , ++ x ) ; } M /= i ; } } if ( M > 1 ) { if ( prime . get ( M ) != null ) { int x = prime . get ( M ) ; prime . put ( M , ++ x ) ; } else prime . put ( M , 1 ) ; } int ans = 1 ; for ( HashMap . Entry < Integer , Integer > entry : prime . entrySet ( ) ) { ans *= ( nCr ( N + entry . getValue ( ) - 1 , N - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , M = 6 ; System . out . println ( NoofSequences ( N , M ) ) ; } }
Number of hours after which the second person moves ahead of the first person if they travel at a given speed | Java implementation of the above approach ; Function to return the number of hours for the second person to move ahead ; Time taken to equalize ; Time taken to move ahead ; Driver code
import java . io . * ; class GFG { static int findHours ( int a , int b , int k ) { if ( a >= b ) return - 1 ; int time = k / ( b - a ) ; time = time + 1 ; return time ; } public static void main ( String [ ] args ) { int a = 4 , b = 5 , k = 1 ; System . out . println ( findHours ( a , b , k ) ) ; } }
Number of ways of distributing N identical objects in R distinct groups with no groups empty | Java implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code
import java . io . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int NoOfDistributions ( int N , int R ) { return ncr ( N - 1 , R - 1 ) ; } public static void main ( String [ ] args ) { int N = 4 ; int R = 3 ; System . out . println ( NoOfDistributions ( N , R ) ) ; } }
Number of ways of distributing N identical objects in R distinct groups | Java implementation of the above approach ; Function to return the value of ncr effectively ; Initialize the answer ; Divide simultaneously by i to avoid overflow ; Function to return the number of ways to distribute N identical objects in R distinct objects ; Driver code ; Function call
import java . util . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int NoOfDistributions ( int N , int R ) { return ncr ( N + R - 1 , R - 1 ) ; } public static void main ( String [ ] args ) { int N = 4 , R = 3 ; System . out . println ( NoOfDistributions ( N , R ) ) ; } }