codeparrot/xlcost-text-to-code huggingface dataset:
Area of a circle inscribed in a rectangle which is inscribed in a semicircle | Java Program to find the area of the circle inscribed within the rectangle which in turn is inscribed in a semicircle ; Function to find the area of the circle ; radius cannot be negative ; area of the circle ; Driver code
import java . io . * ; class GFG { static float area ( float r ) { if ( r < 0 ) return - 1 ; float area = ( float ) ( 3.14 * Math . pow ( r / ( 2 * Math . sqrt ( 2 ) ) , 2 ) ) ; return area ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( area ( a ) ) ; } }
Find count of Almost Prime numbers from 1 to N | Java program to count almost prime numbers from 1 to n ; 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 ; Function to count almost prime numbers from 1 to n ; to store required answer ; 6 is first almost prime number ; to count prime factors ; if it is perfect square ; if I is almost prime number ; Driver code
import java . io . * ; class GFG { static int N = 100005 ; static boolean prime [ ] = new boolean [ N ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < N ; i += p ) prime [ i ] = false ; } } } static int almostPrimes ( int n ) { int ans = 0 ; for ( int i = 6 ; i <= n ; i ++ ) { int c = 0 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) { if ( prime [ j ] ) c ++ ; } else { if ( prime [ j ] ) c ++ ; if ( prime [ i / j ] ) c ++ ; } } } if ( c == 2 ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 21 ; System . out . println ( almostPrimes ( n ) ) ; } }
Divide a number into two parts such that sum of digits is maximum | Java implementation of above approach Returns sum of digits of x ; Returns sum of digits of x ; Returns closest number to x in terms of 9 's. ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int sumOfDigitsSingle ( int x ) { int ans = 0 ; while ( x != 0 ) { ans += x % 10 ; x /= 10 ; } return ans ; } static int closest ( int x ) { int ans = 0 ; while ( ans * 10 + 9 <= x ) ans = ans * 10 + 9 ; return ans ; } static int sumOfDigitsTwoParts ( int N ) { int A = closest ( N ) ; return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) ; } public static void main ( String args [ ] ) { int N = 35 ; System . out . print ( sumOfDigitsTwoParts ( N ) ) ; } }
Primality Test | Set 5 ( Using Lucas | Java program to check for primality using Lucas - Lehmer series . ; Function to check whether ( 2 ^ p - 1 ) is prime or not . ; generate the number ; First number of the series ; Generate the rest ( p - 2 ) terms of the series . ; now if the ( p - 1 ) th term is 0 return true else false . ; Driver Program ; Check whether 2 ^ p - 1 is prime or not .
class GFG { static boolean isPrime ( int p ) { double checkNumber = Math . pow ( 2 , p ) - 1 ; double nextval = 4 % checkNumber ; for ( int i = 1 ; i < p - 1 ; i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ; return ( nextval == 0 ) ; } public static void main ( String [ ] args ) { int p = 7 ; double checkNumber = Math . pow ( 2 , p ) - 1 ; if ( isPrime ( p ) ) System . out . println ( ( int ) checkNumber + " ▁ is ▁ Prime . " ) ; else System . out . println ( ( int ) checkNumber + " ▁ is ▁ not ▁ Prime . " ) ; } }
Sophie Germain Prime | Java program to print all sophie german  prime number till n . ; function to detect prime number here we have used sieve method https : www . geeksforgeeks . org / sieve - of - eratosthenes / to detect prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; We have made array till 2 * n + 1 so that we can check prime number till that and conclude about sophie german prime . ; checking every i whether it is sophie german prime or not . ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void sieve ( int n , boolean prime [ ] ) { for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < n ; i += p ) prime [ i ] = false ; } } } static void printSophieGermanNumber ( int n ) { boolean prime [ ] = new boolean [ 2 * n + 1 ] ; Arrays . fill ( prime , true ) ; sieve ( 2 * n + 1 , prime ) ; for ( int i = 2 ; i < n ; ++ i ) { if ( prime [ i ] && prime [ 2 * i + 1 ] ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 25 ; printSophieGermanNumber ( n ) ; } }
Bessel 's Interpolation | Java Program to interpolate using Bessel 's interpolation ; calculating u mentioned in the formula ; calculating factorial of given number n ; Driver code ; Number of values given ; y [ ] [ ] is used for difference table with y [ ] [ 0 ] used for input ; Calculating the central difference table ; Displaying the central difference table ; value to interpolate at ; Initializing u and sum ; k is origin thats is f ( 0 ) ; if ( ( n % 2 ) > 0 ) origin for odd ; k = n / 2 - 1 ; origin for even ; Solving using bessel 's formula
import java . text . * ; class GFG { static double ucal ( double u , int n ) { if ( n == 0 ) return 1 ; double temp = u ; for ( int i = 1 ; i <= n / 2 ; i ++ ) temp = temp * ( u - i ) ; for ( int i = 1 ; i < n / 2 ; i ++ ) temp = temp * ( u + i ) ; return temp ; } static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } public static void main ( String [ ] args ) { int n = 6 ; double x [ ] = { 25 , 26 , 27 , 28 , 29 , 30 } ; double [ ] [ ] y = new double [ n ] [ n ] ; y [ 0 ] [ 0 ] = 4.000 ; y [ 1 ] [ 0 ] = 3.846 ; y [ 2 ] [ 0 ] = 3.704 ; y [ 3 ] [ 0 ] = 3.571 ; y [ 4 ] [ 0 ] = 3.448 ; y [ 5 ] [ 0 ] = 3.333 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < n - i ; j ++ ) y [ j ] [ i ] = y [ j + 1 ] [ i - 1 ] - y [ j ] [ i - 1 ] ; DecimalFormat df = new DecimalFormat ( " # . # # # # # # # # " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) System . out . print ( y [ i ] [ j ] + " TABSYMBOL " ) ; System . out . println ( " " ) ; } double value = 27.4 ; double sum = ( y [ 2 ] [ 0 ] + y [ 3 ] [ 0 ] ) / 2 ; int k ; k = n / 2 ; else double u = ( value - x [ k ] ) / ( x [ 1 ] - x [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( i % 2 ) > 0 ) sum = sum + ( ( u - 0.5 ) * ucal ( u , i - 1 ) * y [ k ] [ i ] ) / fact ( i ) ; else sum = sum + ( ucal ( u , i ) * ( y [ k ] [ i ] + y [ -- k ] [ i ] ) / ( fact ( i ) * 2 ) ) ; } System . out . printf ( " Value ▁ at ▁ " + value + " ▁ is ▁ % .5f " , sum ) ; } }
An efficient way to check whether n | A simple Java program to check if n - th Fibonacci number is multiple of 10. ; Returns true if n - th Fibonacci number is multiple of 10. ; main function
class Fibonacci { static int fibonacci ( int n ) { int a = 0 ; int b = 1 ; int c = 0 ; if ( n <= 1 ) return n ; for ( int i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } static boolean isMultipleOf10 ( int n ) { int f = fibonacci ( 30 ) ; return ( f % 10 == 0 ) ; } public static void main ( String [ ] args ) { int n = 30 ; if ( isMultipleOf10 ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Program to find whether a given number is power of 2 | Java program for the above approach ; Function which checks whether a number is a power of 2 ; base cases '1' is the only odd number which is a power of 2 ( 2 ^ 0 ) ; all other odd numbers are not powers of 2 ; recursive function call ; Driver Code ; True ; False
import java . util . * ; class GFG { static boolean powerOf2 ( int n ) { if ( n == 1 ) return true ; else if ( n % 2 != 0 n == 0 ) return false ; return powerOf2 ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 64 ; int m = 12 ; if ( powerOf2 ( n ) == true ) System . out . print ( " True " + "NEW_LINE"); else System . out . print ( " False " + "NEW_LINE"); if ( powerOf2 ( m ) == true ) System . out . print ( " True " + "NEW_LINE"); else System . out . print ( " False " + "NEW_LINE"); } }
Program to find whether a given number is power of 2 | Java program to efficiently check for power for 2 ; Method to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Driver method
class Test { static boolean isPowerOfTwo ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } public static void main ( String [ ] args ) { System . out . println ( isPowerOfTwo ( 31 ) ? " Yes " : " No " ) ; System . out . println ( isPowerOfTwo ( 64 ) ? " Yes " : " No " ) ; } }
Program to find whether a given number is power of 2 | Java program of the above approach ; Function to check if x is power of 2 ; Driver code
import java . io . * ; class GFG { static boolean isPowerofTwo ( int n ) { if ( n == 0 ) return false ; if ( ( n & ( ~ ( n - 1 ) ) ) == n ) return true ; return false ; } public static void main ( String [ ] args ) { if ( isPowerofTwo ( 30 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isPowerofTwo ( 128 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Smallest power of 2 which is greater than or equal to sum of array elements | Java implementation of the above approach ; Function to find the nearest power of 2 ; The number ; If already a power of 2 ; Find the next power of 2 ; Function to find the memory used ; Sum of array ; Traverse and find the sum of array ; Function call to find the nearest power of 2 ; Driver Code
class GFG { static int nextPowerOf2 ( int n ) { int p = 1 ; if ( n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } static int memoryUsed ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int nearest = nextPowerOf2 ( sum ) ; return nearest ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 } ; int n = arr . length ; System . out . println ( memoryUsed ( arr , n ) ) ; } }
Toggling k | Java program to toogle k - th bit of a number ; main function
class Toggle { static int toggleKthBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 1 ; System . out . println ( toggleKthBit ( n , k ) ) ; } }
Smallest power of 2 greater than or equal to n | Java program to find smallest power of 2 greater than or equal to n ; First n in the below condition is for the case where n is 0 ; Driver Code
import java . io . * ; class GFG { static int nextPowerOf2 ( int n ) { int count = 0 ; if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } public static void main ( String args [ ] ) { int n = 0 ; System . out . println ( nextPowerOf2 ( n ) ) ; } }
Add elements in start to sort the array | Variation of Stalin Sort | Java implementation to sort the array by using the variation of the Stalin sort ; Function to sort the array ; Iterator < Integer > index = arr . iterator ( ) ; ; Driver Code ; Function call
import java . util . * ; class GFG { static void variationStalinsort ( Vector < Integer > arr ) { int j = 0 ; while ( true ) { int moved = 0 ; for ( int i = 0 ; i < ( arr . size ( ) - 1 - j ) ; i ++ ) { if ( arr . get ( i ) > arr . get ( i + 1 ) ) { int index ; int temp ; index = arr . get ( i ) ; temp = arr . get ( i + 1 ) ; arr . removeElement ( index ) ; arr . add ( i , temp ) ; arr . removeElement ( temp ) ; arr . add ( i + 1 , index ) ; moved ++ ; } } j ++ ; if ( moved == 0 ) { break ; } } System . out . print ( arr ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 3 , 6 , 5 , 8 , 7 , 10 , 9 } ; Vector < Integer > arr1 = new Vector < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) arr1 . add ( arr [ i ] ) ; variationStalinsort ( arr1 ) ; } }
Sort an Array which contain 1 to N values in O ( N ) using Cycle Sort | Java program for the above approach ; Function to print array element ; Traverse the array ; Function to sort the array in O ( N ) ; Traverse the array ; If the current element is at correct position ; Else swap the current element with it 's correct position ; Swap the value of arr [ i ] and arr [ arr [ i ] - 1 ] ; Driver Code ; Function call to sort the array ; Function call to print the array
class Main { public static void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void sortArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; ) { if ( arr [ i ] == i + 1 ) { i ++ ; } else { int temp1 = arr [ i ] ; int temp2 = arr [ arr [ i ] - 1 ] ; arr [ i ] = temp2 ; arr [ temp1 - 1 ] = temp1 ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 5 , 3 , 4 } ; int N = arr . length ; sortArray ( arr , N ) ; printArray ( arr , N ) ; } }
Maximum sum of values of N items in 0 | Java implementation for the above approach ; Function to find the maximum value ; base condition ; K elements already reduced to half of their weight ; Dont include item ; If weight of the item is less than or equal to the remaining weight then include the item ; Return the maximum of both cases ; If the weight reduction to half is possible ; Skip the item ; Include item with full weight if weight of the item is less than the remaining weight ; Include item with half weight if half weight of the item is less than the remaining weight ; Return the maximum of all 3 cases ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int maximum ( int value [ ] , int weight [ ] , int weight1 , int flag , int K , int index ) { if ( index >= value . length ) { return 0 ; } if ( flag == K ) { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; int full = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } return Math . max ( full , skip ) ; } else { int skip = maximum ( value , weight , weight1 , flag , K , index + 1 ) ; int full = 0 ; int half = 0 ; if ( weight [ index ] <= weight1 ) { full = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] , flag , K , index + 1 ) ; } if ( weight [ index ] / 2 <= weight1 ) { half = value [ index ] + maximum ( value , weight , weight1 - weight [ index ] / 2 , flag , K , index + 1 ) ; } return Math . max ( full , Math . max ( skip , half ) ) ; } } public static void main ( String [ ] args ) throws Exception { int value [ ] = { 17 , 20 , 10 , 15 } ; int weight [ ] = { 4 , 2 , 7 , 5 } ; int K = 1 ; int W = 4 ; System . out . println ( maximum ( value , weight , W , 0 , K , 0 ) ) ; } }
d | Java program to find the size of the minimum dominating set of the tree ; Definition of a tree node ; Helper function that allocates a new node ; DP array to precompute and store the results ; minDominatingSettion to return the size of the minimum dominating set of the array ; Base case ; Setting the compulsory value if needed ; Check if the answer is already computed ; If it is compulsory to select the node ; Choose the node and set its children as covered ; If it is covered ; If the current node is neither covered nor needs to be selected compulsorily ; Store the result ; Driver code ; Initialising the DP array ; Constructing the tree
import java . util . * ; class GFG { static final int N = 1005 ; static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return node ; } static int [ ] [ ] [ ] dp = new int [ N ] [ 5 ] [ 5 ] ; static int minDominatingSet ( Node root , int covered , int compulsory ) { if ( root == null ) return 0 ; if ( root . left != null && root . right != null && covered > 0 ) compulsory = 1 ; if ( dp [ root . data ] [ covered ] [ compulsory ] != - 1 ) return dp [ root . data ] [ covered ] [ compulsory ] ; if ( compulsory > 0 ) { return dp [ root . data ] [ covered ] [ compulsory ] = 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) ; } if ( covered > 0 ) { return dp [ root . data ] [ covered ] [ compulsory ] = Math . min ( 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) , minDominatingSet ( root . left , 0 , 0 ) + minDominatingSet ( root . right , 0 , 0 ) ) ; } int ans = 1 + minDominatingSet ( root . left , 1 , 0 ) + minDominatingSet ( root . right , 1 , 0 ) ; if ( root . left != null ) { ans = Math . min ( ans , minDominatingSet ( root . left , 0 , 1 ) + minDominatingSet ( root . right , 0 , 0 ) ) ; } if ( root . right != null ) { ans = Math . min ( ans , minDominatingSet ( root . left , 0 , 0 ) + minDominatingSet ( root . right , 0 , 1 ) ) ; } return dp [ root . data ] [ covered ] [ compulsory ] = ans ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 5 ; j ++ ) { for ( int l = 0 ; l < 5 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . left . left . left = newNode ( 5 ) ; root . left . left . left . left = newNode ( 6 ) ; root . left . left . left . right = newNode ( 7 ) ; root . left . left . left . right . right = newNode ( 10 ) ; root . left . left . left . left . left = newNode ( 8 ) ; root . left . left . left . left . right = newNode ( 9 ) ; System . out . print ( minDominatingSet ( root , 0 , 0 ) + "NEW_LINE"); } }
Number of subsets with zero sum | Java implementation of above approach ; variable to store states of dp ; To find the number of subsets with sum equal to 0 Since S can be negative , we will maxSum to it to make it positive ; Base cases ; Returns the value if a state is already solved ; If the state is not visited , then continue ; Recurrence relation ; Returning the value ; Driver function
class GFG { static int maxSum = 100 ; static int arrSize = 51 ; static int [ ] [ ] dp = new int [ arrSize ] [ maxSum ] ; static boolean [ ] [ ] visit = new boolean [ arrSize ] [ maxSum ] ; static int SubsetCnt ( int i , int s , int arr [ ] , int n ) { if ( i == n ) { if ( s == 0 ) { return 1 ; } else { return 0 ; } } if ( visit [ i ] [ s + arrSize ] ) { return dp [ i ] [ s + arrSize ] ; } visit [ i ] [ s + arrSize ] = true ; dp [ i ] [ s + arrSize ] = SubsetCnt ( i + 1 , s + arr [ i ] , arr , n ) + SubsetCnt ( i + 1 , s , arr , n ) ; return dp [ i ] [ s + arrSize ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , - 4 , - 4 } ; int n = arr . length ; System . out . println ( SubsetCnt ( 0 , 0 , arr , n ) ) ; } }
Number of Binary Strings of length N with K adjacent Set Bits | Java program to find the number of Bit Strings of length N with K adjacent set bits ; Function to find the number of Bit Strings of length N with K adjacent set bits ; Base Case when we form bit string of length n ; if f ( bit string ) = k , count this way ; Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset ; set the bit at currentIndex ; unset the bit at currentIndex ; Driver Code ; dp [ i ] [ j ] [ k ] represents bit strings of length i with f ( bit string ) = j and last bit as k ; initialize the dp ; total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )
class solution { static final int MAX = 1000 ; static int waysToKAdjacentSetBits ( int dp [ ] [ ] [ ] , int n , int k , int currentIndex , int adjacentSetBits , int lastBit ) { if ( currentIndex == n ) { if ( adjacentSetBits == k ) return 1 ; return 0 ; } if ( dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] != - 1 ) { return dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] ; } int noOfWays = 0 ; if ( lastBit == 1 ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits + 1 , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } else if ( lastBit == 0 ) { noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 1 ) ; noOfWays += waysToKAdjacentSetBits ( dp , n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } dp [ currentIndex ] [ adjacentSetBits ] [ lastBit ] = noOfWays ; return noOfWays ; } public static void main ( String args [ ] ) { int n = 5 , k = 2 ; int dp [ ] [ ] [ ] = new int [ MAX ] [ MAX ] [ 2 ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k1 = 0 ; k1 < 2 ; k1 ++ ) dp [ i ] [ j ] [ k1 ] = - 1 ; int totalWays = waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( dp , n , k , 1 , 0 , 0 ) ; System . out . print ( " Number ▁ of ▁ ways ▁ = ▁ " + totalWays + "NEW_LINE"); } }
Tetranacci Numbers | A space optimized based Java program to print the nth tetranacci number ; Function to print the N - th tetranacci number ; Initialize first four numbers to base cases ; declare a current variable ; Loop to add previous four numbers for each number starting from 4 and then assign first , second , third to second , third , fourth and curr to fourth respectively ; Driver code
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void printTetra ( int n ) { if ( n < 0 ) return ; int first = 0 , second = 1 ; int third = 1 , fourth = 2 ; int curr = 0 ; if ( n == 0 ) System . out . print ( first ) ; else if ( n == 1 n == 2 ) System . out . print ( second ) ; else if ( n == 3 ) System . out . print ( fourth ) ; else { for ( int i = 4 ; i <= n ; i ++ ) { curr = first + second + third + fourth ; first = second ; second = third ; third = fourth ; fourth = curr ; } System . out . print ( curr ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printTetra ( n ) ; } }
Count ways to reach the nth stair using step 1 , 2 or 3 | Program to find n - th stair using step size 1 or 2 or 3. ; A recursive function used by countWays ; Driver function
import java . lang . * ; import java . util . * ; public class GfG { public static int countWays ( int n ) { int [ ] res = new int [ n + 1 ] ; res [ 0 ] = 1 ; res [ 1 ] = 1 ; res [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) res [ i ] = res [ i - 1 ] + res [ i - 2 ] + res [ i - 3 ] ; return res [ n ] ; } public static void main ( String argc [ ] ) { int n = 4 ; System . out . println ( countWays ( n ) ) ; } }
Count ways to reach the nth stair using step 1 , 2 or 3 | A Java program to count number of ways to reach nth stair when ; A recursive function used by countWays ; Declaring three variables and holding the ways for first three stairs ; Fourth variable ; Starting from 4 as already counted for 3 stairs ; Driver code
import java . io . * ; class GFG { static int countWays ( int n ) { int a = 1 , b = 2 , c = 4 ; int d = 0 ; if ( n == 0 n == 1 n == 2 ) return n ; if ( n == 3 ) return c ; for ( int i = 4 ; i <= n ; i ++ ) { d = c + b + a ; a = b ; b = c ; c = d ; } return d ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countWays ( n ) ) ; } }
Subset Sum Problem in O ( sum ) space |  ; initializing with 1 as sum 0 is always possible ; loop to go through every element of the elements array ; to change the values of all possible sum values to 1 ; if sum is possible then return 1 ; Driver code
import java . io . * ; import java . util . * ; class GFG { static boolean isPossible ( int elements [ ] , int sum ) { int dp [ ] = new int [ sum + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < elements . length ; i ++ ) { for ( int j = sum ; j >= elements [ i ] ; j -- ) { if ( dp [ j - elements [ i ] ] == 1 ) dp [ j ] = 1 ; } } if ( dp [ sum ] == 1 ) return true ; return false ; } public static void main ( String [ ] args ) throws Exception { int elements [ ] = { 6 , 2 , 5 } ; int sum = 7 ; if ( isPossible ( elements , sum ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }