codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost to reach a point N from 0 with two different operations allowed | Java implementation of above approach ; Function to return minimum cost to reach destination ; Initialize cost to 0 ; going backwards until we reach initial position ; if 2 * X jump is better than X + 1 ; if X + 1 jump is better ; return cost ; Driver program
class GFG { static int minCost ( int N , int P , int Q ) { int cost = 0 ; while ( N > 0 ) { if ( ( N & 1 ) > 0 ) { cost += P ; N -- ; } else { int temp = N / 2 ; if ( temp * P > Q ) cost += Q ; else cost += P * temp ; N /= 2 ; } } return cost ; } public static void main ( String [ ] args ) { int N = 9 , P = 5 , Q = 1 ; System . out . println ( minCost ( N , P , Q ) ) ; } }
Number of ways to reach at starting node after travelling through exactly K edges in a complete graph | Java program for the above approach ; Function to find number of ways to reach from node 1 to 1 again , after moving exactly K edges ; Initialize a dp [ ] array , where dp [ i ] stores number of ways to reach at a i node ; Initialize the dp array with 0 ; Base Case ; Iterate for the number of edges moved ; Sum will store number of ways to reach all the nodes ; Iterate for every possible state for the current step ; Update the value of the dp array after travelling each edge ; Print dp [ 0 ] as the answer ; Driver Code ; Given Input ; Function Call
class GFG { static void numberOfWays ( int n , int k ) { int [ ] dp = new int [ 1000 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { int numWays = 0 ; for ( int j = 0 ; j < n ; j ++ ) { numWays += dp [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { dp [ j ] = numWays - dp [ j ] ; } } System . out . println ( dp [ 0 ] + "NEW_LINE"); } public static void main ( String args [ ] ) { int N = 5 , K = 3 ; numberOfWays ( N , K ) ; } }
Count possible decoding of a given digit sequence with hidden characters | Java program for the above approach ; If s [ i ] = = ' * ' there can be 9 possible values of * ; If previous character is 1 then words that can be formed are K ( 11 ) , L ( 12 ) , M ( 13 ) , N ( 14 ) O ( 15 ) , P ( 16 ) , Q ( 17 ) , R ( 18 ) , S ( 19 ) ; If previous character is 2 then the words that can be formed are U ( 21 ) , V ( 22 ) , W ( 23 ) , X ( 24 ) Y ( 25 ) , Z ( 26 ) ; If the previous digit is * then all 15 2 - digit characters can be formed ; If s [ i ] != ' * ' ; Adding first in second if s [ i - 1 ] = 1 ; Adding first in second if s [ i - 1 ] = = 2 and s [ i ] <= '6' ; if s [ i - 1 ] == ' * ' the union of above 2 cases has to be done ; Driver code
import java . io . * ; class GFG { static int M = 1000000007 ; static int waysOfDecoding ( String s ) { long first = 1 , second = s . charAt ( 0 ) == ' * ' ? 9 : s . charAt ( 0 ) == '0' ? 0 : 1 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { long temp = second ; if ( s . charAt ( i ) == ' * ' ) { second = 9 * second ; if ( s . charAt ( i - 1 ) == '1' ) second = ( second + 9 * first ) % M ; else if ( s . charAt ( i - 1 ) == '2' ) second = ( second + 6 * first ) % M ; else if ( s . charAt ( i - 1 ) == ' * ' ) second = ( second + 15 * first ) % M ; } else { second = s . charAt ( i ) != '0' ? second : 0 ; if ( s . charAt ( i - 1 ) == '1' ) second = ( second + first ) % M ; else if ( s . charAt ( i - 1 ) == '2' && s . charAt ( i ) <= '6' ) second = ( second + first ) % M ; else if ( s . charAt ( i - 1 ) == ' * ' ) second = ( second + ( s . charAt ( i ) <= '6' ? 2 : 1 ) * first ) % M ; } first = temp ; } return ( int ) second ; } public static void main ( String [ ] args ) { String s = " * " ; System . out . println ( waysOfDecoding ( s ) ) ; } }
Minimum cost of purchasing at least X chocolates | Java program for above approach ; Function to calculate minimum cost of buying least X chocolates ; Base Case ; Include the i - th box ; Exclude the i - th box ; Return the minimum of the above two cases ; Driver Code ; Given array and value of X ; Store the size of the array ; Print the answer
class GFG { static int findMinCost ( int [ ] [ ] arr , int X , int n , int i ) { if ( X <= 0 ) return 0 ; if ( i >= n ) return Integer . MAX_VALUE ; int inc = findMinCost ( arr , X - arr [ i ] [ 0 ] , n , i + 1 ) ; if ( inc != Integer . MAX_VALUE ) inc += arr [ i ] [ 1 ] ; int exc = findMinCost ( arr , X , n , i + 1 ) ; return Math . min ( inc , exc ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 4 , 3 } , { 3 , 2 } , { 2 , 4 } , { 1 , 3 } , { 4 , 2 } } ; int X = 7 ; int n = arr . length ; int ans = findMinCost ( arr , X , n , 0 ) ; if ( ans != Integer . MAX_VALUE ) System . out . println ( ans ) ; else System . out . println ( - 1 ) ; } }
Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers | Java program for the above approach ; Function to calculate the probability for the given sum to be equal to sum in N throws of dice ; Base cases ; Driver code ; Print the answer
import java . util . * ; class GFG { static double find ( int N , int sum ) { if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return 1.0 / 6 ; else return 0 ; } double s = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) s = s + find ( N - 1 , sum - i ) / 6 ; return s ; } public static void main ( String [ ] args ) { int N = 4 , a = 13 , b = 17 ; double probability = 0.0 ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; System . out . format ( " % .6f " , probability ) ; } }
Minimum steps to reduce N to 0 by given operations | Java program for the above approach ; Function to find the minimum number to steps to reduce N to 0 ; Base case ; Recursive Call to count the minimum steps needed ; Return the answer ; Driver Code ; Given Number N ; Function Call
class GFG { static int minDays ( int n ) { if ( n < 1 ) return n ; int cnt = 1 + Math . min ( n % 2 + minDays ( n / 2 ) , n % 3 + minDays ( n / 3 ) ) ; return cnt ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( minDays ( N ) ) ; } }