codeparrot/xlcost-text-to-code huggingface dataset:
Count of numbers whose sum of increasing powers of digits is equal to the number itself | Java implementation of the approach ; Function to return the count of digits of n ; Function to return the sum of increasing powers of N ; To store the required answer ; Count of digits in n which will be the power of the last digit ; While there are digits left ; Get the last digit ; Add the last digit after raising it to the required power ; Decrement the power for the previous digit ; Remove the last digit ; Function to return the count of integers which satisfy the given conditions ; If current element satisfies the given condition ; Driver code
class GFG { static int countDigits ( int n ) { int cnt = 0 ; while ( n > 0 ) { cnt ++ ; n /= 10 ; } return cnt ; } static int digitPowSum ( int n ) { int sum = 0 ; int pw = countDigits ( n ) ; while ( n > 0 ) { int d = n % 10 ; sum += Math . pow ( d , pw ) ; pw -- ; n /= 10 ; } return sum ; } static int countNum ( int n ) { int count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == digitPowSum ( i ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 200 ; System . out . println ( countNum ( n ) ) ; } }
Print all perfect squares from the given range | Java implementation of the approach ; Function to print all the perfect squares from the given range ; For every element from the range ; If current element is a perfect square ; Driver code
import java . io . * ; class GFG { static void perfectSquares ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( Math . sqrt ( i ) == ( int ) Math . sqrt ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }
Print all perfect squares from the given range | Java implementation of the approach ; Function to print all the perfect squares from the given range ; Getting the very first number ; First number 's square ; Next number is at the difference of ; While the perfect squares are from the range ; Print the perfect square ; Get the next perfect square ; Next odd number to be added ; Driver code
class GFG { static void perfectSquares ( float l , float r ) { int number = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int n2 = number * number ; number = ( number * 2 ) + 1 ; while ( ( n2 >= l && n2 <= r ) ) { System . out . print ( n2 + " ▁ " ) ; n2 = n2 + number ; number += 2 ; } } public static void main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }
Find the value of N XOR 'ed to itself K times | Java implementation of the approach ; Function to return n ^ n ^ ... k times ; Find the result ; Driver code
class GFG { static int xorK ( int n , int k ) { int res = n ; for ( int i = 1 ; i < k ; i ++ ) res = ( res ^ n ) ; return n ; } public static void main ( String [ ] args ) { int n = 123 , k = 3 ; System . out . print ( xorK ( n , k ) ) ; } }
Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | Java implementation of the approach ; Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code
import java . util . * ; class GFG { static void findRemainders ( long n ) { HashSet < Long > vc = new HashSet < Long > ( ) ; for ( long i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( n / i ) ; for ( long i = ( long ) ( n / Math . ceil ( Math . sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . add ( i ) ; for ( long it : vc ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }
Count of primes below N which can be expressed as the sum of two primes | Java implementation of the approach ; Function for Sieve of Eratosthenes ; false here indicates that it is not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p , set them to non - prime ; Function to return the count of primes less than or equal to n which can be expressed as the sum of two primes ; To store the required count ; If the integer is prime and it can be expressed as the sum of 2 and a prime number ; Driver code
class GFG { static int MAX = 100005 ; static boolean [ ] prime = new boolean [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int countPrimes ( int n ) { SieveOfEratosthenes ( ) ; int cnt = 0 ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . print ( countPrimes ( n ) ) ; } }
Print all palindrome dates between the given years | Java implementation of the approach ; Returns true if given year is valid ; Return true if year is a multiple pf 4 and not multiple of 100. OR year is multiple of 400. ; Returns true if given year is valid or not . ; If year , month and day are not in given range ; Handle February month with leap year ; Months of April , June , Sept and Nov must have number of days less than or equal to 30. ; Function to print the palindrome dates between the given years ; For every year ; Current year as a String ; To store the reverse of year ; Get the day and the month ; If the current palindrome date is valid ; Driver code
class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; static boolean isLeap ( int year ) { return ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) ) ; } static boolean isValidDate ( int d , int m , int y ) { if ( y > MAX_VALID_YR y < MIN_VALID_YR ) return false ; if ( m < 1 m > 12 ) return false ; if ( d < 1 d > 31 ) return false ; if ( m == 2 ) { if ( isLeap ( y ) ) return ( d <= 29 ) ; else return ( d <= 28 ) ; } if ( m == 4 m == 6 m == 9 m == 11 ) return ( d <= 30 ) ; return true ; } static void printPalindromeDates ( int y1 , int y2 ) { for ( int year = y1 ; year <= y2 ; year ++ ) { String str = String . valueOf ( year ) ; String rev = str ; rev = reverse ( rev ) ; int day = Integer . parseInt ( rev . substring ( 0 , 2 ) ) ; int month = Integer . parseInt ( rev . substring ( 2 , 2 + 2 ) ) ; if ( isValidDate ( day , month , year ) ) { System . out . print ( rev + str + "NEW_LINE"); } } } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int y1 = 2001 , y2 = 2005 ; printPalindromeDates ( y1 , y2 ) ; } }
Find an integer in the given range that satisfies the given conditions | Java implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code
class GFG { static int MAX = 10 ; static boolean distinctDigits ( int x ) { boolean [ ] present = new boolean [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = Integer . MIN_VALUE ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void main ( String [ ] args ) { int l = 50 , r = 60 ; System . out . print ( findK ( l , r ) ) ; } }
Restore a permutation from the given helper array | Java implementation of the approach ; Function to find the required permutation ; Each element in P ' is like a  cumulative sum in Q ; minval is the minimum value in P ' ; To check if each entry in P is from the range [ 1 , n ] ; Invalid permutation ; If a valid permutation exists ; Print the permutation ; No valid permutation ; Driver code
class GFG { static void findPerm ( int Q [ ] , int n ) { int minval = 0 , qsum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { qsum += Q [ i ] ; if ( qsum < minval ) minval = qsum ; } int [ ] P = new int [ n ] ; P [ 0 ] = 1 - minval ; boolean permFound = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { P [ i + 1 ] = P [ i ] + Q [ i ] ; if ( P [ i + 1 ] > n P [ i + 1 ] < 1 ) { permFound = false ; break ; } } if ( permFound ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( P [ i ] + " ▁ " ) ; } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int Q [ ] = { - 2 , 1 } ; int n = 1 + Q . length ; findPerm ( Q , n ) ; } }
Convert Decimal To Hexa | Java program to convert decimal to hexadecimal covering negative numbers ; Function to convert decimal no . to hexadecimal number ; map for decimal to hexa , 0 - 9 are straightforward , alphabets a - f used for 10 to 15. ; string to be returned ; check if num is 0 and directly return "0" ; if num > 0 , use normal technique as discussed in other post ; if num < 0 , we need to use the elaborated trick above , lets see this ; store num in a u_int , size of u_it is greater , it will be positive since msb is 0 ; use the same remainder technique . ; Driver Code
import java . util . * ; import java . util . HashMap ; import java . util . Map ; class GFG { static String Hex ( int num ) { HashMap < Integer , Character > m = new HashMap < Integer , Character > ( ) ; char digit = '0' ; char c = ' a ' ; for ( int i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m . put ( i , digit ) ; digit ++ ; } else { m . put ( i , c ) ; c ++ ; } } String res = " " ; if ( num == 0 ) { return "0" ; } if ( num > 0 ) { while ( num != 0 ) { res = m . get ( num % 16 ) + res ; num /= 16 ; } } else { int n = num ; while ( n != 0 ) { res = m . get ( n % 16 ) + res ; n /= 16 ; } } return res ; } public static void main ( String [ ] args ) { int x = 134 , y = - 1 , z = - 234 ; System . out . println ( " Hexa ▁ representation ▁ for " ) ; System . out . println ( x + " ▁ is ▁ " + Hex ( x ) ) ; System . out . println ( y + " ▁ is ▁ " + Hex ( y ) ) ; System . out . println ( z + " ▁ is ▁ " + Hex ( z ) ) ; } }
Find the player who will win the Coin game | Java program to find the player who wins the game ; Function to check the wining player ; As discussed in the above approach ; Driver Code
class GFG { static void findWinner ( int n ) { if ( ( n - 1 ) % 6 == 0 ) { System . out . println ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; } else { System . out . println ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; findWinner ( n ) ; } }
Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls | Java implementation of the approach ; To store the factorial and the factorial mod inverse of a number ; Function to find ( a ^ m1 ) % mod ; Function to find factorial of all the numbers ; Function to find the factorial mod inverse of all the numbers ; Function to return nCr ; Function to find ways to arrange K green balls among N balls such that we need exactly i moves to collect all K green balls ; Driver code ; Function call
import java . util . * ; class GFG { static final int N = 100005 ; static final int mod = ( int ) ( 1e9 + 7 ) ; static long [ ] factorial = new long [ N ] ; static long [ ] modinverse = new long [ N ] ; static long power ( long a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1L * a * a ) % mod ; else if ( m1 % 2 == 1 ) return ( 1L * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1L * factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = ( int ) ( power ( factorial [ N - 1 ] , mod - 2 ) % mod ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1 * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static long binomial ( int n , int r ) { if ( r > n ) return 0 ; long a = ( 1L * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1 * a * modinverse [ r ] ) % mod ; return a ; } static void arrange_balls ( int n , int k ) { factorialfun ( ) ; modinversefun ( ) ; for ( int i = 1 ; i <= k ; i ++ ) System . out . print ( ( 1L * binomial ( n - k + 1 , i ) * binomial ( k - 1 , i - 1 ) ) % mod + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 3 ; arrange_balls ( n , k ) ; } }
Minimum inversions required so that no two adjacent elements are same | Java implementation of the approach ; Function to return the minimum inversions required so that no two adjacent elements are same ; To store the inversions required to make the array { 1 , 0 , 1 , 0 , 1 , 0 , 1 , ... } and { 0 , 1 , 0 , 1 , 0 , 1 , 0 , ... } respectively ; Find all the changes required ; Return the required answer ; Driver code
class GFG { static int min_changes ( int a [ ] , int n ) { int ans_a = 0 , ans_b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . min ( ans_a , ans_b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } ; int n = a . length ; System . out . println ( min_changes ( a , n ) ) ; } }
Sum of all the numbers present at given level in Modified Pascal ’ s triangle | Java program to calculate sum of all the numbers present at given level in an Modified Pascal 's triangle ; Function to calculate sum ; Driver Code
class GFG { static void ans ( int n ) { if ( n == 1 ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } public static void main ( String [ ] args ) { int n = 2 ; ans ( n ) ; } }
Find if there exists multiple ways to draw line through ( x , y ) to cut rectangle in equal halfs | Java implementation of the approach ; Function that returns true if multiple lines are possible passing through ( x , y ) that divide the given rectangle into two equal parts ; If the point ( x , y ) is the centre of the rectangle ; Driver code
class GFG { static boolean isPossible ( int w , int h , int x , int y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } public static void main ( String [ ] args ) { int w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
Minimize the number by changing at most K digits | Java implementation of the approach ; Function to return the minimized number ; Total digits in the number ; If the string is empty or there are no operations to perform ; "0" is a valid number ; If the first digit is not already 1 then update it to 1 and decrement k ; While there are operations left and the number can still be updated ; If the current digit is not already 0 then update it to 0 and decrement k ; Return the minimised number ; Driver code
class GFG { static String minNum ( char num [ ] , int k ) { int len = num . length ; if ( len == 0 k == 0 ) { String num_str = new String ( num ) ; return num_str ; } if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } String num_str = new String ( num ) ; return num_str ; } public static void main ( String args [ ] ) { String num = "91945" ; int k = 3 ; System . out . println ( minNum ( num . toCharArray ( ) , k ) ) ; } }
Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M | Java program to find minimum number of steps to cover distance x ; Function to calculate the minimum number of steps required total steps taken is divisible by m and only 1 or 2 steps can be taken at a time ; If m > n ans is - 1 ; else discussed above approach ; Driver code
class GFG { static int minsteps ( int n , int m ) { if ( m > n ) { return - 1 ; } else { return ( ( n + 1 ) / 2 + m - 1 ) / m * m ; } } public static void main ( String [ ] args ) { int n = 17 , m = 4 ; int ans = minsteps ( n , m ) ; System . out . println ( ans ) ; } }
Compare numbers represented by Linked Lists | Java implementation of the approach ; Structure for a linked list node ; A helper function to remove zeros from the start of the linked list ; A helper function to find the length of linked list ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Allocate node ; Set the data ; Link the old list after the new node ; Set the head to point to the new node ; Function to compare the numbers represented as linked lists ; Remover leading zeroes from the linked lists ; Since the number represented by a has a greater length , it will be greater ; If the lengths of two numbers are equal we have to check their magnitudes ; If we reach here , then a and b are not null and their data is same , so move to next nodes in both lists ; If linked lists are identical , then we need to return zero ; Driver code ; The constructed linked lists are : a : 5 -> 6 -> 7 b : 2 -> 3 -> 3
class GFG { static class Node { int data ; Node next ; } ; static Node removeLeadingZeros ( Node a ) { if ( a != null && a . data == 0 ) return removeLeadingZeros ( a . next ) ; else return a ; } static int getSize ( Node a ) { int sz = 0 ; while ( a != null ) { a = a . next ; sz ++ ; } return sz ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int compare ( Node a , Node b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; int lenA = getSize ( a ) ; int lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return - 1 ; } while ( a != null && b != null ) { if ( a . data > b . data ) return 1 ; else if ( a . data < b . data ) return - 1 ; a = a . next ; b = b . next ; } return 0 ; } public static void main ( String [ ] args ) { Node a = null ; a = push ( a , 7 ) ; a = push ( a , 6 ) ; a = push ( a , 5 ) ; Node b = null ; b = push ( b , 3 ) ; b = push ( b , 3 ) ; b = push ( b , 2 ) ; System . out . println ( compare ( a , b ) ) ; } }
Sum of the updated array after performing the given operation | Java implementation of the approach ; Utility function to return the sum of the array ; Function to return the sum of the modified array ; Find the sum of the subarray arr [ i + 1. . . n - 1 ] ; Subtract the subarray sum ; Return the sum of the modified array ; Driver code
import java . util . * ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int subSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 40 , 25 , 12 , 10 } ; int n = arr . length ; System . out . println ( sumModArr ( arr , n ) ) ; } }
Find the minimum possible health of the winning player | Java implementation of the above approach ; Function to return the minimum possible health of the last player ; Find the GCD of the array elements ; Driver code
class GFG { static int minHealth ( int health [ ] , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { gcd = __gcd ( gcd , health [ i ] ) ; } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int health [ ] = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n = health . length ; System . out . println ( minHealth ( health , n ) ) ; } }
Construct an array from its pair | Java implementation of the approach ; Utility function to print the array ; Function to generate the original array from the pair - product array ; First element of the resulting array ; Find all the other elements ; Print the elements of the generated array ; Driver code
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void constructArr ( int pair [ ] , int n ) { int size = ( 1 + ( int ) Math . sqrt ( 1 + 8 * n ) ) / 2 ; int [ ] arr = new int [ size ] ; arr [ 0 ] = ( int ) Math . sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } public static void main ( String [ ] args ) { int pair [ ] = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = pair . length ; constructArr ( pair , n ) ; } }
Count of odd and even sum pairs in an array | Java implementation of the approach ; Function to find the count of pairs with odd sum and the count of pairs with even sum ; To store the count of even and odd number from the array ; If the current element is even ; If it is odd ; To store the count of pairs with even sum ; All the even elements will make pairs with each other and the sum of the pair will be even ; All the odd elements will make pairs with each other and the sum of the pair will be even ; To store the count of pairs with odd sum ; All the even elements will make pairs with all the odd element and the sum of the pair will be odd ; Driver code
class GFG { static void findPairs ( int arr [ ] , int n ) { int cntEven = 0 , cntOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) cntEven ++ ; else cntOdd ++ ; } int evenPairs = 0 ; evenPairs += ( ( cntEven * ( cntEven - 1 ) ) / 2 ) ; evenPairs += ( ( cntOdd * ( cntOdd - 1 ) ) / 2 ) ; int oddPairs = 0 ; oddPairs += ( cntEven * cntOdd ) ; System . out . println ( " Odd ▁ pairs ▁ = ▁ " + oddPairs ) ; System . out . println ( " Even ▁ pairs ▁ = ▁ " + evenPairs ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; findPairs ( arr , n ) ; } }
Number of ways to distribute N Paper Set among M students | Java implementation of the approach ; Function to return n ! % 1000000007 ; To store the factorial ; Find the factorial ; Function to return the count of possible ways ; Driver code
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int factMod ( int n ) { long fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact *= ( i % MOD ) ; fact %= MOD ; } return ( int ) fact ; } static int countWays ( int n , int m ) { return factMod ( m ) ; } public static void main ( String args [ ] ) { int n = 2 , m = 2 ; System . out . print ( countWays ( n , m ) ) ; } }
Program for nth Fuss – Catalan Number | Java program for nth Fuss - Catalan Number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth Fuss - Catalan number in O ( n ) time ; Calculate value of 3 nCn ; return 3 nCn / ( 2 n + 1 ) ; Driver code
class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int Fuss_catalan ( int n ) { int c = binomialCoeff ( 3 * n , n ) ; return c / ( 2 * n + 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) System . out . print ( Fuss_catalan ( i ) + " ▁ " ) ; } }