codeparrot/xlcost-text-to-code huggingface dataset:
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | Java implementation of the approach ; Function to find the minimum required sum using dynamic programming ; dp [ i ] [ j ] = minimum val of accommodation till j 'th index of the string  using i+1 number of buckets.  Final ans will be in dp[n-1][K-1] ; Corner cases ; Filling first row , if only 1 bucket then simple count number of zeros and ones and do the multiplication ; If k = 0 then this arrangement is not possible ; If no arrangement is possible then our answer will remain INT_MAX so return - 1 ; Driver code ; K buckets
class GFG { static long solve ( String str , int K ) { int n = str . length ( ) ; long dp [ ] [ ] = new long [ K ] [ n ] ; if ( n < K ) return - 1 ; else if ( n == K ) return 0 ; long zeroes = 0 , ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) zeroes ++ ; else ones ++ ; dp [ 0 ] [ i ] = ones * zeroes ; } for ( int s = 1 ; s < K ; s ++ ) { for ( int i = 0 ; i < n ; i ++ ) { dp [ s ] [ i ] = Integer . MAX_VALUE ; ones = 0 ; zeroes = 0 ; for ( int k = i ; k >= 0 ; k -- ) { if ( str . charAt ( k ) == '0' ) zeroes ++ ; else ones ++ ; dp [ s ] [ i ] = Math . min ( dp [ s ] [ i ] , + ( ( k - 1 >= 0 ) ? ones * zeroes + dp [ s - 1 ] [ k - 1 ] : Integer . MAX_VALUE ) ) ; } } } return ( dp [ K - 1 ] [ n - 1 ] == Integer . MAX_VALUE ) ? - 1 : dp [ K - 1 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { String S = "0101" ; int K = 2 ; System . out . println ( solve ( S , K ) ) ; } }
Queries to check if string B exists as substring in string A |  ; Dp Array ; String to character array ; initialize matrix with 1 ; for r from 1 to string length ; for c from 1 b string length ; For every query ; print true ; print false ; Driver Code
import java . io . * ; import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { private static void substringCheck ( String stra , String strb , int [ ] query ) { int [ ] [ ] matrix = new int [ strb . length ( ) ] [ stra . length ( ) ] ; char [ ] charCrr = stra . toCharArray ( ) ; char [ ] charRrr = strb . toCharArray ( ) ; for ( int c = 0 ; c < stra . length ( ) ; c ++ ) { if ( charRrr [ 0 ] == charCrr ) { matrix [ 0 ] = 1 ; } } for ( int r = 1 ; r < charRrr . length ; r ++ ) { char ch = charRrr [ r ] ; for ( int c = 1 ; c < charCrr . length ; c ++ ) { if ( ch == charCrr && matrix [ r - 1 ] == 1 ) { matrix [ r ] = 1 ; } } } for ( int q : query ) { int matLoc = ( q + ( strb . length ( ) - 1 ) ) ; if ( matLoc >= stra . length ( ) ) { System . out . println ( false ) ; } else { if ( matrix [ strb . length ( ) - 1 ] [ matLoc ] == 1 ) { System . out . println ( true ) ; } else { System . out . println ( false ) ; } } } } public static void main ( String [ ] args ) { String stra = " GeeksForGeeks " ; String strb = " Geeks " ; int [ ] query = { 0 , 5 , 8 } ; substringCheck ( stra , strb , query ) ; }
Queries for bitwise AND in the index range [ L , R ] of the given array | Java implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
import java . io . * ; class GFG { static int MAX = 100000 ; static int bitscount = 32 ; static int [ ] [ ] prefix_count = new int [ bitscount ] [ MAX ] ; static void findPrefixCount ( int arr [ ] , int n ) { for ( int i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; } } } static int rangeAnd ( int l , int r ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int x ; if ( l == 0 ) x = prefix_count [ i ] [ r ] ; else x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; if ( x == r - l + 1 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 5 , 3 , 5 , 2 , 3 } ; int n = arr . length ; findPrefixCount ( arr , n ) ; int queries [ ] [ ] = { { 1 , 3 } , { 4 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( rangeAnd ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } }
Longest path in a directed Acyclic graph | Dynamic Programming | Java program to find the longest path in the DAG ; graph class ; Function to traverse the DAG and apply Dynamic Programming to find the longest path ; Mark as visited ; Traverse for all its children ; If not visited ; Store the max of the paths ; Function to add an edge ; Function that returns the longest path ; Dp array ; Visited array to know if the node has been visited previously or not ; Call DFS for every unvisited vertex ; Traverse and find the maximum of all dp [ i ] ; Driver code ; Example - 1
import java . util . ArrayList ; class Graph { int vertices ; ArrayList < Integer > edge [ ] ; Graph ( int vertices ) { this . vertices = vertices ; edge = new ArrayList [ vertices + 1 ] ; for ( int i = 0 ; i <= vertices ; i ++ ) { edge [ i ] = new ArrayList < > ( ) ; } } void dfs ( int node , ArrayList < Integer > adj [ ] , int dp [ ] , boolean visited [ ] ) { visited [ node ] = true ; for ( int i = 0 ; i < adj [ node ] . size ( ) ; i ++ ) { if ( ! visited [ adj [ node ] . get ( i ) ] ) dfs ( adj [ node ] . get ( i ) , adj , dp , visited ) ; dp [ node ] = Math . max ( dp [ node ] , 1 + dp [ adj [ node ] . get ( i ) ] ) ; } } void addEdge ( int a , int b ) { edge [ a ] . add ( b ) ; } int findLongestPath ( int n ) { ArrayList < Integer > adj [ ] = edge ; int [ ] dp = new int [ n + 1 ] ; boolean [ ] visited = new boolean [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! visited [ i ] ) dfs ( i , adj , dp , visited ) ; } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = Math . max ( ans , dp [ i ] ) ; } return ans ; } } public class Main { public static void main ( String [ ] args ) { int n = 5 ; Graph graph = new Graph ( n ) ; graph . addEdge ( 1 , 2 ) ; graph . addEdge ( 1 , 3 ) ; graph . addEdge ( 3 , 2 ) ; graph . addEdge ( 2 , 4 ) ; graph . addEdge ( 3 , 4 ) ; graph . findLongestPath ( n ) ; System . out . println ( graph . findLongestPath ( n ) ) ; } }
Maximum length subsequence such that adjacent elements in the subsequence have a common factor | Java implementation of the above approach ; Function to compute least prime divisor of i upto MAX element of the input array it will be space efficient if more test cases are there it 's  better to find prime divisor  upto upperbound of input element  it will be cost efficient ; Function that returns the maximum length subsequence such that adjacent elements have a common factor . ; Initialize dp array with 1. ; p has appeared at least once . ; Update latest occurrence of prime p . ; Take maximum value as the answer . ; Driver code
import java . util . * ; class GfG { static int N , MAX ; static int lpd [ ] ; static void preCompute ( ) { lpd = new int [ MAX + 1 ] ; lpd [ 0 ] = lpd [ 1 ] = 1 ; for ( int i = 2 ; i * i <= MAX ; i ++ ) { for ( int j = i * 2 ; j <= MAX ; j += i ) { if ( lpd [ j ] == 0 ) { lpd [ j ] = i ; } } } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( lpd [ i ] == 0 ) { lpd [ i ] = i ; } } } static int maxLengthSubsequence ( Integer arr [ ] , int n ) { Integer dp [ ] = new Integer [ N ] ; Map < Integer , Integer > pos = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { while ( arr [ i ] > 1 ) { int p = lpd [ arr [ i ] ] ; if ( pos . containsKey ( p ) ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ pos . get ( p ) ] ) ; } pos . put ( p , i ) ; while ( arr [ i ] % p == 0 ) arr [ i ] /= p ; } } int ans = Collections . max ( Arrays . asList ( dp ) ) ; return ans ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 12 , 2 , 8 , 6 , 3 , 1 , 9 } ; N = arr . length ; MAX = Collections . max ( Arrays . asList ( arr ) ) ; preCompute ( ) ; System . out . println ( maxLengthSubsequence ( arr , N - 1 ) ) ; } }
Number of ways to partition a string into two balanced subsequences | Java implementation of the above approach ; For maximum length of input string ; Declaring the DP table ; Function to calculate the number of valid assignments ; Return 1 if both subsequences are balanced ; Increment the count if it an opening bracket ; Decrement the count if it a closing bracket ; Driver Code ; Initializing the DP table ; Initial value for c_x and c_y is zero
class GFG { static int MAX = 10 ; static int [ ] [ ] [ ] F = new int [ MAX ] [ MAX ] [ MAX ] ; static int noOfAssignments ( String s , int n , int i , int c_x , int c_y ) { if ( F [ i ] [ c_x ] [ c_y ] != - 1 ) return F [ i ] [ c_x ] [ c_y ] ; if ( i == n ) { F [ i ] [ c_x ] [ c_y ] = ( c_x == 0 && c_y == 0 ) ? 1 : 0 ; return F [ i ] [ c_x ] [ c_y ] ; } if ( s . charAt ( i ) == ' ( ' ) { F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( s , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( s , n , i + 1 , c_x , c_y + 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } F [ i ] [ c_x ] [ c_y ] = 0 ; if ( c_x != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x , c_y - 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } public static void main ( String [ ] args ) { String s = " ( ( ) ) " ; int n = s . length ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) F [ i ] [ j ] [ k ] = - 1 ; System . out . println ( noOfAssignments ( s , n , 0 , 0 , 0 ) ) ; } }
Gould 's Sequence | JAVA program to generate Gould 's Sequence ; Utility function to count odd numbers in ith row of Pascals 's triangle ; Initialize count as zero ; Return 2 ^ count ; Function to generate gould 's Sequence ; loop to generate gould 's Sequence up to n ; Driver code ; Get n ; Function call
class GFG { static int countOddNumber ( int row_num ) { int count = 0 ; while ( row_num > 0 ) { count += row_num & 1 ; row_num >>= 1 ; } return ( 1 << count ) ; } static void gouldSequence ( int n ) { for ( int row_num = 0 ; row_num < n ; row_num ++ ) { System . out . print ( countOddNumber ( row_num ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 16 ; gouldSequence ( n ) ; } }
Count numbers ( smaller than or equal to N ) with given digit sum | Java program to count 2 s from 0 to n ; N can be max 10 ^ 18 and hence digitsum will be 162 maximum . ; If sum_so_far equals to given sum then return 1 else 0 ; Our constructed number should not become greater than N . ; If tight is true then it will also be true for ( i + 1 ) digit . ; Driver code
class GFG { static long dp [ ] [ ] [ ] = new long [ 18 ] [ 2 ] [ 162 ] ; static long solve ( int i , boolean tight , int sum_so_far , int Sum , String number , int len ) { if ( i == len ) { if ( sum_so_far == Sum ) return 1 ; else return 0 ; } long ans = dp [ i ] [ 1 ] [ sum_so_far ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; boolean ntight ; int nsum_so_far ; for ( char currdigit = '0' ; currdigit <= '9' ; currdigit ++ ) { if ( ! tight && currdigit > number . charAt ( i ) ) { break ; } ntight = tight || currdigit < number . charAt ( i ) ; nsum_so_far = sum_so_far + ( currdigit - '0' ) ; ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , len ) ; } return ans ; } public static void main ( String [ ] args ) { long count = 0 ; int sum = 4 ; String number = "100" ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int k = 0 ; k < 162 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } System . out . println ( solve ( 0 , false , 0 , sum , number , number . length ( ) ) ) ; } }
Longest dividing subsequence | Dynamic Programming Java implementation of lds problem ; lds ( ) returns the length of the longest dividing subsequence in arr [ ] of size n ; Compute optimized lds values in bottom up manner ; Return maximum value in lds [ ] ; Driver program to test above function
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int lds ( Integer arr [ ] , int n ) { Integer lds [ ] = new Integer [ n ] ; lds [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = Math . max ( lds [ i ] , lds [ j ] + 1 ) ; } int max = ( int ) Collections . max ( Arrays . asList ( lds ) ) ; return max ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lds ▁ is ▁ " + lds ( arr , n ) ) ; } }
Connect nodes at same level |  ; initialize queue to hold nodes at same level ; adding nodes to tehe queue ; initializing prev to null ; i > 0 because when i is 0 prev points the last node of previous level , so we skip it ; pointing last node of the nth level to null ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / 3 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
import java . util . * ; import java . io . * ; class Node { int data ; Node left , right , nextRight ; Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { Node root ; void connect ( Node p ) { Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node temp = null ; while ( ! q . isEmpty ( ) ) { int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Node prev = temp ; temp = q . poll ( ) ; if ( i > 0 ) prev . nextRight = temp ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } temp . nextRight = null ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . connect ( tree . root ) ; System . out . println ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; } }
Memoization ( 1D , 2D and 3D ) | A recursive implementation of LCS problem of three strings ; Utility function to get max of 2 integers ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; base case ; if equal , then check for next combination ; recursive call ; return the maximum of the three other possible states in recursion ; Driver code
class GFG { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int lcs ( char [ ] X , char [ ] Y , char [ ] Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] && Y [ n - 1 ] == Z [ o - 1 ] ) { return 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; } else { return Math . max ( lcs ( X , Y , Z , m , n - 1 , o ) , Math . max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; } } public static void main ( String [ ] args ) { char [ ] X = " geeks " . toCharArray ( ) ; char [ ] Y = " geeksfor " . toCharArray ( ) ; char [ ] Z = " geeksforge " . toCharArray ( ) ; int m = X . length ; int n = Y . length ; int o = Z . length ; System . out . println ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , Z , m , n , o ) ) ; } }
Memoization ( 1D , 2D and 3D ) | A memoize recursive implementation of LCS problem ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] memoization applied in recursive solution ; base case ; if the same state has already been computed ; if equal , then we store the value of the function call ; store it in arr to avoid further repetitive work in future function calls ; store it in arr to avoid further repetitive work in future function calls ; Utility function to get max of 2 integers ; Driver Code
import java . io . * ; class GFG { public static int [ ] [ ] [ ] arr = new int [ 100 ] [ 100 ] [ 100 ] ; static int lcs ( String X , String Y , String Z , int m , int n , int o ) { if ( m == 0 n == 0 o == 0 ) return 0 ; if ( arr [ m - 1 ] [ n - 1 ] [ o - 1 ] != - 1 ) return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) && Y . charAt ( n - 1 ) == Z . charAt ( o - 1 ) ) { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = 1 + lcs ( X , Y , Z , m - 1 , n - 1 , o - 1 ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } else { arr [ m - 1 ] [ n - 1 ] [ o - 1 ] = max ( lcs ( X , Y , Z , m , n - 1 , o ) , max ( lcs ( X , Y , Z , m - 1 , n , o ) , lcs ( X , Y , Z , m , n , o - 1 ) ) ) ; return arr [ m - 1 ] [ n - 1 ] [ o - 1 ] ; } } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int k = 0 ; k < 100 ; k ++ ) { arr [ i ] [ j ] [ k ] = - 1 ; } } } String X = " geeks " ; String Y = " geeksfor " ; String Z = " geeksforgeeks " ; int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; System . out . print ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcs ( X , Y , Z , m , n , o ) ) ; } }
Find the probability of reaching all points after N moves from point N | Java program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Calling function to print the array with probabilities ; Function that prints the array ; Driver Code
import java . util . * ; class GFG { static void printProbabilities ( int n , double left ) { double right = 1 - left ; double [ ] [ ] arr = new double [ n + 1 ] [ 2 * n + 1 ] ; arr [ 1 ] [ n + 1 ] = right ; arr [ 1 ] [ n - 1 ] = left ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= 2 * n ; j ++ ) { arr [ i ] [ j ] += ( arr [ i - 1 ] [ j - 1 ] * right ) ; } for ( int j = 2 * n - 1 ; j >= 0 ; j -- ) { arr [ i ] [ j ] += ( arr [ i - 1 ] [ j + 1 ] * left ) ; } } printArray ( arr , n ) ; } static void printArray ( double [ ] [ ] arr , int n ) { for ( int i = 0 ; i < arr [ 0 ] . length ; i ++ ) { System . out . printf ( " % 5.4f ▁ " , arr [ n ] [ i ] ) ; } } public static void main ( String [ ] args ) { int n = 2 ; double left = 0.5 ; printProbabilities ( n , left ) ; } }
Check if it is possible to reach a number by making jumps of two given length | Java implementation of the approach ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int minStepsNeeded ( int k , int d1 , int d2 , int x ) { int gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return - 1 ; Queue < pair > q = new LinkedList < > ( ) ; HashSet < Integer > visited = new HashSet < > ( ) ; q . add ( new pair ( k , 0 ) ) ; visited . add ( k ) ; while ( ! q . isEmpty ( ) ) { int s = q . peek ( ) . first ; int stp = q . peek ( ) . second ; if ( s == x ) return stp ; q . remove ( ) ; if ( ! visited . contains ( s + d1 ) ) { q . add ( new pair ( s + d1 , stp + 1 ) ) ; visited . add ( s + d1 ) ; } if ( visited . contains ( s + d2 ) ) { q . add ( new pair ( s + d2 , stp + 1 ) ) ; visited . add ( s + d2 ) ; } if ( ! visited . contains ( s - d1 ) ) { q . add ( new pair ( s - d1 , stp + 1 ) ) ; visited . add ( s - d1 ) ; } if ( ! visited . contains ( s - d2 ) ) { q . add ( new pair ( s - d2 , stp + 1 ) ) ; visited . add ( s - d2 ) ; } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int k = 10 , d1 = 4 , d2 = 6 , x = 8 ; System . out . println ( minStepsNeeded ( k , d1 , d2 , x ) ) ; } }
Tiling with Dominoes | Java program to find no . of ways to fill a 3 xn board with 2 x1 dominoes . ; Driver code
import java . io . * ; class GFG { static int countWays ( int n ) { int [ ] A = new int [ n + 1 ] ; int [ ] B = new int [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 0 ; B [ 0 ] = 0 ; B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( countWays ( n ) ) ; } }
Newman | Java program to find nth element of Newman - Conway Sequence ; Recursion to find n - th element ; Driver Program
import java . io . * ; class GFG { static int sequence ( int n ) { if ( n == 1 n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( sequence ( n ) ) ; } }