codeparrot/xlcost-text-to-code huggingface dataset:
Distinct palindromic sub | Java implementation of the approach ; Function to return the count of distinct palindromic sub - strings of the given string s ; To store the positions of palindromic sub - strings ; Map to store the sub - strings ; Sub - strings of length 1 are palindromes ; Store continuous palindromic sub - strings ; Store palindromes of size 2 ; If str [ i ... ( i + 1 ) ] is not a palindromic then set dp [ i ] [ i + 1 ] = 0 ; Find palindromic sub - strings of length >= 3 ; End of palindromic substring ; If s [ start ] = = s [ end ] and dp [ start + 1 ] [ end - 1 ] is already palindrome then s [ start ... . end ] is also a palindrome ; Set dp [ start ] [ end ] = 1 ; Not a palindrome ; Return the count of distinct palindromes ; Driver Code
import java . util . HashMap ; class GFG { static int palindromeSubStrs ( String s ) { int [ ] [ ] dp = new int [ s . length ( ) ] [ s . length ( ) ] ; int st , end , i , len ; HashMap < String , Boolean > m = new HashMap < > ( ) ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { dp [ i ] [ i ] = 1 ; m . put ( s . substring ( i , i + 1 ) , true ) ; } for ( i = 0 ; i < s . length ( ) - 1 ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { dp [ i ] [ i + 1 ] = 1 ; m . put ( s . substring ( i , i + 2 ) , true ) ; } else dp [ i ] [ i + 1 ] = 0 ; } for ( len = 3 ; len <= s . length ( ) ; len ++ ) { for ( st = 0 ; st <= s . length ( ) - len ; st ++ ) { end = st + len - 1 ; if ( s . charAt ( st ) == s . charAt ( end ) && dp [ st + 1 ] [ end - 1 ] == 1 ) { dp [ st ] [ end ] = 1 ; m . put ( s . substring ( st , end + 1 ) , true ) ; } else dp [ st ] [ end ] = 0 ; } } return m . size ( ) ; } public static void main ( String [ ] args ) { String s = " abaaa " ; System . out . println ( palindromeSubStrs ( s ) ) ; } }
Maximum sum path in a matrix from top to bottom and back | Java implementation of the approach ; Input matrix ; DP matrix ; Function to return the sum of the cells arr [ i1 ] [ j1 ] and arr [ i2 ] [ j2 ] ; Recursive function to return the required maximum cost path ; Column number of second path ; Base Case ; If already calculated , return from DP matrix ; Recurring for neighbouring cells of both paths together ; Saving result to the DP matrix for current state ; Driver code ; set initial value
import java . util . * ; class GFG { static int n = 4 , m = 4 ; static int arr [ ] [ ] = { { 1 , 0 , 3 , - 1 } , { 3 , 5 , 1 , - 2 } , { - 2 , 0 , 1 , 1 } , { 2 , 1 , - 1 , 1 } } ; static int cache [ ] [ ] [ ] = new int [ 5 ] [ 5 ] [ 5 ] ; static int sum ( int i1 , int j1 , int i2 , int j2 ) { if ( i1 == i2 && j1 == j2 ) { return arr [ i1 ] [ j1 ] ; } return arr [ i1 ] [ j1 ] + arr [ i2 ] [ j2 ] ; } static int maxSumPath ( int i1 , int j1 , int i2 ) { int j2 = i1 + j1 - i2 ; if ( i1 >= n i2 >= n j1 >= m j2 >= m ) { return 0 ; } if ( cache [ i1 ] [ j1 ] [ i2 ] != - 1 ) { return cache [ i1 ] [ j1 ] [ i2 ] ; } int ans = Integer . MIN_VALUE ; ans = Math . max ( ans , maxSumPath ( i1 + 1 , j1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 , j1 + 1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 , j1 + 1 , i2 + 1 ) + sum ( i1 , j1 , i2 , j2 ) ) ; ans = Math . max ( ans , maxSumPath ( i1 + 1 , j1 , i2 ) + sum ( i1 , j1 , i2 , j2 ) ) ; cache [ i1 ] [ j1 ] [ i2 ] = ans ; return ans ; } public static void main ( String args [ ] ) { for ( int i = 0 ; i < 5 ; i ++ ) for ( int i1 = 0 ; i1 < 5 ; i1 ++ ) for ( int i2 = 0 ; i2 < 5 ; i2 ++ ) cache [ i ] [ i1 ] [ i2 ] = - 1 ; System . out . println ( maxSumPath ( 0 , 0 , 0 ) ) ; } }
Bellman Ford Algorithm ( Simple Implementation ) | A Java program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver code ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
class GFG { static void BellmanFord ( int graph [ ] [ ] , int V , int E , int src ) { int [ ] dis = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dis [ i ] = Integer . MAX_VALUE ; dis [ src ] = 0 ; for ( int i = 0 ; i < V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { if ( dis [ graph [ j ] [ 0 ] ] != Integer . MAX_VALUE && dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] < dis [ graph [ j ] [ 1 ] ] ) dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] ; } } for ( int i = 0 ; i < E ; i ++ ) { int x = graph [ i ] [ 0 ] ; int y = graph [ i ] [ 1 ] ; int weight = graph [ i ] [ 2 ] ; if ( dis [ x ] != Integer . MAX_VALUE && dis [ x ] + weight < dis [ y ] ) System . out . println ( " Graph ▁ contains ▁ negative " + " ▁ weight ▁ cycle " ) ; } System . out . println ( " Vertex ▁ Distance ▁ from ▁ Source " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . println ( i + " TABSYMBOL TABSYMBOL " + dis [ i ] ) ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , - 1 } , { 0 , 2 , 4 } , { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 4 , 2 } , { 3 , 2 , 5 } , { 3 , 1 , 1 } , { 4 , 3 , - 3 } } ; BellmanFord ( graph , V , E , 0 ) ; } }
Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal | Java implementation of the approach ; Function to perform simple DFS ; Finding the number of times each bit is set in all the values of a subtree rooted at v ; Checking for each bit whether the numbers with that particular bit as set are either zero in both the resulting trees or greater than zero in both the resulting trees ; Driver code ; Number of nodes ; ArrayList to store the tree ; Array to store the value of nodes ; Array to store the number of times each bit is set in all the values of a subtree ; Array to store the number of times each bit is set in all the values in complete tree ; Finding the set bits in the value of node i ; Add edges
import java . util . * ; public class GFG { static int m [ ] , a [ ] [ ] , x [ ] ; static ArrayList < Integer > g [ ] ; static int ans = 0 ; static void dfs ( int u , int p ) { for ( int v : g [ u ] ) { if ( v != p ) { dfs ( v , u ) ; for ( int i = 0 ; i < 22 ; i ++ ) a [ u ] [ i ] += a [ v ] [ i ] ; } } int pp = 0 ; for ( int i = 0 ; i < 22 ; i ++ ) { if ( ! ( ( a [ u ] [ i ] > 0 && x [ i ] - a [ u ] [ i ] > 0 ) || ( a [ u ] [ i ] == 0 && x [ i ] == 0 ) ) ) { pp = 1 ; break ; } } if ( pp == 0 ) ans ++ ; } public static void main ( String args [ ] ) { int n = 4 ; g = new ArrayList [ n + 1 ] ; m = new int [ n + 1 ] ; m [ 1 ] = 1 ; m [ 2 ] = 3 ; m [ 3 ] = 2 ; m [ 4 ] = 3 ; a = new int [ n + 1 ] [ 22 ] ; x = new int [ 22 ] ; for ( int i = 1 ; i <= n ; i ++ ) { g [ i ] = new ArrayList < > ( ) ; int y = m [ i ] ; int k = 0 ; while ( y != 0 ) { int p = y % 2 ; if ( p == 1 ) { x [ k ] ++ ; a [ i ] [ k ] ++ ; } y = y / 2 ; k ++ ; } } g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; g [ 1 ] . add ( 3 ) ; g [ 3 ] . add ( 1 ) ; g [ 1 ] . add ( 4 ) ; g [ 4 ] . add ( 1 ) ; dfs ( 1 , 0 ) ; System . out . println ( ans ) ; } }
Find the maximum number of composite summands of a number | Java implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array
class GFG { static int maxn = 16 ; static int [ ] precompute ( ) { int dp [ ] = new int [ maxn ] , arr [ ] = { 4 , 6 , 9 } ; for ( int i = 0 ; i < maxn ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < maxn ; ++ i ) { for ( int k = 0 ; k < 3 ; k ++ ) { int j = arr [ k ] ; if ( i >= j && dp [ i - j ] != - 1 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - j ] + 1 ) ; } } } return dp ; } static int Maximum_Summands ( int [ ] dp , int n ) { if ( n < maxn ) return dp [ n ] ; else { int t = ( n - maxn ) / 4 + 1 ; return t + dp [ n - 4 * t ] ; } } public static void main ( String args [ ] ) { int n = 12 ; int [ ] dp = precompute ( ) ; System . out . println ( Maximum_Summands ( dp , n ) ) ; } }
Minimum cost to reach end of array array when a maximum jump of K index is allowed | Java implementation of the approach ; Function for returning the min of two elements ; for calculating the number of elements ; Allocating Memo table and initializing with INT_MAX ; Base case ; For every element relax every reachable element ie relax next k elements ; reaching next k element ; Relaxing the element ; return the last element in the array ; Driver Code
import java . util . * ; class GFG { static int min ( int a , int b ) { return ( a > b ) ? b : a ; } static int minCostJumpsDP ( int [ ] A , int k ) { int size = A . length ; int [ ] x = new int [ size ] ; Arrays . fill ( x , Integer . MAX_VALUE ) ; x [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < i + k + 1 && j < size ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + Math . abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } public static void main ( String [ ] args ) { int [ ] input = { 83 , 26 , 37 , 35 , 33 , 35 , 56 } ; System . out . println ( minCostJumpsDP ( input , 3 ) ) ; } }
DP on Trees | Set | Java program to find diameter of a tree using DFS . ; Function to find the diameter of the tree using Dynamic Programming ; Store the first maximum and secondmax ; Traverse for all children of node ; Call DFS function again ; Find first max ; Secondmaximum ; Find secondmaximum ; Base case for every node ; if ( firstmax != - 1 ) Add ; Find dp [ 2 ] ; Return maximum of both ; Constructed tree is 1 / \ 2 3 / \ 4 5 ; create undirected edges ; Find diameter by calling function
import java . util . * ; public class Main { static int dfs ( int node , int parent , int [ ] dp1 , int [ ] dp2 , Vector < Vector < Integer > > adj ) { int firstmax = - 1 ; int secondmax = - 1 ; for ( int i = 0 ; i < adj . get ( node ) . size ( ) ; ++ i ) { if ( adj . get ( node ) . get ( i ) == parent ) continue ; dfs ( adj . get ( node ) . get ( i ) , node , dp1 , dp2 , adj ) ; if ( firstmax == - 1 ) { firstmax = dp1 [ adj . get ( node ) . get ( i ) ] ; } else if ( dp1 [ adj . get ( node ) . get ( i ) ] >= firstmax ) { secondmax = firstmax ; firstmax = dp1 [ adj . get ( node ) . get ( i ) ] ; } else if ( dp1 [ adj . get ( node ) . get ( i ) ] > secondmax ) { secondmax = dp1 [ adj . get ( node ) . get ( i ) ] ; } } dp1 [ node ] = 1 ; dp1 [ node ] += firstmax ; if ( secondmax != - 1 ) dp2 [ node ] = 1 + firstmax + secondmax ; return Math . max ( dp1 [ node ] , dp2 [ node ] ) ; } public static void main ( String [ ] args ) { int n = 5 ; Vector < Vector < Integer > > adj = new Vector < Vector < Integer > > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { adj . add ( new Vector < Integer > ( ) ) ; } adj . get ( 1 ) . add ( 2 ) ; adj . get ( 2 ) . add ( 1 ) ; adj . get ( 1 ) . add ( 3 ) ; adj . get ( 3 ) . add ( 1 ) ; adj . get ( 2 ) . add ( 4 ) ; adj . get ( 4 ) . add ( 2 ) ; adj . get ( 2 ) . add ( 5 ) ; adj . get ( 5 ) . add ( 2 ) ; int [ ] dp1 = new int [ n + 1 ] ; int [ ] dp2 = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { dp1 [ i ] = 0 ; dp2 [ i ] = 0 ; } System . out . println ( " Diameter ▁ of ▁ the ▁ given ▁ tree ▁ is ▁ " + dfs ( 1 , 1 , dp1 , dp2 , adj ) ) ; } }
Find sub | Java implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call
class GfG { static int N = 4 ; static int getSum ( int r1 , int r2 , int c1 , int c2 , int dp [ ] [ ] ) { return dp [ r2 ] [ c2 ] - dp [ r2 ] [ c1 ] - dp [ r1 ] [ c2 ] + dp [ r1 ] [ c1 ] ; } static boolean sumFound ( int K , int S , int grid [ ] [ ] ) { int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i + 1 ] [ j + 1 ] = dp [ i + 1 ] [ j ] + dp [ i ] [ j + 1 ] - dp [ i ] [ j ] + grid [ i ] [ j ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int sum = getSum ( i , i + K , j , j + K , dp ) ; if ( sum == S ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int K = 2 ; int S = 14 ; if ( sumFound ( K , S , grid ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Stepping Numbers | A Java program to find all the Stepping Number in range [ n , m ] ; Prints all stepping numbers reachable from num and in range [ n , m ] ; Queue will contain all the stepping Numbers ; Get the front element and pop from the queue ; If the Stepping Number is in the range [ n , m ] then display ; If Stepping Number is 0 or greater then m , no need to explore the neighbors ; Get the last digit of the currently visited Stepping Number ; There can be 2 cases either digit to be appended is lastDigit + 1 or lastDigit - 1 ; If lastDigit is 0 then only possible digit after 0 can be 1 for a Stepping Number ; If lastDigit is 9 then only possible digit after 9 can be 8 for a Stepping Number ; Prints all stepping numbers in range [ n , m ] using BFS . ; For every single digit Number ' i ' find all the Stepping Numbers starting with i ; Driver code ; Display Stepping Numbers in the range [ n , m ]
import java . util . * ; class Main { public static void bfs ( int n , int m , int num ) { Queue < Integer > q = new LinkedList < Integer > ( ) ; q . add ( num ) ; while ( ! q . isEmpty ( ) ) { int stepNum = q . poll ( ) ; if ( stepNum <= m && stepNum >= n ) { System . out . print ( stepNum + " ▁ " ) ; } if ( stepNum == 0 stepNum > m ) continue ; int lastDigit = stepNum % 10 ; int stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; int stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) q . add ( stepNumB ) ; else if ( lastDigit == 9 ) q . add ( stepNumA ) ; else { q . add ( stepNumA ) ; q . add ( stepNumB ) ; } } } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = 0 ; i <= 9 ; i ++ ) bfs ( n , m , i ) ; } public static void main ( String args [ ] ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
Minimum number of operations required to sum to binary string S | Java program to find the minimum number of operations required to sum to N ; Function to return the minimum operations required to sum to a number reprented by the binary string S ; Reverse the string to consider it from LSB to MSB ; initialise the dp table ; If S [ 0 ] = '0' , there is no need to perform any operation ; If S [ 0 ] = '1' , just perform a single operation ( i . e Add 2 ^ 0 ) ; Irrespective of the LSB , dp [ 0 ] [ 1 ] is always 1 as there is always the need of making the suffix of the binary string of the form "11 . . . . 1" as suggested by the definition of dp [ i ] [ 1 ] ; Transition from dp [ i - 1 ] [ 0 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of subtracting 2 ^ i 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of subtracting 2 ^ ( i + 1 ) ; Transition from dp [ i - 1 ] [ 1 ] ; 1. Transition from dp [ i - 1 ] [ 1 ] by just doing 1 extra operation of adding 2 ^ ( i + 1 ) 2. Transition from dp [ i - 1 ] [ 0 ] by just doing 1 extra operation of adding 2 ^ i ; Swap values of left and right ; Driver Code
class GFG { static int findMinOperations ( String S ) { S = reverse ( S ) ; int n = S . length ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ 2 ] ; if ( S . charAt ( 0 ) == '0' ) { dp [ 0 ] [ 0 ] = 0 ; } else { dp [ 0 ] [ 0 ] = 1 ; } dp [ 0 ] [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S . charAt ( i ) == '0' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = 1 + Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] ) ; } else { dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] = 1 + Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; } } return dp [ n - 1 ] [ 0 ] ; } 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 S = "100" ; System . out . println ( findMinOperations ( S ) ) ; S = "111" ; System . out . println ( findMinOperations ( S ) ) ; } }
K | Java implementation of above approach ; Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Driver code ; get the Nth value of K - Fibonacci series
public class GFG { static void solve ( int N , int K ) { int Array [ ] = new int [ N + 1 ] ; if ( N <= K ) { System . out . println ( "1" ) ; return ; } int i = 0 ; int sum = K ; for ( i = 1 ; i <= K ; ++ i ) { Array [ i ] = 1 ; } Array [ i ] = sum ; for ( i = K + 2 ; i <= N ; ++ i ) { Array [ i ] = sum - Array [ i - K - 1 ] + Array [ i - 1 ] ; sum = Array [ i ] ; } System . out . println ( Array [ N ] ) ; } public static void main ( String args [ ] ) { int N = 4 , K = 2 ; solve ( N , K ) ; } }
Minimum sum possible of any bracket sequence of length N | Java program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver code
public class GFG { final static int MAX_VAL = 10000000 ; static int dp [ ] [ ] = new int [ 100 ] [ 100 ] ; static int find ( int index , int openbrk , int n , int adj [ ] [ ] ) { if ( openbrk < 0 ) return MAX_VAL ; if ( index == n ) { if ( openbrk == 0 ) { return 0 ; } return MAX_VAL ; } if ( dp [ index ] [ openbrk ] != - 1 ) return dp [ index ] [ openbrk ] ; dp [ index ] [ openbrk ] = Math . min ( adj [ index ] [ 1 ] + find ( index + 1 , openbrk + 1 , n , adj ) , adj [ index ] [ 0 ] + find ( index + 1 , openbrk - 1 , n , adj ) ) ; return dp [ index ] [ openbrk ] ; } public static void main ( String args [ ] ) { int n = 4 ; int adj [ ] [ ] = { { 5000 , 3000 } , { 6000 , 2000 } , { 8000 , 1000 } , { 9000 , 6000 } } ; for ( int i = 0 ; i < dp . length ; i ++ ) for ( int j = 0 ; j < dp . length ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . println ( find ( 1 , 1 , n , adj ) + adj [ 0 ] [ 1 ] ) ; } }
Understanding The Coin Change Problem With Dynamic Programming | We have input values of N and an array Coins that holds all of the coins . We use data type of long because we want to be able to test large values without integer overflow ; Create the ways array to 1 plus the amount to stop overflow ; Set the first way to 1 because its 0 and there is 1 way to make 0 with 0 coins ; Go through all of the coins ; Make a comparison to each index value of ways with the coin value . ; Update the ways array ; return the value at the Nth position of the ways array . ; Driver Code
class getWays { static long getNumberOfWays ( long N , long [ ] Coins ) { long [ ] ways = new long [ ( int ) N + 1 ] ; ways [ 0 ] = 1 ; for ( int i = 0 ; i < Coins . length ; i ++ ) { for ( int j = 0 ; j < ways . length ; j ++ ) { if ( Coins [ i ] <= j ) { ways [ j ] += ways [ ( int ) ( j - Coins [ i ] ) ] ; } } } return ways [ ( int ) N ] ; } static void printArray ( long [ ] coins ) { for ( long i : coins ) System . out . println ( i ) ; } public static void main ( String args [ ] ) { long Coins [ ] = { 1 , 5 , 10 } ; System . out . println ( " The ▁ Coins ▁ Array : " ) ; printArray ( Coins ) ; System . out . println ( " Solution : " ) ; System . out . println ( getNumberOfWays ( 12 , Coins ) ) ; } }
Minimum cost to buy N kilograms of sweet for M persons | Java program to minimum cost to buy N kilograms of sweet for M persons ; Function to find the minimum cost of sweets ; Defining the sweet array ; DP array to store the values ; Since index starts from 1 we reassign the array into sweet ; Assigning base cases for dp array ; At 0 it is free ; Package not available for desirable amount of sweets ; Buying the ' k ' kg package and assigning it to dp array ; If no solution , select from previous k - 1 packages ; If solution does not exist ; Print the solution ; Driver code ; Calling the desired function
public class GFG { static int find ( int m , int n , int adj [ ] ) { int sweet [ ] = new int [ n + 1 ] ; int dp [ ] [ ] [ ] = new int [ n + 1 ] [ n + 1 ] [ n + 1 ] ; sweet [ 0 ] = 0 ; for ( int i = 1 ; i <= m ; ++ i ) sweet [ i ] = adj [ i - 1 ] ; for ( int i = 0 ; i <= m ; ++ i ) { for ( int k = 0 ; k <= n ; ++ k ) dp [ i ] [ 0 ] [ k ] = 0 ; for ( int k = 1 ; k <= n ; ++ k ) dp [ i ] [ k ] [ 0 ] = - 1 ; } for ( int i = 0 ; i <= m ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { for ( int k = 1 ; k <= n ; ++ k ) { dp [ i ] [ j ] [ k ] = - 1 ; if ( i > 0 && j >= k && sweet [ k ] > 0 && dp [ i - 1 ] [ j - k ] [ k ] != - 1 ) dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j - k ] [ k ] + sweet [ k ] ; if ( dp [ i ] [ j ] [ k ] == - 1 || ( dp [ i ] [ j ] [ k - 1 ] != - 1 && dp [ i ] [ j ] [ k ] > dp [ i ] [ j ] [ k - 1 ] ) ) dp [ i ] [ j ] [ k ] = dp [ i ] [ j ] [ k - 1 ] ; } } } if ( dp [ m ] [ n ] [ n ] == - 1 ) return 0 ; else return dp [ m ] [ n ] [ n ] ; } public static void main ( String args [ ] ) { int m = 3 ; int adj [ ] = { 2 , 1 , 3 , 0 , 4 , 10 } ; int n = adj . length ; System . out . println ( find ( m , n , adj ) ) ; } }
Maximum length of segments of 0 ' s ▁ and ▁ 1' s | Java implementation of above approach ; Recursive Function to find total length of the array Where 1 is greater than zero ; If reaches till end ; If dp is saved ; Finding for each length ; If the character scanned is 1 ; If one is greater than zero , add total length scanned till now ; Continue with next length ; Return the value for start index ; Driver code ; Size of string ; Calling the function to find the value of function
import java . util . * ; import java . lang . Math ; class GFG { public static int find ( int start , String adj , int n , int dp [ ] ) { if ( start == n ) return 0 ; if ( dp [ start ] != - 1 ) return dp [ start ] ; dp [ start ] = 0 ; int one = 0 , zero = 0 , k ; for ( k = start ; k < n ; k ++ ) { if ( adj . charAt ( k ) == '1' ) one ++ ; else zero ++ ; if ( one > zero ) dp [ start ] = Math . max ( dp [ start ] , find ( k + 1 , adj , n , dp ) + k - start + 1 ) ; else dp [ start ] = Math . max ( dp [ start ] , find ( k + 1 , adj , n , dp ) ) ; } return dp [ start ] ; } public static void main ( String [ ] args ) { String adj = "100110001010001" ; int n = adj . length ( ) ; int dp [ ] = new int [ n + 1 ] ; Arrays . fill ( dp , - 1 ) ; System . out . println ( find ( 0 , adj , n , dp ) ) ; } }