codeparrot/xlcost-text-to-code huggingface dataset:
Find length of the largest region in Boolean Matrix | Java program to find the length of the largest region in boolean 2D - matrix ; A function to check if a given cell ( row , col ) can be included in DFS ; row number is in range , column number is in range and value is 1 and not yet visited ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; These arrays are used to get row and column numbers of 8 neighbours of a given cell ; Mark this cell as visited ; Recur for all connected neighbours ; increment region length by one ; The main function that returns largest length region of a given boolean 2D matrix ; Make a boolean array to mark visited cells . Initially all cells are unvisited ; Initialize result as 0 and traverse through the all cells of given matrix ; If a cell with value 1 is not ; visited yet , then new region found ; maximum region ; Driver code ; Function call
import java . io . * ; import java . util . * ; class GFG { static int ROW , COL , count ; static boolean isSafe ( int [ ] [ ] M , int row , int col , boolean [ ] [ ] visited ) { return ( ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ( M [ row ] [ col ] == 1 && ! visited [ row ] [ col ] ) ) ; } static void DFS ( int [ ] [ ] M , int row , int col , boolean [ ] [ ] visited ) { int [ ] rowNbr = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; int [ ] colNbr = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; visited [ row ] [ col ] = true ; for ( int k = 0 ; k < 8 ; k ++ ) { if ( isSafe ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ) { count ++ ; DFS ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ; } } } static int largestRegion ( int [ ] [ ] M ) { boolean [ ] [ ] visited = new boolean [ ROW ] [ COL ] ; int result = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( M [ i ] [ j ] == 1 && ! visited [ i ] [ j ] ) { count = 1 ; DFS ( M , i , j , visited ) ; result = Math . max ( result , count ) ; } } } return result ; } public static void main ( String args [ ] ) { int M [ ] [ ] = { { 0 , 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 } } ; ROW = 4 ; COL = 5 ; System . out . println ( largestRegion ( M ) ) ; } }
Lucas Numbers | Iterative Java program to find n 'th Lucas Number ; Iterative function ; declaring base values for positions 0 and 1 ; generating number ; Driver Code
class GFG { static int lucas ( int n ) { int a = 2 , b = 1 , c , i ; if ( n == 0 ) return a ; for ( i = 2 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return b ; } public static void main ( String args [ ] ) { int n = 9 ; System . out . println ( lucas ( n ) ) ; } }
Recursively break a number in 3 parts to get maximum sum | A simple recursive JAVA program to find maximum sum by recursively breaking a number in 3 parts . ; Function to find the maximum sum ; base conditions ; Fill in bottom - up manner using recursive formula . ; Driver program to test the above function
import java . io . * ; class GFG { final int MAX = 1000000 ; static int breakSum ( int n ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = Math . max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ; return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 24 ; System . out . println ( breakSum ( n ) ) ; } }
Longest repeating and non | Java program to find the longest repeated non - overlapping substring ; Returns the longest repeating non - overlapping substring in str ; building table in bottom - up manner ; ( j - i ) > LCSRe [ i - 1 ] [ j - 1 ] to remove overlapping ; updating maximum length of the substring and updating the finishing index of the suffix ; If we have non - empty result , then insert all characters from first character to last character of String ; Driver program to test the above function
class GFG { static String longestRepeatedSubstring ( String str ) { int n = str . length ( ) ; int LCSRe [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; int i , index = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( str . charAt ( i - 1 ) == str . charAt ( j - 1 ) && LCSRe [ i - 1 ] [ j - 1 ] < ( j - i ) ) { LCSRe [ i ] [ j ] = LCSRe [ i - 1 ] [ j - 1 ] + 1 ; if ( LCSRe [ i ] [ j ] > res_length ) { res_length = LCSRe [ i ] [ j ] ; index = Math . max ( i , index ) ; } } else { LCSRe [ i ] [ j ] = 0 ; } } } if ( res_length > 0 ) { for ( i = index - res_length + 1 ; i <= index ; i ++ ) { res += str . charAt ( i - 1 ) ; } } return res ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( longestRepeatedSubstring ( str ) ) ; } }
Minimum cost to fill given weight in a bag | Java program to find minimum cost to get exactly W Kg with given packets ; Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Build the table val [ ] in bottom up manner and return the last entry from the table ; Driver code
import java . util . * ; class Main { public static int minCost ( int cost [ ] , int n ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int min_cost = Integer . MAX_VALUE ; for ( int j = 0 ; j < i ; j ++ ) if ( j < cost . length && cost [ j ] != - 1 ) { min_cost = Math . min ( min_cost , cost [ j ] + dp [ i - j - 1 ] ) ; } dp [ i ] = min_cost ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int cost [ ] = { 10 , - 1 , - 1 , - 1 , - 1 } ; int W = cost . length ; System . out . print ( minCost ( cost , W ) ) ; } }
Printing Maximum Sum Increasing Subsequence | Dynamic Programming solution to construct Maximum Sum Increasing Subsequence ; Utility function to calculate sum of all vector elements ; Function to construct Maximum Sum Increasing Subsequence ; 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 ] ; L [ i ] now stores Maximum Sum Increasing Subsequence of arr [ 0. . i ] that ends with arr [ i ] ; find max ; max will contain result ; Driver Code ; construct and print Max Sum IS of arr
import java . util . * ; class GFG { static int findSum ( Vector < Integer > arr ) { int sum = 0 ; for ( int i : arr ) sum += i ; return sum ; } static void printMaxSumIs ( int [ ] arr , int n ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] L = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] = new Vector < > ( ) ; L [ 0 ] . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( findSum ( L [ i ] ) < findSum ( L [ j ] ) ) ) { for ( int k : L [ j ] ) if ( ! L [ i ] . contains ( k ) ) L [ i ] . add ( k ) ; } } L [ i ] . add ( arr [ i ] ) ; } Vector < Integer > res = new Vector < > ( L [ 0 ] ) ; for ( Vector < Integer > x : L ) if ( findSum ( x ) > findSum ( res ) ) res = x ; for ( int i : res ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 6 , 4 , 5 , 1 } ; int n = arr . length ; printMaxSumIs ( arr , n ) ; } }
Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence | Dynamic Programming solution to conLongest Increasing Subsequence ; Utility function to print LIS ; Function to conand print Longest Increasing Subsequence ; L [ i ] - The longest increasing sub - sequence ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; Start from index 1 ; Do for every j less than i ; L [ i ] = { Max ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores increasing sub - sequence of arr [ 0. . i ] that ends with arr [ i ] ; LIS will be max of all increasing sub - sequences of arr ; max will contain LIS ; Driver function ; construct and print LIS of arr
import java . util . * ; class GFG { static void printLIS ( Vector < Integer > arr ) { for ( int x : arr ) System . out . print ( x + " ▁ " ) ; System . out . println ( ) ; } static void constructPrintLIS ( int 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 [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( L [ i ] . size ( ) < L [ j ] . size ( ) + 1 ) ) L [ i ] = L [ j ] ; } L [ i ] . add ( arr [ i ] ) ; } Vector < Integer > max = L [ 0 ] ; for ( Vector < Integer > x : L ) if ( x . size ( ) > max . size ( ) ) max = x ; printLIS ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 } ; int n = arr . length ; constructPrintLIS ( arr , n ) ; } }
Find if string is K | Java program to find if given String is K - Palindrome or not ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X and Y ; find if given String is K - Palindrome or not ; Find reverse of String ; find longest palindromic subsequence of given String ; If the difference between longest palindromic subsequence and the original String is less than equal to k , then the String is k - palindrome ; Driver code
class GFG { static int lcs ( String X , String Y , int m , int n ) { int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } return L [ m ] [ n ] ; } static boolean isKPal ( String str , int k ) { int n = str . length ( ) ; StringBuilder revStr = new StringBuilder ( str ) ; revStr = revStr . reverse ( ) ; int lps = lcs ( str , revStr . toString ( ) , n , n ) ; return ( n - lps <= k ) ; } public static void main ( String [ ] args ) { String str = " abcdeca " ; int k = 2 ; if ( isKPal ( str , k ) ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } }
Wildcard Pattern Matching | Java program to implement wildcard pattern matching algorithm ; Function that matches input str with given wildcard pattern ; empty pattern can only match with empty string ; lookup table for storing results of subproblems ; initialize lookup table to false ; empty pattern can match with empty string ; Only ' * ' can match with empty string ; fill the table in bottom - up fashion ; Two cases if we see a ' * ' a ) We ignore ' * ' ' ▁ character ▁ and ▁ move ▁ to ▁ next ▁ character ▁ in ▁ the ▁ pattern , ▁ i . e . , ▁ ' * ' ▁ indicates ▁ an ▁ empty ▁ sequence . ▁ b ) ▁ ' * ' character matches with ith  character in input ; Current characters are considered as matching in two cases ( a ) current character of pattern is ' ? ' ( b ) characters actually match ; If characters don 't match ; Driver code ; String pattern = " ba * * * * * ab " ; String pattern = " ba * ab " ; String pattern = " a * ab " ; String pattern = " a * * * * * ab " ; String pattern = " * a * * * * * ab " ; String pattern = " ba * ab * * * * " ; String pattern = " * * * * " ; String pattern = " * " ; String pattern = " aa ? ab " ; String pattern = " b * b " ; String pattern = " a * a " ; String pattern = " baaabab " ; String pattern = " ? baaabab " ; String pattern = " * baaaba * " ;
import java . util . Arrays ; public class GFG { static boolean strmatch ( String str , String pattern , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; boolean [ ] [ ] lookup = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) Arrays . fill ( lookup [ i ] , false ) ; lookup [ 0 ] [ 0 ] = true ; for ( int j = 1 ; j <= m ; j ++ ) if ( pattern . charAt ( j - 1 ) == ' * ' ) lookup [ 0 ] [ j ] = lookup [ 0 ] [ j - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( pattern . charAt ( j - 1 ) == ' * ' ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] || lookup [ i - 1 ] [ j ] ; else if ( pattern . charAt ( j - 1 ) == ' ? ' || str . charAt ( i - 1 ) == pattern . charAt ( j - 1 ) ) lookup [ i ] [ j ] = lookup [ i - 1 ] [ j - 1 ] ; else lookup [ i ] [ j ] = false ; } } return lookup [ n ] [ m ] ; } public static void main ( String args [ ] ) { String str = " baaabab " ; String pattern = " * * * * * ba * * * * * ab " ; if ( strmatch ( str , pattern , str . length ( ) , pattern . length ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find if string is K | A Naive recursive Java program to find if given string is K - Palindrome or not ; find if given string is K - Palindrome or not ; If first string is empty , the only option is to remove all characters of second string ; If second string is empty , the only option is to remove all characters of first string ; If last characters of two strings are same , ignore last characters and get count for remaining strings . ; If last characters are not same , 1. Remove last char from str1 and recur for m - 1 and n 2. Remove last char from str2 and recur for m and n - 1 Take minimum of above two operations return 1 + Math . min ( isKPalRec ( str1 , str2 , m - 1 , n ) , Remove from str1 isKPalRec ( str1 , str2 , m , n - 1 ) ) ; Remove from str2 ; Returns true if str is k palindrome . ; Swap values of left and right ; Driver code
class GFG { static int isKPalRec ( String str1 , String str2 , int m , int n ) { if ( m == 0 ) { return n ; } if ( n == 0 ) { return m ; } if ( str1 . charAt ( m - 1 ) == str2 . charAt ( n - 1 ) ) { return isKPalRec ( str1 , str2 , m - 1 , n - 1 ) ; } } static boolean isKPal ( String str , int k ) { String revStr = str ; revStr = reverse ( revStr ) ; int len = str . length ( ) ; return ( isKPalRec ( str , revStr , len , len ) <= k * 2 ) ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String str = " acdcb " ; int k = 2 ; if ( isKPal ( str , k ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Minimum time to finish tasks without skipping two consecutive | Java program to find minimum time to finish tasks such that no two consecutive tasks are skipped . ; arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included . There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code
import java . io . * ; class GFG { static int minTime ( int arr [ ] , int n ) { if ( n <= 0 ) return 0 ; int incl = arr [ 0 ] ; int excl = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int incl_new = arr [ i ] + Math . min ( excl , incl ) ; int excl_new = incl ; incl = incl_new ; excl = excl_new ; } return Math . min ( incl , excl ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 10 , 5 , 2 , 7 , 10 } ; int n1 = arr1 . length ; System . out . println ( minTime ( arr1 , n1 ) ) ; int arr2 [ ] = { 10 , 5 , 7 , 10 } ; int n2 = arr2 . length ; System . out . println ( minTime ( arr2 , n2 ) ) ; int arr3 [ ] = { 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 } ; int n3 = arr3 . length ; System . out . println ( minTime ( arr3 , n3 ) ) ; } }
A Space Optimized Solution of LCS | Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Find lengths of two strings ; Following steps build L [ m + 1 ] [ n + 1 ] in bottom up fashion . Note that L [ i ] [ j ] contains length of LCS of X [ 0. . i - 1 ] and Y [ 0. . j - 1 ] ; L [ m ] [ n ] contains length of LCS for X [ 0. . n - 1 ] and Y [ 0. . m - 1 ]
class GFG { public static int lcs ( String X , String Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } }
Matrix Exponentiation | JAVA program to find value of f ( n ) where f ( n ) is defined as F ( n ) = F ( n - 1 ) + F ( n - 2 ) + F ( n - 3 ) , n >= 3 Base Cases : F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; A utility function to multiply two matrices a [ ] [ ] and b [ ] [ ] . Multiplication result is stored back in b [ ] [ ] ; Creating an auxiliary matrix to store elements of the multiplication matrix ; storing the multiplication result in a [ ] [ ] ; Updating our matrix ; Function to compute F raise to power n - 2. ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Multiply it with initial values i . e with F ( 0 ) = 0 , F ( 1 ) = 1 , F ( 2 ) = 1 ; Return n 'th term of a series defined  using below recurrence relation.  f(n) is defined as  f(n) = f(n-1) + f(n-2) + f(n-3), n>=3  Base Cases :  f(0) = 0, f(1) = 1, f(2) = 1 ; Driver code
import java . io . * ; class GFG { static void multiply ( int a [ ] [ ] , int b [ ] [ ] ) { int mul [ ] [ ] = new int [ 3 ] [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { mul [ i ] [ j ] = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; } } for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) a [ i ] [ j ] = mul [ i ] [ j ] ; } static int power ( int F [ ] [ ] , int n ) { int M [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 1 ) return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; } static int findNthTerm ( int n ) { int F [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; return power ( F , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " F ( 5 ) ▁ is ▁ " + findNthTerm ( n ) ) ; } }
Count number of ways to fill a " n ▁ x ▁ 4" grid using "1 ▁ x ▁ 4" tiles | Java program to count of ways to place 1 x 4 tiles on n x 4 grid ; Function that count the number of ways to place 1 x 4 tiles on n x 4 grid . ; Create a table to store results of sub - problems dp [ i ] stores count of ways for i x 4 grid . ; Fill the table from d [ 1 ] to dp [ n ] ; Base cases ; dp ( i - 1 ) : Place first tile horizontally dp ( i - 4 ) : Place first tile vertically which means 3 more tiles have to be placed vertically . ; Driver program
import java . io . * ; class Grid { static int count ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i >= 1 && i <= 3 ) dp [ i ] = 1 ; else if ( i == 4 ) dp [ i ] = 2 ; else { dp [ i ] = dp [ i - 1 ] + dp [ i - 4 ] ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Count ▁ of ▁ ways ▁ is : ▁ " + count ( n ) ) ; } }
Count Derangements ( Permutation such that no element appears in its original position ) | Java implementation of the above approach ; Function to count derangements ; Base case ; Variable for storing prev values ; manner using above recursive formula ; Return result for n ; Driver Code
import java . io . * ; class GFG { static int countDer ( int n ) { if ( n == 1 n == 2 ) { return n - 1 ; } int a = 0 ; int b = 1 ; for ( int i = 3 ; i <= n ; ++ i ) { int cur = ( i - 1 ) * ( a + b ) ; a = b ; b = cur ; } return b ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Count ▁ of ▁ Dearrangements ▁ is ▁ " + countDer ( n ) ) ; } }
Bell Numbers ( Number of ways to Partition a Set ) | Java program to find n 'th Bell number ; Function to find n 'th Bell Number ; Explicitly fill for j = 0 ; Fill for remaining values of j ; Driver program
import java . io . * ; class GFG { static int bellNumber ( int n ) { int [ ] [ ] bell = new int [ n + 1 ] [ n + 1 ] ; bell [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { bell [ i ] [ 0 ] = bell [ i - 1 ] [ i - 1 ] ; for ( int j = 1 ; j <= i ; j ++ ) bell [ i ] [ j ] = bell [ i - 1 ] [ j - 1 ] + bell [ i ] [ j - 1 ] ; } return bell [ n ] [ 0 ] ; } public static void main ( String [ ] args ) { for ( int n = 0 ; n <= 5 ; n ++ ) System . out . println ( " Bell ▁ Number ▁ " + n + " ▁ is ▁ " + bellNumber ( n ) ) ; } }
Count number of ways to cover a distance | A Dynamic Programming based Java program to count number of ways to cover a distance with 1 , 2 and 3 steps ; Function returns count of ways to cover ' dist ' ; Initialize base values . There is one way to cover 0 and 1 distances and two ways to cover 2 distance ; Fill the count array in bottom up manner ; driver program
import java . io . * ; class GFG { static int printCountDP ( int dist ) { int [ ] count = new int [ dist + 1 ] ; count [ 0 ] = 1 ; if ( dist >= 1 ) count [ 1 ] = 1 ; if ( dist >= 2 ) count [ 2 ] = 2 ; for ( int i = 3 ; i <= dist ; i ++ ) count [ i ] = count [ i - 1 ] + count [ i - 2 ] + count [ i - 3 ] ; return count [ dist ] ; } public static void main ( String [ ] args ) { int dist = 4 ; System . out . println ( printCountDP ( dist ) ) ; } }
Count even length binary sequences with same sum of first and second half bits | Java program to find remaining chocolates after k iterations ; Returns the count of even length sequences ; Calculate SUM ( ( nCr ) ^ 2 ) ; Compute nCr using nC ( r - 1 ) nCr / nC ( r - 1 ) = ( n + 1 - r ) / r ; ; Driver code
class GFG { static int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . print ( " Count ▁ of ▁ sequences ▁ is ▁ " ) ; System . out . println ( countSeq ( n ) ) ; } }
Remove minimum elements from either side such that 2 * min becomes more than max | Java program of above approach ; A utility function to find minimum of two numbers ; A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the table is filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Driver program to test above functions
class GFG { static int min ( int a , int b ) { return ( a < b ) ? a : b ; } static int min ( int arr [ ] , int l , int h ) { int mn = arr [ l ] ; for ( int i = l + 1 ; i <= h ; i ++ ) { if ( mn > arr [ i ] ) { mn = arr [ i ] ; } } return mn ; } static int max ( int arr [ ] , int l , int h ) { int mx = arr [ l ] ; for ( int i = l + 1 ; i <= h ; i ++ ) { if ( mx < arr [ i ] ) { mx = arr [ i ] ; } } return mx ; } static int minRemovalsDP ( int arr [ ] , int n ) { int table [ ] [ ] = new int [ n ] [ n ] , gap , i , j , mn , mx ; for ( gap = 0 ; gap < n ; ++ gap ) { for ( i = 0 , j = gap ; j < n ; ++ i , ++ j ) { mn = min ( arr , i , j ) ; mx = max ( arr , i , j ) ; table [ i ] [ j ] = ( 2 * mn > mx ) ? 0 : min ( table [ i ] [ j - 1 ] + 1 , table [ i + 1 ] [ j ] + 1 ) ; } } return table [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 4 , 1 , 3 } ; int n = arr . length ; System . out . println ( minRemovalsDP ( arr , n ) ) ; } }