codeparrot/xlcost-text-to-code huggingface dataset:
Size of array after repeated deletion of LIS | Java program to find size of array after repeated deletion of LIS ; Function to conMaximum Sum LIS ; L [ i ] - The Maximum Sum Increasing Subsequence that ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; Start from index 1 ; For every j less than i ; L [ i ] = { MaxSum ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; Set lis = LIS whose size is max among all ; The > sign makes sure that the LIS ending first is chose . ; Function to minimize array ; Find LIS of current array ; If all elements are in decreasing order ; Remove lis elements from current array . Note that both lis [ ] and arr [ ] are sorted in increasing order . ; If first element of lis [ ] is found ; Remove lis element from arr [ ] ; Erase first element of lis [ ] ; print remaining element of array ; print - 1 for empty array ; Driver function ; minimize array after deleting LIS
import java . util . * ; class GFG { static Vector < Integer > findLIS ( Vector < Integer > arr , int n ) { Vector < Integer > [ ] L = new Vector [ n ] ; for ( int i = 0 ; i < L . length ; i ++ ) L [ i ] = new Vector < Integer > ( ) ; L [ 0 ] . add ( arr . elementAt ( 0 ) ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr . elementAt ( i ) > arr . elementAt ( j ) && ( L [ i ] . size ( ) < L [ j ] . size ( ) ) ) L [ i ] = L [ j ] ; } L [ i ] . add ( arr . elementAt ( i ) ) ; } int maxSize = 1 ; Vector < Integer > lis = new Vector < > ( ) ; for ( Vector < Integer > x : L ) { if ( x . size ( ) > maxSize ) { lis = x ; maxSize = x . size ( ) ; } } return lis ; } static void minimize ( int input [ ] , int n ) { Vector < Integer > arr = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr . add ( input [ i ] ) ; while ( arr . size ( ) != 0 ) { Vector < Integer > lis = findLIS ( arr , arr . size ( ) ) ; if ( lis . size ( ) < 2 ) break ; for ( int i = 0 ; i < arr . size ( ) && lis . size ( ) > 0 ; i ++ ) { if ( arr . elementAt ( i ) == lis . elementAt ( 0 ) ) { arr . removeAll ( lis ) ; i -- ; lis . remove ( 0 ) ; } } } int i ; for ( i = 1 ; i < arr . size ( ) ; i ++ ) System . out . print ( arr . elementAt ( i ) + " ▁ " ) ; if ( i == 0 ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int input [ ] = { 3 , 2 , 6 , 4 , 5 , 1 } ; int n = input . length ; minimize ( input , n ) ; } }
Probability of getting at least K heads in N tosses of Coins | JAVA Code for Probability of getting atleast K heads in N tosses of Coins ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins
class GFG { public static double fact [ ] ; public static double probability ( int k , int n ) { double ans = 0 ; for ( int i = k ; i <= n ; ++ i ) ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) ; ans = ans / ( 1 << n ) ; return ans ; } public static void precompute ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 2 ; i < 20 ; ++ i ) fact [ i ] = fact [ i - 1 ] * i ; } public static void main ( String [ ] args ) { fact = new double [ 100 ] ; precompute ( ) ; System . out . println ( probability ( 2 , 3 ) ) ; System . out . println ( probability ( 3 , 6 ) ) ; System . out . println ( probability ( 12 , 18 ) ) ; } }
Count binary strings with k times appearing adjacent two set bits | Java program to count number of binary strings with k times appearing consecutive 1 's. ; dp [ i ] [ j ] [ 0 ] stores count of binary strings of length i with j consecutive 1 ' s ▁ and ▁ ending ▁ at ▁ 0 . ▁ ▁ dp [ i ] [ j ] [1 ] ▁ stores ▁ count ▁ of ▁ binary ▁ ▁ strings ▁ of ▁ length ▁ i ▁ with ▁ j ▁ consecutive ▁ ▁ 1' s and ending at 1. ; If n = 1 and k = 0. ; number of adjacent 1 's can not exceed i-1 ; Driver code
class GFG { static int countStrings ( int n , int k ) { int dp [ ] [ ] [ ] = new int [ n + 1 ] [ k + 1 ] [ 2 ] ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i && j < k + 1 ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ 1 ] ; dp [ i ] [ j ] [ 1 ] = dp [ i - 1 ] [ j ] [ 0 ] ; if ( j - 1 >= 0 ) { dp [ i ] [ j ] [ 1 ] += dp [ i - 1 ] [ j - 1 ] [ 1 ] ; } } } return dp [ n ] [ k ] [ 0 ] + dp [ n ] [ k ] [ 1 ] ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( countStrings ( n , k ) ) ; } }
Friends Pairing Problem | Java program for solution of friends pairing problem Using Recursion ; Returns count of ways n people can remain single or paired up . ; Driver code
class GFG { static int [ ] dp = new int [ 1000 ] ; static int countFriendsPairings ( int n ) { if ( dp [ n ] != - 1 ) return dp [ n ] ; if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ; else return dp [ n ] = n ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 1000 ; i ++ ) dp [ i ] = - 1 ; int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
Friends Pairing Problem | Java soln using mathematical approach ; Returns count of ways n people can remain single or paired up . ; pow of 1 will always be one ; Driver code
import java . util . * ; class GFG { static Vector < Integer > fact ; static void preComputeFact ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) fact . add ( fact . elementAt ( i - 1 ) * i ) ; } static int countFriendsPairings ( int n ) { int ones = n , twos = 1 , ans = 0 ; while ( ones >= 0 ) { ans += fact . elementAt ( n ) / ( twos * fact . elementAt ( ones ) * fact . elementAt ( ( n - ones ) / 2 ) ) ; ones -= 2 ; twos *= 2 ; } return ans ; } public static void main ( String [ ] args ) { fact = new Vector < > ( ) ; fact . add ( 1 ) ; preComputeFact ( 100 ) ; int n = 4 ; System . out . print ( countFriendsPairings ( n ) + "NEW_LINE"); } }
Minimum Sum Path In 3 | Java program for Min path sum of 3D - array ; A utility function that returns minimum of 3 integers ; function to calculate MIN path sum of 3D array ; Initialize first row of tSum array ; Initialize first column of tSum array ; Initialize first width of tSum array ; Initialize first row - First column of tSum array ; Initialize first row - First width of tSum array ; Initialize first width - First column of tSum array ; Construct rest of the tSum array ; Driver program
import java . io . * ; class GFG { static int l = 3 ; static int m = 3 ; static int n = 3 ; static int min ( int x , int y , int z ) { return ( x < y ) ? ( ( x < z ) ? x : z ) : ( ( y < z ) ? y : z ) ; } static int minPathSum ( int arr [ ] [ ] [ ] ) { int i , j , k ; int tSum [ ] [ ] [ ] = new int [ l ] [ m ] [ n ] ; tSum [ 0 ] [ 0 ] [ 0 ] = arr [ 0 ] [ 0 ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) tSum [ i ] [ 0 ] [ 0 ] = tSum [ i - 1 ] [ 0 ] [ 0 ] + arr [ i ] [ 0 ] [ 0 ] ; for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ 0 ] = tSum [ 0 ] [ j - 1 ] [ 0 ] + arr [ 0 ] [ j ] [ 0 ] ; for ( k = 1 ; k < n ; k ++ ) tSum [ 0 ] [ 0 ] [ k ] = tSum [ 0 ] [ 0 ] [ k - 1 ] + arr [ 0 ] [ 0 ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ i ] [ j ] [ 0 ] = min ( tSum [ i - 1 ] [ j ] [ 0 ] , tSum [ i ] [ j - 1 ] [ 0 ] , Integer . MAX_VALUE ) + arr [ i ] [ j ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ 0 ] [ k ] = min ( tSum [ i - 1 ] [ 0 ] [ k ] , tSum [ i ] [ 0 ] [ k - 1 ] , Integer . MAX_VALUE ) + arr [ i ] [ 0 ] [ k ] ; for ( k = 1 ; k < n ; k ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ k ] = min ( tSum [ 0 ] [ j ] [ k - 1 ] , tSum [ 0 ] [ j - 1 ] [ k ] , Integer . MAX_VALUE ) + arr [ 0 ] [ j ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ j ] [ k ] = min ( tSum [ i - 1 ] [ j ] [ k ] , tSum [ i ] [ j - 1 ] [ k ] , tSum [ i ] [ j ] [ k - 1 ] ) + arr [ i ] [ j ] [ k ] ; return tSum [ l - 1 ] [ m - 1 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] [ ] = { { { 1 , 2 , 4 } , { 3 , 4 , 5 } , { 5 , 2 , 1 } } , { { 4 , 8 , 3 } , { 5 , 2 , 1 } , { 3 , 4 , 2 } } , { { 2 , 4 , 1 } , { 3 , 1 , 4 } , { 6 , 3 , 8 } } } ; System . out . println ( minPathSum ( arr ) ) ; } }
Printing brackets in Matrix Chain Multiplication Problem | Java program to print optimal parenthesization in matrix chain multiplication . ; Function for printing the optimal parenthesization of a matrix chain product ; If only one matrix left in current segment ; Recursively put brackets around subexpression from i to bracket [ i ] [ j ] . Note that " * ( ( bracket + i * n ) + j ) " is similar to bracket [ i ] [ j ] ; Recursively put brackets around subexpression from bracket [ i ] [ j ] + 1 to j . ; Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n Please refer below article for details of this function https : goo . gl / k6EYKj ; * For simplicity of the program , one extra row and one extra column are * allocated in m [ ] [ ] . 0 th row and 0 th column of m [ ] [ ] are not used ; bracket [ i ] [ j ] stores optimal break point in subexpression from i to j . ; cost is zero when multiplying one matrix . ; L is chain length . ; q = cost / scalar multiplications ; Each entry bracket [ i , j ] = k shows where to split the product arr i , i + 1. . . . j for the minimum cost . ; The first matrix is printed as ' A ' , next as ' B ' , and so on ; Driver code
class GFG { static char name ; static void printParenthesis ( int i , int j , int n , int [ ] [ ] bracket ) { if ( i == j ) { System . out . print ( name ++ ) ; return ; } System . out . print ( " ( " ) ; printParenthesis ( i , bracket [ i ] [ j ] , n , bracket ) ; printParenthesis ( bracket [ i ] [ j ] + 1 , j , n , bracket ) ; System . out . print ( " ) " ) ; } static void matrixChainOrder ( int p [ ] , int n ) { int [ ] [ ] m = new int [ n ] [ n ] ; int [ ] [ ] bracket = new int [ n ] [ n ] ; for ( int i = 1 ; i < n ; i ++ ) m [ i ] [ i ] = 0 ; for ( int L = 2 ; L < n ; L ++ ) { for ( int i = 1 ; i < n - L + 1 ; i ++ ) { int j = i + L - 1 ; m [ i ] [ j ] = Integer . MAX_VALUE ; for ( int k = i ; k <= j - 1 ; k ++ ) { int q = m [ i ] [ k ] + m [ k + 1 ] [ j ] + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( q < m [ i ] [ j ] ) { m [ i ] [ j ] = q ; bracket [ i ] [ j ] = k ; } } } } name = ' A ' ; System . out . print ( " Optimal ▁ Parenthesization ▁ is ▁ : ▁ " ) ; printParenthesis ( 1 , n - 1 , n , bracket ) ; System . out . print ( " Optimal Cost is : " + m[1][n - 1]); } public static void main ( String [ ] args ) { int arr [ ] = { 40 , 20 , 30 , 10 , 30 } ; int n = arr . length ; matrixChainOrder ( arr , n ) ; } }
Maximum path sum in a triangle . | Java Program for Dynamic Programming implementation of Max sum problem in a triangle ; Function for finding maximum sum ; loop for bottom - up calculation ; for each element , check both elements just below the number and below right to the number add the maximum of them to it ; return the top element which stores the maximum sum ; Driver program to test above functions
import java . io . * ; class GFG { static int N = 3 ; static int maxPathSum ( int tri [ ] [ ] , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ; } } return tri [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { int tri [ ] [ ] = { { 1 , 0 , 0 } , { 4 , 8 , 0 } , { 1 , 5 , 3 } } ; System . out . println ( maxPathSum ( tri , 2 , 2 ) ) ; } }
Find Maximum dot product of two arrays with insertion of 0 's | Java program to find maximum dot product of two array ; Function to compute Maximum Dot Product and return it ; Create 2D Matrix that stores dot product dp [ i + 1 ] [ j + 1 ] stores product considering B [ 0. . i ] and A [ 0. . . j ] . Note that since all m > n , we fill values in upper diagonal of dp [ ] [ ] ; Traverse through all elements of B [ ] ; Consider all values of A [ ] with indexes greater than or equal to i and compute dp [ i ] [ j ] ; Two cases arise 1 ) Include A [ j ] 2 ) Exclude A [ j ] ( insert 0 in B [ ] ) ; return Maximum Dot Product ; Driver code
import java . util . * ; class GFG { static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) { int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] ) ; return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 1 , 7 , 8 } ; int B [ ] = { 3 , 6 , 7 } ; int m = A . length ; int n = B . length ; System . out . print ( MaxDotProduct ( A , B , m , n ) ) ; } }
LCS ( Longest Common Subsequence ) of three strings | Java program to find LCS of three strings ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] and Z [ 0. . o - 1 ] ; Driver code
class GFG { static String X = " AGGT12" ; static String Y = "12TXAYB " ; static String Z = "12XBA " ; static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 j == - 1 k == - 1 ) { return 0 ; } if ( dp [ i ] [ j ] [ k ] != - 1 ) { return dp [ i ] [ j ] [ k ] ; } if ( X . charAt ( i ) == Y . charAt ( j ) && Y . charAt ( j ) == Z . charAt ( k ) ) { return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; } else { return dp [ i ] [ j ] [ k ] = Math . max ( Math . max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) ) ; } } 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 ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; System . out . print ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( m - 1 , n - 1 , o - 1 ) ) ; } }
Minimum number of elements which are not part of Increasing or decreasing subsequence in array | Java program to return minimum number of elements which are not part of increasing or decreasing subsequences . ; Return minimum number of elements which is not part of any of the sequence . ; If already calculated , return value . ; If whole array is traversed . ; calculating by considering element as part of decreasing sequence . ; calculating by considering element as part of increasing sequence . ; If cannot be calculated for decreasing sequence . ; After considering once by decreasing sequence , now try for increasing sequence . ; If element cannot be part of any of the sequence . ; After considering element as part of increasing and decreasing sequence trying as not part of any of the sequence . ; Wrapper Function ; Adding two number at the end of array , so that increasing and decreasing sequence can be made . MAX - 2 index is assigned INT_MAX for decreasing sequence because next number of sequence must be less than it . Similarly , for Increasing sequence INT_MIN is assigned to MAX - 1 index . ; Driver Code
import java . util . * ; class GFG { static int MAX = 102 ; static int countMin ( int arr [ ] , int dp [ ] [ ] [ ] , int n , int dec , int inc , int i ) { if ( dp [ dec ] [ inc ] [ i ] != - 1 ) return dp [ dec ] [ inc ] [ i ] ; if ( i == n ) return 0 ; if ( arr [ i ] < arr [ dec ] ) dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , i , inc , i + 1 ) ; if ( arr [ i ] > arr [ inc ] ) { if ( dp [ dec ] [ inc ] [ i ] == - 1 ) dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , dec , i , i + 1 ) ; else dp [ dec ] [ inc ] [ i ] = Math . min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) ; } if ( dp [ dec ] [ inc ] [ i ] == - 1 ) dp [ dec ] [ inc ] [ i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) ; else dp [ dec ] [ inc ] [ i ] = Math . min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) ; return dp [ dec ] [ inc ] [ i ] ; } static int wrapper ( int arr [ ] , int n ) { arr [ MAX - 2 ] = Integer . MAX_VALUE ; arr [ MAX - 1 ] = Integer . MIN_VALUE ; int [ ] [ ] [ ] dp = new int [ MAX ] [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 ) ; } public static void main ( String [ ] args ) { int n = 12 ; int [ ] arr = new int [ MAX ] ; arr [ 0 ] = 7 ; arr [ 1 ] = 8 ; arr [ 2 ] = 1 ; arr [ 3 ] = 2 ; arr [ 4 ] = 4 ; arr [ 5 ] = 6 ; arr [ 6 ] = 3 ; arr [ 7 ] = 5 ; arr [ 8 ] = 2 ; arr [ 9 ] = 1 ; arr [ 10 ] = 8 ; arr [ 11 ] = 7 ; System . out . println ( wrapper ( arr , n ) ) ; } }
Find all distinct subset ( or subsequence ) sums of an array | Java program to print distinct subset sums of a given array . ; sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void distSumRec ( int arr [ ] , int n , int sum , int currindex , HashSet < Integer > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; for ( int i : s ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }
Find all distinct subset ( or subsequence ) sums of an array | Java program to print distinct subset sums of a given array . ; Uses Dynamic Programming to find distinct subset sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with sum equal to j . ; There is always a subset with 0 sum ; Fill dp [ ] [ ] in bottom up manner ; Sums that were achievable without current array element ; Print last row elements ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void printDistSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] = true ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] == true ) { dp [ i ] [ j ] = true ; dp [ i ] [ j + arr [ i - 1 ] ] = true ; } } } for ( int j = 0 ; j <= sum ; j ++ ) if ( dp [ n ] [ j ] == true ) System . out . print ( j + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }
Count number of ways to reach destination in a Maze | Java program to count number of paths in a maze with obstacles . ; Returns count of possible paths in a maze [ R ] [ C ] from ( 0 , 0 ) to ( R - 1 , C - 1 ) ; If the initial cell is blocked , there is no way of moving anywhere ; Initializing the leftmost column ; If we encounter a blocked cell in leftmost row , there is no way of visiting any cell directly below it . ; Similarly initialize the topmost row ; If we encounter a blocked cell in bottommost row , there is no way of visiting any cell directly below it . ; The only difference is that if a cell is - 1 , simply ignore it else recursively compute count value maze [ i ] [ j ] ; If blockage is found , ignore this cell ; If we can reach maze [ i ] [ j ] from maze [ i - 1 ] [ j ] then increment count . ; If we can reach maze [ i ] [ j ] from maze [ i ] [ j - 1 ] then increment count . ; If the final cell is blocked , output 0 , otherwise the answer ; Driver code
import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int maze [ ] [ ] ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( countPaths ( maze ) ) ; } }
Minimum sum subsequence such that at least one of every four consecutive elements is picked | Java program to find minimum sum subsequence of an array such that one of every four consecutive elements is picked . ; Returns sum of minimum sum subsequence such that one of every four consecutive elements is picked from arr [ ] . ; dp [ i ] is going to store minimum sum subsequence of arr [ 0. . i ] such that arr [ i ] is part of the solution . Note that this may not be the best solution for subarray arr [ 0. . i ] ; If there is single value , we get the minimum sum equal to arr [ 0 ] ; If there are two values , we get the minimum sum equal to the minimum of two values ; If there are three values , return minimum of the three elements of array ; If there are four values , return minimum of the four elements of array ; Return the minimum of last 4 index ; Driver code
import java . io . * ; class GFG { static int minSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return Math . min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return Math . min ( arr [ 0 ] , Math . min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return Math . min ( Math . min ( arr [ 0 ] , arr [ 1 ] ) , Math . min ( arr [ 2 ] , arr [ 3 ] ) ) ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = arr [ 1 ] ; dp [ 2 ] = arr [ 2 ] ; dp [ 3 ] = arr [ 3 ] ; for ( int i = 4 ; i < n ; i ++ ) dp [ i ] = arr [ i ] + Math . min ( Math . min ( dp [ i - 1 ] , dp [ i - 2 ] ) , Math . min ( dp [ i - 3 ] , dp [ i - 4 ] ) ) ; return Math . min ( Math . min ( dp [ n - 1 ] , dp [ n - 2 ] ) , Math . min ( dp [ n - 4 ] , dp [ n - 3 ] ) ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 1 } ; int n = arr . length ; System . out . println ( minSum ( arr , n ) ) ; } }
Maximum decimal value path in a binary matrix | Java program to find maximum decimal value path in binary matrix ; Returns maximum decimal value in binary matrix . Here p indicate power of 2 ; Out of matrix boundary ; If current matrix value is 1 then return result + power ( 2 , p ) else result ; Driver program
class GFG { static final int N = 4 ; static int maxDecimalValue ( int mat [ ] [ ] , int i , int j , int p ) { if ( i >= N j >= N ) { return 0 ; } int result = Math . max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) { return ( int ) ( Math . pow ( 2 , p ) + result ) ; } else { return result ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } , { 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 } , } ; System . out . println ( maxDecimalValue ( mat , 0 , 0 , 0 ) ) ; } }