codeparrot/xlcost-text-to-code huggingface dataset:
Connect nodes at same level | A binary tree node ; Sets the nextRight of root and calls connectRecur ( ) for other nodes ; Set the nextRight for root ; Set the next right for rest of the nodes ( other than root ) ; Set next right of all descendents of p . Assumption : p is a compete binary tree ; Base case ; Set the nextRight pointer for p 's left child ; Set the nextRight pointer for p 's right child  p->nextRight will be NULL if p is the right most child  at its level ; Set nextRight for other nodes in pre order fashion ; 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
class Node { int data ; Node left , right , nextRight ; Node ( int item ) { data = item ; left = right = nextRight = null ; } } class BinaryTree { Node root ; void connect ( Node p ) { p . nextRight = null ; connectRecur ( p ) ; } void connectRecur ( Node p ) { if ( p == null ) return ; if ( p . left != null ) p . left . nextRight = p . right ; if ( p . right != null ) p . right . nextRight = ( p . nextRight != null ) ? p . nextRight . left : null ; connectRecur ( p . left ) ; connectRecur ( p . right ) ; } 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 ) ; } }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of people reduce to ( x - 2 ) . ; Driver code
import java . io . * ; class GFG { static int numberOfWays ( int x ) { if ( x == 0 x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } public static void main ( String [ ] args ) { int x = 3 ; System . out . println ( numberOfWays ( x ) ) ; } }
Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Driver code
import java . io . * ; class GFG { static int numberOfWays ( int x ) { int dp [ ] = new int [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; } public static void main ( String [ ] args ) { int x = 3 ; System . out . println ( numberOfWays ( x ) ) ; } }
Longest Repeated Subsequence | Refer https : www . geeksforgeeks . org / longest - repeating - subsequence / for complete code . This function mainly returns LCS ( str , str ) with a condition that same characters at same index are not considered . ; Create and initialize DP table ; Fill dp table ( similar to LCS loops ) ; If characters match and indexes are not same ; If characters do not match
static int findLongestRepeatingSubSeq ( String str ) { int n = str . length ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str . charAt ( i - 1 ) == str . charAt ( j - 1 ) && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; }
Number of ways to arrange N items under given constraints | Java program to find number of ways to arrange items under given constraint ; method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code
class GFG { static int waysToArrange ( int N , int K , int [ ] k ) { int [ ] [ ] C = new int [ N + 1 ] [ N + 1 ] ; int i , j ; for ( i = 0 ; i <= N ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) { C [ i ] [ j ] = 1 ; } else { C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; } } } int [ ] dp = new int [ K + 1 ] ; int count = 0 ; dp [ 0 ] = 1 ; for ( i = 0 ; i < K ; i ++ ) { dp [ i + 1 ] = ( dp [ i ] * C [ count + k [ i ] - 1 ] [ k [ i ] - 1 ] ) ; count += k [ i ] ; } return dp [ K ] ; } public static void main ( String [ ] args ) { int N = 4 ; int [ ] k = new int [ ] { 2 , 2 } ; int K = k . length ; System . out . println ( waysToArrange ( N , K , k ) ) ; } }
Minimum cells required to reach destination with jumps equal to cell values | Java implementation to count minimum cells required to be covered to reach destination ; function to count minimum cells required to be covered to reach destination ; to store min cells required to be covered to reach a particular cell ; initially no cells can be reached ; base case ; building up the dp [ ] [ ] matrix ; dp [ i ] [ j ] != INT_MAX denotes that cell ( i , j ) can be reached from cell ( 0 , 0 ) and the other half of the condition finds the cell on the right that can be reached from ( i , j ) ; the other half of the condition finds the cell right below that can be reached from ( i , j ) ; it true then cell ( m - 1 , n - 1 ) can be reached from cell ( 0 , 0 ) and returns the minimum number of cells covered ; cell ( m - 1 , n - 1 ) cannot be reached from cell ( 0 , 0 ) ; Driver code
class MinCellsDestination { static final int SIZE = 100 ; static int minCells ( int mat [ ] [ ] , int m , int n ) { int dp [ ] [ ] = new int [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i ] [ j ] != Integer . MAX_VALUE && ( j + mat [ i ] [ j ] ) < n && ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) dp [ i ] [ j + mat [ i ] [ j ] ] = dp [ i ] [ j ] + 1 ; if ( dp [ i ] [ j ] != Integer . MAX_VALUE && ( i + mat [ i ] [ j ] ) < m && ( dp [ i ] [ j ] + 1 ) < dp [ i + mat [ i ] [ j ] ] [ j ] ) dp [ i + mat [ i ] [ j ] ] [ j ] = dp [ i ] [ j ] + 1 ; } } if ( dp [ m - 1 ] [ n - 1 ] != Integer . MAX_VALUE ) return dp [ m - 1 ] [ n - 1 ] ; return - 1 ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 2 , 3 , 2 , 1 , 4 } , { 3 , 2 , 5 , 8 , 2 } , { 1 , 1 , 2 , 2 , 1 } } ; int m = 3 , n = 5 ; System . out . println ( " Minimum ▁ number ▁ of ▁ cells " + " ▁ = ▁ " + minCells ( mat , m , n ) ) ; } }
Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays | Java to find largest bitonic sequence such that ; utility Binary search ; function to find LIS in reverse form ; Add boundary case , when array n is zero Depend on smart pointers ; Integer len = 1 ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential candidate of future subsequence It will replace ceil value in tailIndices ; put LIS into vector ; function for finding longest bitonic seq ; find LIS of array 1 in reverse form ; reverse res to get LIS of first array ; reverse array2 and find its LIS ; print result ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > res = new Vector < > ( ) ; static Integer GetCeilIndex ( Integer [ ] arr , Integer [ ] T , Integer l , Integer r , Integer key ) { while ( r - l > 1 ) { Integer m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static void LIS ( Integer [ ] arr , Integer n ) { Integer [ ] tailIndices = new Integer [ n ] ; Integer [ ] prevIndices = new Integer [ n ] ; for ( Integer i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { Integer pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } for ( Integer i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) res . add ( arr [ i ] ) ; } static void longestBitonic ( Integer [ ] arr1 , Integer n1 , Integer [ ] arr2 , Integer n2 ) { LIS ( arr1 , n1 ) ; Collections . reverse ( res ) ; Collections . reverse ( Arrays . asList ( arr2 ) ) ; LIS ( arr2 , n2 ) ; for ( Integer i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . elementAt ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Integer [ ] arr1 = { 1 , 2 , 4 , 3 , 2 } ; Integer [ ] arr2 = { 8 , 6 , 4 , 7 , 8 , 9 } ; Integer n1 = arr1 . length ; Integer n2 = arr2 . length ; longestBitonic ( arr1 , n1 , arr2 , n2 ) ; } }
Maximize the binary matrix by filpping submatrix once | Java program to find maximum number of ones after one flipping in Binary Matrix ; Return number of ones in square submatrix of size k x k starting from ( x , y ) ; Return maximum number of 1 s after flipping a submatrix ; Precomputing the number of 1 s ; Finding the maximum number of 1 s after flipping ; Driver code
class GFG { static final int R = 3 ; static final int C = 3 ; static int cal ( int ones [ ] [ ] , int x , int y , int k ) { return ones [ x + k - 1 ] [ y + k - 1 ] - ones [ x - 1 ] [ y + k - 1 ] - ones [ x + k - 1 ] [ y - 1 ] + ones [ x - 1 ] [ y - 1 ] ; } static int sol ( int mat [ ] [ ] ) { int ans = 0 ; int val = 0 ; int ones [ ] [ ] = new int [ R + 1 ] [ C + 1 ] ; for ( int i = 1 ; i <= R ; i ++ ) for ( int j = 1 ; j <= C ; j ++ ) { if ( mat [ i - 1 ] [ j - 1 ] == 1 ) val = 1 ; ones [ i ] [ j ] = ones [ i - 1 ] [ j ] + ones [ i ] [ j - 1 ] - ones [ i - 1 ] [ j - 1 ] + ( val ) ; } for ( int k = 1 ; k <= Math . min ( R , C ) ; k ++ ) for ( int i = 1 ; i + k - 1 <= R ; i ++ ) for ( int j = 1 ; j + k - 1 <= C ; j ++ ) ans = Math . max ( ans , ( ones [ R ] [ C ] + k * k - 2 * cal ( ones , i , j , k ) ) ) ; return ans ; } static public void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 } , { 0 , 0 , 1 } , { 1 , 0 , 1 } } ; System . out . println ( sol ( mat ) ) ; } }
Level with maximum number of nodes | Java implementation to find the level having maximum number of Nodes ; A binary tree Node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; function to find the level having maximum number of Nodes ; Current level ; Maximum Nodes at same level ; Level having maximum Nodes ; Count Nodes in a level ; If it is maximum till now Update level_no to current level ; Pop complete current level ; Increment for next level ; Driver program to test above ; binary tree formation ; 2 ; / \ ; 1 3 ; / \ \ ; 4 6 8 ; / ; 5
import java . util . * ; class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int maxNodeLevel ( Node root ) { if ( root == null ) return - 1 ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; int level = 0 ; int max = Integer . MIN_VALUE ; int level_no = 0 ; while ( true ) { int NodeCount = q . size ( ) ; if ( NodeCount == 0 ) break ; if ( NodeCount > max ) { max = NodeCount ; level_no = level ; } while ( NodeCount > 0 ) { Node Node = q . peek ( ) ; q . remove ( ) ; if ( Node . left != null ) q . add ( Node . left ) ; if ( Node . right != null ) q . add ( Node . right ) ; NodeCount -- ; } level ++ ; } return level_no ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 8 ) ; root . left . right . left = newNode ( 5 ) ; System . out . println ( " Level ▁ having ▁ maximum ▁ number ▁ of ▁ Nodes ▁ : ▁ " + maxNodeLevel ( root ) ) ; } }
Minimum steps to minimize n as per given condition | A tabulation based solution in Java ; Driver Code
import java . io . * ; class GFG { static int getMinSteps ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { int min = dp [ i - 1 ] ; if ( i % 2 == 0 ) { min = Math . min ( min , dp [ i / 2 ] ) ; } if ( i % 3 == 0 ) { min = Math . min ( min , dp [ i / 3 ] ) ; } dp [ i ] = min + 1 ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( getMinSteps ( n ) ) ; } }
Maximum points collected by two persons allowed to meet once | Java program to find maximum points that can be collected by two persons in a matrix . ; To store points collected by Person P1 when he / she begins journy from start and from end . ; To store points collected by Person P2 when he / she begins journey from start and from end . ; Table for P1 's journey from  start to meeting cell ; Table for P1 's journey from  end to meet cell ; Table for P2 's journey from start to meeting cell ; Table for P2 's journey from end to meeting cell ; Now iterate over all meeting positions ( i , j ) ; Driver code ; input the calories burnt matrix
class GFG { static final int M = 3 ; static final int N = 3 ; static int findMaxPoints ( int A [ ] [ ] ) { int [ ] [ ] P1S = new int [ M + 2 ] [ N + 2 ] ; int [ ] [ ] P1E = new int [ M + 2 ] [ N + 2 ] ; int [ ] [ ] P2S = new int [ M + 2 ] [ N + 2 ] ; int [ ] [ ] P2E = new int [ M + 2 ] [ N + 2 ] ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 1 ; j <= M ; j ++ ) P1S [ i ] [ j ] = Math . max ( P1S [ i - 1 ] [ j ] , P1S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = N ; i >= 1 ; i -- ) for ( int j = M ; j >= 1 ; j -- ) P1E [ i ] [ j ] = Math . max ( P1E [ i + 1 ] [ j ] , P1E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = N ; i >= 1 ; i -- ) for ( int j = 1 ; j <= M ; j ++ ) P2S [ i ] [ j ] = Math . max ( P2S [ i + 1 ] [ j ] , P2S [ i ] [ j - 1 ] ) + A [ i - 1 ] [ j - 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = M ; j >= 1 ; j -- ) P2E [ i ] [ j ] = Math . max ( P2E [ i - 1 ] [ j ] , P2E [ i ] [ j + 1 ] ) + A [ i - 1 ] [ j - 1 ] ; int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { for ( int j = 2 ; j < M ; j ++ ) { int op1 = P1S [ i ] [ j - 1 ] + P1E [ i ] [ j + 1 ] + P2S [ i + 1 ] [ j ] + P2E [ i - 1 ] [ j ] ; int op2 = P1S [ i - 1 ] [ j ] + P1E [ i + 1 ] [ j ] + P2S [ i ] [ j - 1 ] + P2E [ i ] [ j + 1 ] ; ans = Math . max ( ans , Math . max ( op1 , op2 ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 100 , 100 , 100 } , { 100 , 1 , 100 } , { 100 , 100 , 100 } } ; System . out . print ( " Max ▁ Points ▁ : ▁ " + findMaxPoints ( A ) ) ; } }
Longest subsequence such that difference between adjacents is one | Java program to find the longest subsequence such that the difference between adjacent elements of the subsequence is one . ; Function to find the length of longest subsequence ; Initialize the dp [ ] array with 1 as a single element will be of 1 length ; Start traversing the given array ; Compare with all the previous elements ; If the element is consecutive then consider this subsequence and update dp [ i ] if required . ; Longest length will be the maximum value of dp array . ; Driver code ; Longest subsequence with one difference is { 1 , 2 , 3 , 4 , 3 , 2 }
import java . io . * ; class GFG { static int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } } int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( result < dp [ i ] ) result = dp [ i ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( longestSubseqWithDiffOne ( arr , n ) ) ; } }
Non | Java program to count number of ways to connect n ( wheren is even ) points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; A dynamic programming based function to find nth Catalan number ; Table to store results of subproblems ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Returns count of ways to connect n points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; Throw error if n is odd ; Else return n / 2 'th  Catalan number ; Driver Code
import java . io . * ; class GFG { static int catalanDP ( int n ) { int [ ] catalan = new int [ n + 1 ] ; catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } return catalan [ n ] ; } static int countWays ( int n ) { if ( n < 1 ) { System . out . println ( " Invalid " ) ; return 0 ; } return catalanDP ( n / 2 ) ; } public static void main ( String [ ] args ) { System . out . println ( countWays ( 6 ) + " ▁ " ) ; } }
Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence |  ;  ; Compare previous highest subsequence ; Print list
package BIT ; import java . util . ArrayList ; import java . util . Iterator ; public class LongestIncreasingSubsequence { public static void main ( String [ ] args ) { int array [ ] = { 10 , 9 , 8 , 6 , 5 , 4 } ; ArrayList list = new ArrayList ( ) ; ArrayList longestList = new ArrayList ( ) ; int currentMax ; int highestCount = 0 ; for ( int i = 0 ; i < array . length ; i ++ ) { currentMax = Integer . MIN_VALUE ; for ( int j = i ; j < array . length ; j ++ ) { if ( array [ j ] > currentMax ) { list . add ( array [ j ] ) ; currentMax = array [ j ] ; } } if ( highestCount < list . size ( ) ) { highestCount = list . size ( ) ; longestList = new ArrayList ( list ) ; } list . clear ( ) ; } System . out . println ( ) ; Iterator itr = longestList . iterator ( ) ; System . out . println ( " The ▁ Longest ▁ subsequence " ) ; while ( itr . hasNext ( ) ) { System . out . print ( itr . next ( ) + " ▁ " ) ; } System . out . println ( ) ; System . out . println ( " Length ▁ of ▁ LIS : ▁ " + highestCount ) ; } }
Ways to arrange Balls such that adjacent balls are of different types | Java program to count number of ways to arrange three types of balls such that no two balls of same color are adjacent to each other ; table to store to store results of subproblems ; Returns count of arrangements where last placed ball is ' last ' . ' last ' is 0 for ' p ' , 1 for ' q ' and 2 for ' r ' ; if number of balls of any color becomes less than 0 the number of ways arrangements is 0. ; If last ball required is of type P and the number of balls of P type is 1 while number of balls of other color is 0 the number of ways is 1. ; Same case as above for ' q ' and ' r ' ; If this subproblem is already evaluated ; if last ball required is P and the number of ways is the sum of number of ways to form sequence with ' p - 1' P balls , q Q balss and r R balls ending with Q and R . ; Same as above case for ' q ' and ' r ' ; ( last == 2 ) ; Returns count of required arrangements ; Initialize ' dp ' array ; Three cases arise : return countWays ( p , q , r , 0 ) + Last required balls is type P countWays ( p , q , r , 1 ) + Last required balls is type Q countWays ( p , q , r , 2 ) ; Last required balls is type R ; Driver code
import java . util . Arrays ; class GFG { static final int MAX = 100 ; static int dp [ ] [ ] [ ] [ ] = new int [ MAX ] [ MAX ] [ MAX ] [ 3 ] ; static int countWays ( int p , int q , int r , int last ) { if ( p < 0 q < 0 r < 0 ) return 0 ; if ( p == 1 && q == 0 && r == 0 && last == 0 ) return 1 ; if ( p == 0 && q == 1 && r == 0 && last == 1 ) return 1 ; if ( p == 0 && q == 0 && r == 1 && last == 2 ) return 1 ; if ( dp [ p ] [ q ] [ r ] [ last ] != - 1 ) return dp [ p ] [ q ] [ r ] [ last ] ; if ( last == 0 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ; else if ( last == 1 ) dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q - 1 , r , 0 ) + countWays ( p , q - 1 , r , 2 ) ; else dp [ p ] [ q ] [ r ] [ last ] = countWays ( p , q , r - 1 , 0 ) + countWays ( p , q , r - 1 , 1 ) ; return dp [ p ] [ q ] [ r ] [ last ] ; } static int countUtil ( int p , int q , int r ) { for ( int [ ] [ ] [ ] row : dp ) { for ( int [ ] [ ] innerRow : row ) { for ( int [ ] innerInnerRow : innerRow ) { Arrays . fill ( innerInnerRow , - 1 ) ; } } } ; } public static void main ( String [ ] args ) { int p = 1 , q = 1 , r = 1 ; System . out . print ( countUtil ( p , q , r ) ) ; } }
Count Derangements ( Permutation such that no element appears in its original position ) | A Naive Recursive java program to count derangements ; Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code
import java . io . * ; class GFG { static int countDer ( int n ) { if ( n == 1 ) return 0 ; if ( n == 2 ) return 1 ; return ( n - 1 ) * ( countDer ( n - 1 ) + countDer ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Count ▁ of ▁ Derangements ▁ is ▁ " + countDer ( n ) ) ; } }
Count Derangements ( Permutation such that no element appears in its original position ) | A Dynamic programming based java program to count derangements ; Function to count derangements ; Create an array to store counts for subproblems ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver program
import java . io . * ; class GFG { static int countDer ( int n ) { int der [ ] = new int [ n + 1 ] ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Count ▁ of ▁ Derangements ▁ is ▁ " + countDer ( n ) ) ; } }
Find number of solutions of a linear equation of n variables | A Dynamic programming based Java program to find number of non - negative solutions for a given linear equation ; Returns counr of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver code
import java . util . Arrays ; class GFG { static int countSol ( int coeff [ ] , int n , int rhs ) { int dp [ ] = new int [ rhs + 1 ] ; Arrays . fill ( dp , 0 ) ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; } public static void main ( String [ ] args ) { int coeff [ ] = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . length ; System . out . print ( countSol ( coeff , n , rhs ) ) ; } }