codeparrot/xlcost-text-to-code huggingface dataset:
Count unique paths with given sum in an N | Java program for the above approach ; Function for counting total no of paths possible with the sum is equal to X ; If the path of the sum from the root to current node is stored in sum ; If already computed ; Count different no of paths using all possible ways ; Return total no of paths ; Driver Code ; Stores the number of ways to obtains sums 0 to X ; Function call
import java . io . * ; import java . util . * ; class GFG { static int mod = ( int ) 1e9 + 7 ; static int findTotalPath ( int X , int n , ArrayList < Integer > dp ) { if ( X == 0 ) { return 1 ; } int ans = 0 ; if ( dp . get ( X ) != - 1 ) { return dp . get ( X ) ; } for ( int i = 1 ; i <= Math . min ( X , n ) ; ++ i ) { ans += findTotalPath ( X - i , n , dp ) % mod ; ans %= mod ; } dp . set ( X , ans ) ; return ans ; } public static void main ( String [ ] args ) { int n = 3 , X = 2 ; ArrayList < Integer > dp = new ArrayList < Integer > ( Collections . nCopies ( X + 1 , - 1 ) ) ; System . out . print ( findTotalPath ( X , n , dp ) ) ; } }
Count sequences of positive integers having product X | Java program for the above approach ; Function to print the total number of possible sequences with product X ; Precomputation of binomial coefficients ; Max length of a subsequence ; Ways dp array ; Fill i slots using all the primes ; Subtract ways for all slots that exactly fill less than i slots ; Total possible sequences ; Print the resultant count ; Driver Code ; Function call
import java . util . * ; class GFG { static int [ ] [ ] bin = new int [ 3000 ] [ 3000 ] ; static void countWays ( int [ ] arr ) { int mod = ( int ) 1e9 + 7 ; bin [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < 3000 ; i ++ ) { bin [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod ; } } int n = 0 ; for ( int x : arr ) n += x ; int [ ] ways = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( int j = 0 ; j < arr . length ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod ; } for ( int j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod ; } } int ans = 0 ; for ( int x : ways ) ans = ( ans + x ) % mod ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; countWays ( arr ) ; } }
Maximize trailing zeros in product from top left to bottom right of given Matrix | Java program for the above approach ; Create a class pair to store count of 2 ' s ▁ and ▁ 5' s ; Parameterized Constructor ; Function to covert into Strings ; Function to get maximum no of zeros in product of path from topleft to bottom right ; Base Case ; Store the maximum count of zeros till ith and jth index ; Initialize the ( 0 , 0 ) ; Initialize the first row and column explicitly ; Iterate through all the cells ; Get the pair from the top and from left ; If there are more number of 0 s from top or left ; Print the no of zeros min ( no of 2 ' s , ▁ no ▁ of ▁ 5' s ) ; Function to calculate no of zeros ; Function to calculate no of 2 's ; Return the final count ; Function to calculate no of 5 's ; Return the final count ; Function to add pairs ; Return the resultant pair ; Driver Code ; Given N & M ; Given matrix ; Function Call
import java . io . * ; import java . util . * ; class pair { int x , y ; pair ( int x , int y ) { this . x = x ; this . y = y ; } public String toString ( ) { return " ( " + this . x + " , ▁ " + this . y + " ) " ; } } class GFG { public static void maxZeros ( int [ ] [ ] mat , int n , int m ) { if ( n == 0 m == 0 ) return ; pair dp [ ] [ ] = new pair [ n + 1 ] [ m + 1 ] ; dp [ 0 ] [ 0 ] = new pair ( countTwos ( mat [ 0 ] [ 0 ] ) , countFives ( mat [ 0 ] [ 0 ] ) ) ; for ( int i = 1 ; i < n ; i ++ ) dp [ i ] [ 0 ] = add ( dp [ i - 1 ] [ 0 ] , new pair ( countTwos ( mat [ i ] [ 0 ] ) , countFives ( mat [ i ] [ 0 ] ) ) ) ; for ( int i = 1 ; i < m ; i ++ ) dp [ 0 ] [ i ] = add ( dp [ 0 ] [ i - 1 ] , new pair ( countTwos ( mat [ 0 ] [ i ] ) , countFives ( mat [ 0 ] [ i ] ) ) ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < m ; j ++ ) { pair top = dp [ i - 1 ] [ j ] ; pair left = dp [ i ] [ j - 1 ] ; pair curr = new pair ( countTwos ( mat [ i ] [ j ] ) , countFives ( mat [ i ] [ j ] ) ) ; top = add ( top , curr ) ; left = add ( left , curr ) ; if ( check ( top , left ) ) dp [ i ] [ j ] = top ; else dp [ i ] [ j ] = left ; } } System . out . println ( Math . min ( dp [ n - 1 ] [ m - 1 ] . x , dp [ n - 1 ] [ m - 1 ] . y ) ) ; } public static boolean check ( pair one , pair two ) { int top = Math . min ( one . x , one . y ) ; int left = Math . min ( two . x , two . y ) ; if ( top > left ) return true ; else return false ; } public static int countTwos ( int num ) { int count = 0 ; while ( num != 0 && num % 2 == 0 ) { num /= 2 ; count ++ ; } return count ; } public static int countFives ( int num ) { int count = 0 ; while ( num != 0 && num % 5 == 0 ) { num /= 5 ; count ++ ; } return count ; } public static pair add ( pair one , pair two ) { pair np = new pair ( one . x + two . x , one . y + two . y ) ; return np ; } public static void main ( String [ ] args ) { int N = 3 , M = 4 ; int mat [ ] [ ] = { { 6 , 25 , 4 , 10 } , { 12 , 25 , 1 , 15 } , { 7 , 15 , 15 , 5 } } ; maxZeros ( mat , N , M ) ; } }
Maximum subsequence sum obtained by concatenating disjoint subarrays whose lengths are prime | Java program for the above approach ; Function to return all prime numbers smaller than N ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all its entries as true ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it ; Stores all prime numbers smaller than MAX ; Store all prime numbers ; If p is prime ; Function to build the auxiliary DP array from the start ; Base Case ; Stores all prime numbers < N ; Stores prefix sum ; Update prefix sum ; Iterate over range ; Update each state i . e . . when current element is excluded ; Find start & end index of subarrays when prime [ i ] is taken ; Check if starting point lies in the array ; Include the elements al al + 1 . . . ar ; Check if element lies before start of selected subarray ; Update value of dp [ i ] ; Function to find the maximum sum subsequence with prime length ; Auxiliary DP array ; Build DP array ; Print the result ; Driver Code ; Given arr [ ] ; Size of array ; Function Call
import java . util . * ; class GFG { static int MAX = 100005 ; static Vector < Integer > SieveOfEratosthenes ( ) { boolean [ ] seive = new boolean [ MAX ] ; Arrays . fill ( seive , true ) ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( seive [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) { seive [ i ] = false ; } } } Vector < Integer > v = new Vector < Integer > ( ) ; for ( int p = 2 ; p < MAX ; p ++ ) { if ( seive [ p ] ) { v . add ( p ) ; } } return v ; } static void build ( int dp [ ] , int arr [ ] , int N ) { dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; Vector < Integer > prime = SieveOfEratosthenes ( ) ; int [ ] pref = new int [ N + 1 ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { pref [ i ] = pref [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; for ( int j = 0 ; j <= prime . size ( ) ; j ++ ) { int r = i - 1 ; int l = r - prime . get ( j ) + 1 ; if ( l < 0 ) break ; int temp = 0 ; temp = pref [ r + 1 ] - pref [ l ] ; if ( l - 2 >= 0 ) temp += dp [ l - 2 + 1 ] ; dp [ i ] = Math . max ( dp [ i ] , temp ) ; } } } static void maxSumSubseq ( int arr [ ] , int N ) { int [ ] dp = new int [ N + 1 ] ; build ( dp , arr , N ) ; System . out . print ( dp [ N ] ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 10 , 7 , 10 , 10 , 10 } ; int N = arr . length ; maxSumSubseq ( arr , N ) ; } }
Maximum possible score that can be obtained by constructing a Binary Tree based on given conditions | Java program for the above approach ; Function to find the maximum score for one possible tree having N nodes N - 1 Edges ; Number of nodes ; Initialize dp [ ] [ ] ; Score with 0 vertices is 0 ; Traverse the nodes from 1 to N ; Find maximum scores for each sum ; Iterate over degree of new node ; Update the current state ; Return maximum score for N node tree having 2 ( N - 1 ) sum of degree ; Driver Code ; Given array of scores ; Function Call
import java . util . * ; class GFG { static int maxScore ( int [ ] arr ) { int N = arr . length ; N ++ ; int [ ] [ ] dp = new int [ N + 1 ] [ 2 * ( N - 1 ) + 1 ] ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int s = 1 ; s <= 2 * ( N - 1 ) ; s ++ ) { for ( int j = 1 ; j <= N - 1 && j <= s ; j ++ ) { dp [ i ] [ s ] = Math . max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] ) ; } } } return dp [ N ] [ 2 * ( N - 1 ) ] - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 0 } ; System . out . print ( maxScore ( arr ) ) ; } }
Minimize cost of choosing and skipping array elements to reach end of the given array | Java program for the above approach ; Function to find the minimum cost to reach the end of the array from the first element ; Store the results ; Consider first index cost ; Find answer for each position i ; First Element ; Second Element ; For remaining element ; Consider min cost for skipping ; Last index represents the minimum total cost ; Driver Code ; Given X ; Given array cost [ ] ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void minimumCost ( int [ ] cost , int n , int x ) { int [ ] dp = new int [ n + 2 ] ; dp [ 0 ] = cost [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i == 1 ) dp [ i ] = cost [ i ] + dp [ i - 1 ] ; if ( i == 2 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) ; if ( i >= 3 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , Math . min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ; } System . out . println ( dp [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int X = 4 ; int [ ] cost = { 6 , 3 , 9 , 2 , 1 , 3 } ; int N = cost . length ; minimumCost ( cost , N , X ) ; } }
Maximum possible sum of non | Java program for the above approach ; Function to find the maximum sum not exceeding K possible by selecting a subset of non - adjacent elements ; Base Case ; Not selecting current element ; If selecting current element is possible ; Return answer ; Driver Code ; Given array arr [ ] ; Given K ; Function Call
import java . io . * ; class GFG { public static int maxSum ( int a [ ] , int n , int k ) { if ( n <= 0 ) return 0 ; int option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = Math . max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 10 , 20 , 30 , 40 } ; int N = arr . length ; int K = 100 ; System . out . println ( maxSum ( arr , N , K ) ) ; } }
Count possible binary strings of length N without P consecutive 0 s and Q consecutive 1 s | Java program to implement the above approach ; Function to check if a string satisfy the given condition or not ; Stores the length of string ; Stores the previous character of the string ; Stores the count of consecutive equal characters ; Traverse the string ; If current character is equal to the previous character ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Reset value of cnt ; If count of consecutive 1 s is more than Q ; If count of consecutive 0 s is more than P ; Function to count all distinct binary strings that satisfy the given condition ; Stores the length of str ; If length of str is N ; If str satisfy the given condition ; If str does not satisfy the given condition ; Append a character '0' at end of str ; Append a character '1' at end of str ; Return total count of binary strings ; Driver Code
class GFG { static boolean checkStr ( String str , int P , int Q ) { int N = str . length ( ) ; char prev = str . charAt ( 0 ) ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == prev ) { cnt ++ ; } else { if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str . charAt ( i ) ; } if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } return true ; } static int cntBinStr ( String str , int N , int P , int Q ) { int len = str . length ( ) ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } int X = cntBinStr ( str + '0' , N , P , Q ) ; int Y = cntBinStr ( str + '1' , N , P , Q ) ; return X + Y ; } public static void main ( String [ ] args ) { int N = 5 , P = 2 , Q = 3 ; System . out . println ( cntBinStr ( " " , N , P , Q ) ) ; } }
Count possible permutations of given array satisfying the given conditions | Java Program to implement the above approach ; Function to get the value of binomial coefficient ; Stores the value of binomial coefficient ; Since C ( N , R ) = C ( N , N - R ) ; Calculate the value of C ( N , R ) ; Function to get the count of permutations of the array that satisfy the condition ; Stores count of permutations of the array that satisfy the given condition ; Stores the value of C ( 2 N , N ) ; Stores the value of catalan number ; Return answer ; Driver Code
import java . io . * ; class GFG { static int binCoff ( int N , int R ) { int res = 1 ; if ( R > ( N - R ) ) { R = ( N - R ) ; } for ( int i = 0 ; i < R ; i ++ ) { res *= ( N - i ) ; res /= ( i + 1 ) ; } return res ; } static int cntPermutation ( int N ) { int cntPerm ; int C_2N_N = binCoff ( 2 * N , N ) ; cntPerm = C_2N_N / ( N + 1 ) ; return cntPerm ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( cntPermutation ( N / 2 ) ) ; } }
Smallest submatrix with Kth maximum XOR | Java Program for above approach ; Function to print smallest index of Kth maximum Xor value of submatrices ; Dimensions of matrix ; Stores XOR values for every index ; Min heap to find the kth maximum XOR value ; Stores indices for corresponding XOR values ; Traversing matrix to calculate XOR values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xor [ i ] [ j ] ; Stores the kth maximum element ; Print the required index ; Driver Code ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void smallestPosition ( int m [ ] [ ] , int k ) { int n = m . length ; int mm = m [ 0 ] . length ; int [ ] [ ] xor = new int [ n ] [ mm ] ; PriorityQueue < Integer > minHeap = new PriorityQueue < > ( ) ; Map < Integer , int [ ] > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xor [ i - 1 ] [ j ] : 0 ; int b = j - 1 >= 0 ? xor [ i ] [ j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xor [ i - 1 ] [ j - 1 ] : 0 ; xor [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c ; minHeap . add ( xor [ i ] [ j ] ) ; if ( minHeap . size ( ) > k ) { minHeap . poll ( ) ; } if ( ! map . containsKey ( xor [ i ] [ j ] ) ) map . put ( xor [ i ] [ j ] , new int [ ] { i , j } ) ; } } int kth_max_e = minHeap . poll ( ) ; System . out . println ( ( map . get ( kth_max_e ) [ 0 ] + 1 ) + " ▁ " + ( map . get ( kth_max_e ) [ 1 ] + 1 ) ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k ) ; } }
Length of Longest Palindrome Substring | Java program for the above approach ; Function to obtain the length of the longest palindromic substring ; Length of given string ; Stores the maximum length ; Iterate over the string ; Iterate over the string ; Check for palindrome ; If string [ i , j - i + 1 ] is palindromic ; Return length of LPS ; Driver Code ; Given string ; Function call
import java . io . * ; class GFG { static int longestPalSubstr ( String str ) { int n = str . length ( ) ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i ; j < str . length ( ) ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str . charAt ( i + k ) != str . charAt ( j - k ) ) flag = 0 ; if ( flag != 0 && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } return maxLength ; } public static void main ( String [ ] args ) { String str = " forgeeksskeegfor " ; System . out . print ( longestPalSubstr ( str ) ) ; } }
Maximize cost to empty an array by removing contiguous subarrays of equal elements | Java program for the above approach ; Initialize dp array ; Function that removes elements from array to maximize the cost ; Base case ; Check if an answer is stored ; Deleting count + 1 i . e . including the first element and starting a new sequence ; Removing [ left + 1 , i - 1 ] elements to continue with previous sequence ; Store the result ; Return answer ; Function to remove the elements ; Function call ; Driver Code ; Given array ; Function call
import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 101 ] [ 101 ] [ 101 ] ; static int helper ( int arr [ ] , int left , int right , int count , int m ) { if ( left > right ) return 0 ; if ( dp [ left ] [ right ] [ count ] != - 1 ) { return dp [ left ] [ right ] [ count ] ; } int ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( int i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = Math . max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left ] [ right ] [ count ] = ans ; return ans ; } static int maxPoints ( int arr [ ] , int n , int m ) { int len = n ; for ( int i = 0 ; i < 101 ; i ++ ) { for ( int j = 0 ; j < 101 ; j ++ ) { for ( int k = 0 ; k < 101 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } return helper ( arr , 0 , len - 1 , 0 , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 } ; int M = 3 ; int N = arr . length ; System . out . print ( maxPoints ( arr , N , M ) ) ; } }
Minimum cost to convert M to N by repeated addition of its even divisors | Java program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function Call ; If conversion is not possible ; Print the cost
import java . util . * ; public class GFG { static int inf = 1000000008 ; public static int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } public static void main ( String args [ ] ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; System . out . println ( minimum_cost ) ; } }
Represent a number as the sum of positive numbers ending with 9 | Java program for the above approach ; Function to find the minimum count of numbers ending with 9 to form N ; Extract last digit of N ; Calculate the last digit ; If the last digit satisfies the condition ; Driver Code
import java . util . * ; class GFG { static int minCountOfNumbers ( int N ) { int k = N % 10 ; int z = N - ( 9 * ( 9 - k ) ) ; if ( z >= 9 && z % 10 == 9 ) { return 10 - k ; } else return - 1 ; } public static void main ( String [ ] args ) { int N = 156 ; System . out . print ( minCountOfNumbers ( N ) ) ; } }
Binary Matrix after flipping submatrices in given range for Q queries | Java program to implement the above approach ; Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call
import java . util . * ; import java . lang . * ; class GFG { static void manipulation ( int [ ] [ ] matrix , int [ ] q ) { int x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( int i = x1 - 1 ; i < x2 ; i ++ ) { for ( int j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i ] [ j ] == 1 ) matrix [ i ] [ j ] = 0 ; else matrix [ i ] [ j ] = 1 ; } } } static void queries_fxn ( int [ ] [ ] matrix , int [ ] [ ] queries ) { for ( int [ ] q : queries ) manipulation ( matrix , q ) ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int [ ] [ ] queries = { { 1 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 3 } } ; queries_fxn ( matrix , queries ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < matrix . length ; i ++ ) { System . out . print ( " [ " ) ; for ( int j = 0 ; j < matrix [ i ] . length ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; if ( i == matrix . length - 1 ) System . out . print ( " ] " ) ; else System . out . print ( " ] , ▁ " ) ; } System . out . print ( " ] " ) ; } }
Minimum repeated addition of even divisors of N required to convert N to M | Java program for the above approach ; INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; if i is divisor of cur ; if i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Driver Code ; Given N and M ; Function Call
class GFG { static int INF = ( int ) 1e7 ; static int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return op ; } static void min_operations ( int N , int M ) { int op = min_op ( N , M ) ; if ( op >= INF ) System . out . print ( " - 1" ) ; else System . out . print ( op + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 6 , M = 24 ; min_operations ( N , M ) ; } }