codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of groups that can receive fresh donuts distributed in batches of size K | Java program for the above approach ; Stores the result of the same recursive calls ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Store the key and check if it is present in the hashmap ; If already calculated ; If left is 0 ; Traverse the array arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Otherwise , traverse the given array arr [ ] ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Memoize the result and return it ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array arr [ ] ; Hashmap to memoize the results ; Store the maximum number of groups ; Return the answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static HashMap < String , Integer > memo ; public static int dfs ( int [ ] V , int left , int K ) { int q = 0 ; String key = Arrays . toString ( V ) ; key += Integer . toString ( left ) ; if ( memo . containsKey ( key ) ) return memo . get ( key ) ; else if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) if ( V [ i ] > 0 ) { V [ i ] -- ; q = Math . max ( q , 1 + dfs ( V , K - i , K ) ) ; V [ i ] ++ ; } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( V [ i ] > 0 ) { V [ i ] -- ; int nleft = i <= left ? left - i : K + left - i ; q = Math . max ( q , dfs ( V , nleft , K ) ) ; V [ i ] ++ ; } } } memo . put ( key , q ) ; return q ; } public static int maxGroups ( int K , int [ ] arr ) { int V [ ] = new int [ K ] ; for ( int x : arr ) V [ x % K ] ++ ; memo = new HashMap < String , Integer > ( ) ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; System . out . println ( maxGroups ( K , arr ) ) ; } }
Maximum Sum Alternating Subarray | Java implementation for the above approach ; Function to find the maximum alternating sum of a subarray for the given array ; Traverse the array ; Store sum of subarrays starting at even indices ; Update sum ; Traverse the array ; Store sum of subarrays starting at odd indices ; Update sum ; Driver code ; Given Input ; Function call
import java . io . * ; class GFG { public static int alternatingSum ( int [ ] arr ) { int sum = 0 ; int sumSoFar = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( i % 2 == 1 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . max ( sum , sumSoFar ) ; } sumSoFar = 0 ; for ( int i = 1 ; i < arr . length ; i ++ ) { if ( i % 2 == 0 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = Math . max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = Math . max ( sum , sumSoFar ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { - 4 , - 10 , 3 , 5 } ; int ans = alternatingSum ( arr ) ; System . out . println ( ans ) ; } }
Smallest subarray with sum greater than a given value |  ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver program to test above functions
class SmallestSubArraySum { static int smallestSubWithSum ( int arr [ ] , int n , int x ) { int min_len = n + 1 ; for ( int start = 0 ; start < n ; start ++ ) { int curr_sum = arr [ start ] ; if ( curr_sum > x ) return 1 ; for ( int end = start + 1 ; end < n ; end ++ ) { curr_sum += arr [ end ] ; if ( curr_sum > x && ( end - start + 1 ) < min_len ) min_len = ( end - start + 1 ) ; } } return min_len ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res1 ) ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res2 ) ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res3 ) ; } }
Smallest subarray with sum greater than a given value | O ( n ) solution for finding smallest subarray with sum greater than x ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver program to test above functions
class SmallestSubArraySum { static int smallestSubWithSum ( int arr [ ] , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res1 ) ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res2 ) ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res3 ) ; } }
Sum of nodes on the longest path from root to leaf node | Java implementation to find the sum of nodes on the longest path from root to leaf node ; Node of a binary tree ; function to find the sum of nodes on the longest path from root to leaf node ; if true , then we have traversed a root to leaf path ; update maximum length and maximum sum according to the given conditions ; recur for left subtree ; recur for right subtree ; utility function to find the sum of nodes on the longest path from root to leaf node ; if tree is NULL , then sum is 0 ; finding the maximum sum ' maxSum ' for the maximum length root to leaf path ; required maximum sum ; Driver program to test above ; binary tree formation ; 4 ; / \ ; 2 5 ; / \ / \ ; 7 1 2 3 ; 6
public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static int maxLen ; static int maxSum ; static void sumOfLongRootToLeafPath ( Node root , int sum , int len ) { if ( root == null ) { if ( maxLen < len ) { maxLen = len ; maxSum = sum ; } else if ( maxLen == len && maxSum < sum ) maxSum = sum ; return ; } sumOfLongRootToLeafPath ( root . left , sum + root . data , len + 1 ) ; sumOfLongRootToLeafPath ( root . right , sum + root . data , len + 1 ) ; } static int sumOfLongRootToLeafPathUtil ( Node root ) { if ( root == null ) return 0 ; maxSum = Integer . MIN_VALUE ; maxLen = 0 ; sumOfLongRootToLeafPath ( root , 0 , 0 ) ; return maxSum ; } public static void main ( String args [ ] ) { Node root = new Node ( 4 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 2 ) ; root . right . right = new Node ( 3 ) ; root . left . right . left = new Node ( 6 ) ; System . out . println ( " Sum ▁ = ▁ " + sumOfLongRootToLeafPathUtil ( root ) ) ; } }
Count L | Java program for the above approach ; Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
import java . io . * ; class GFG { static int numberOfArrays ( int n , int l ) { int [ ] [ ] dp = new int [ l + 1 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int k = j ; k <= n ; k += j ) { dp [ i + 1 ] [ k ] += dp [ i ] [ j ] ; } } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans += dp [ l ] [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , L = 4 ; System . out . println ( numberOfArrays ( N , L ) ) ; } }
Count minimum steps to get the given desired array | Java program to count minimum number of operations to get the given arr array ; Returns count of minimum operations to convert a zero array to arr array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert arr to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of arr don 't become 0. ; To store count of zeroes in current arr array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver method to test the above function
class Test { static int arr [ ] = new int [ ] { 16 , 16 , 16 } ; static int countMinOperations ( int n ) { int result = 0 ; while ( true ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) break ; else if ( arr [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; if ( i == n ) { for ( int j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] / 2 ; result ++ ; } for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] % 2 == 1 ) { arr [ j ] -- ; result ++ ; } } } } public static void main ( String [ ] args ) { System . out . println ( "Minimum number of steps required to NEW_LINE" + " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " + countMinOperations ( arr . length ) ) ; } }
Maximize sum of odd | Java program for the above approach ; Function to find the maximum sum of array elements chosen by Player A according to the given criteria ; Store the key ; Corner Case ; Check if all the elements can be taken or not ; If the difference is less than or equal to the available chances then pick all numbers ; Find the sum of array elements over the range [ start , N ] ; If yes then return that value ; Traverse over the range [ 1 , 2 * M ] ; Sum of elements for Player A ; Even chance sum can be obtained by subtracting the odd chances sum - total and picking up the maximum from that ; Storing the value in dictionary ; Return the maximum sum of odd chances ; Driver code ; Stores the precomputed values ; Function Call
import java . util . * ; import java . awt . Point ; public class GFG { static int recursiveChoosing ( int [ ] arr , int start , int M , HashMap < Point , Integer > dp ) { Point key = new Point ( start , M ) ; if ( start >= arr . length ) { return 0 ; } if ( arr . length - start <= 2 * M ) { int Sum = 0 ; for ( int i = start ; i < arr . length ; i ++ ) { Sum = Sum + arr [ i ] ; } return Sum ; } int sum = 0 ; for ( int i = start ; i < arr . length ; i ++ ) { sum = sum + arr [ i ] ; } int total = sum ; if ( dp . containsKey ( key ) ) { return dp . get ( key ) ; } int psa = 0 ; for ( int x = 1 ; x < 2 * M + 1 ; x ++ ) { int psb = recursiveChoosing ( arr , start + x , Math . max ( x , M ) , dp ) ; psa = Math . max ( psa , total - psb ) ; } dp . put ( key , psa ) ; return dp . get ( key ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 9 , 4 , 4 } ; int N = arr . length ; HashMap < Point , Integer > dp = new HashMap < Point , Integer > ( ) ; System . out . print ( recursiveChoosing ( arr , 0 , 1 , dp ) ) ; } }
Maximum Sum Subsequence made up of consecutive elements of different parity | Java program for the above approach ; Function to find the maximum sum of subsequence with consecutive terms having different parity ; Base Case ; Store the parity of number at the ith position ; If the dp state has already been calculated , return it ; If the array is traversed and no element has been selected yet then select the current element ; If the parity of the current and previously selected element are different , then select the current element ; Skip the current element and move to the next element ; Return the result ; Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Initialize the dp [ ] array with - 1 ; Initially the prev value is set to say 2 , as the first element can anyways be selected ; Driver Code
import java . io . * ; import java . util . Arrays ; class GFG { static int maxSum ( int [ ] arr , int i , int n , int prev , boolean is_selected , int [ ] [ ] dp ) { if ( i == n ) { return 0 ; } int cur = Math . abs ( arr [ i ] ) % 2 ; if ( dp [ i ] [ prev ] != - 1 ) { return dp [ i ] [ prev ] ; } if ( i == n - 1 && is_selected == false ) return dp [ i ] [ prev ] = arr [ i ] ; if ( cur != prev ) { dp [ i ] [ prev ] = arr [ i ] + maxSum ( arr , i + 1 , n , cur , true , dp ) ; } dp [ i ] [ prev ] = Math . max ( dp [ i ] [ prev ] , maxSum ( arr , i + 1 , n , prev , is_selected , dp ) ) ; return dp [ i ] [ prev ] ; } static void maxSumUtil ( int arr [ ] , int n ) { int [ ] [ ] dp = new int [ 100 ] [ 3 ] ; for ( int [ ] arr1 : dp ) Arrays . fill ( arr1 , - 1 ) ; System . out . print ( maxSum ( arr , 0 , n , 2 , false , dp ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 6 , 8 , - 5 , 10 } ; int N = arr . length ; maxSumUtil ( arr , N ) ; } }
Find minimum number of merge operations to make an array palindrome | Java program to find number of operations to make an array palindrome ; Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver method to test the above function
class GFG { static int findMinOps ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 4 , 5 , 9 , 1 } ; System . out . println ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + findMinOps ( arr , arr . length ) ) ; } }
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | Java program to find the smallest positive value that cannot be represented as sum of subsets of a given sorted array ; Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver program to test above functions
class FindSmallestInteger { int findSmallest ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } public static void main ( String [ ] args ) { FindSmallestInteger small = new FindSmallestInteger ( ) ; int arr1 [ ] = { 1 , 3 , 4 , 5 } ; int n1 = arr1 . length ; System . out . println ( small . findSmallest ( arr1 , n1 ) ) ; int arr2 [ ] = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = arr2 . length ; System . out . println ( small . findSmallest ( arr2 , n2 ) ) ; int arr3 [ ] = { 1 , 1 , 1 , 1 } ; int n3 = arr3 . length ; System . out . println ( small . findSmallest ( arr3 , n3 ) ) ; int arr4 [ ] = { 1 , 1 , 3 , 4 } ; int n4 = arr4 . length ; System . out . println ( small . findSmallest ( arr4 , n4 ) ) ; } }
Count distinct possible Bitwise XOR values of subsets of an array | Java program for the above approach ; Stores the Bitwise XOR of every possible subset ; Function to generate all combinations of subsets and store their Bitwise XOR in set S ; If the end of the subset is reached ; Stores the Bitwise XOR of the current subset ; Iterate comb [ ] to find XOR ; Insert the Bitwise XOR of R elements ; Otherwise , iterate to generate all possible subsets ; Recursive call for next index ; Function to find the size of the set having Bitwise XOR of all the subsets of the given array ; Iterate ove the given array ; Generate all possible subsets ; Print the size of the set ; Driver Code ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static HashSet < Integer > s ; static void countXOR ( int arr [ ] , int comb [ ] , int start , int end , int index , int r ) { if ( index == r ) { int new_xor = 0 ; for ( int j = 0 ; j < r ; j ++ ) { new_xor ^= comb [ j ] ; } s . add ( new_xor ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { comb [ index ] = arr [ i ] ; countXOR ( arr , comb , i + 1 , end , index + 1 , r ) ; } } static void maxSizeSet ( int arr [ ] , int N ) { for ( int r = 1 ; r <= N ; r ++ ) { int comb [ ] = new int [ r + 1 ] ; countXOR ( arr , comb , 0 , N - 1 , 0 , r ) ; } System . out . println ( s . size ( ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; s = new HashSet < > ( ) ; maxSizeSet ( arr , N ) ; } }
Size of The Subarray With Maximum Sum | Java program to print length of the largest contiguous array sum ; Function to find maximum subarray sum ; Driver code
class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } return ( end - start + 1 ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; System . out . println ( maxSubArraySum ( a , n ) ) ; } }
Longest subarray of an array which is a subsequence in another array | Java program for the above approach ; Function to find the length of the longest subarray in arr1 [ ] which is a subsequence in arr2 [ ] ; Length of the array arr1 [ ] ; Length of the array arr2 [ ] ; Length of the required longest subarray ; Initialize DP [ ] array ; Traverse array arr1 [ ] ; Traverse array arr2 [ ] ; arr1 [ i - 1 ] contributes to the length of the subarray ; Otherwise ; Find the maximum value present in DP [ ] [ ] ; Return the result ; Driver Code ; Function call to find the length of the longest required subarray
import java . io . * ; class GFG { private static int LongSubarrSeq ( int [ ] arr1 , int [ ] arr2 ) { int M = arr1 . length ; int N = arr2 . length ; int maxL = 0 ; int [ ] [ ] DP = new int [ M + 1 ] [ N + 1 ] ; for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( arr1 [ i - 1 ] == arr2 [ j - 1 ] ) { DP [ i ] [ j ] = 1 + DP [ i - 1 ] [ j - 1 ] ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] ; } } } for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { maxL = Math . max ( maxL , DP [ i ] [ j ] ) ; } } return maxL ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 4 , 2 , 3 , 1 , 5 , 6 } ; int [ ] arr2 = { 3 , 1 , 4 , 6 , 5 , 2 } ; System . out . println ( LongSubarrSeq ( arr1 , arr2 ) ) ; } }
Find minimum difference between any two elements | Java implementation of simple method to find minimum difference between any pair ; Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver method to test the above function
class GFG { static int findMinDiff ( int [ ] arr , int n ) { int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . length ) ) ; } }
Minimize removal or insertions required to make two strings equal | Java program for the above approach ; Function to find the longest substring in string A which is a subsequence in B ; If the indices are out of bounds ; If an already computed subproblem occurred ; Required answer if the all the characters of A and B are the same ; Required answer if there is no substring A which is a subsequence in B ; Required answer if the current character in B is skipped ; The answer for the subproblem is the maximum among the three ; Function to return the minimum strings operations required to make two strings equal ; Initialize the dp vector ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int findLongestSubstring ( int posA , int posB , String A , String B , int canSkip , int n , int dp [ ] [ ] [ ] ) { if ( posA >= n posB >= n ) { return 0 ; } if ( dp [ posA ] [ posB ] [ canSkip ] != - 1 ) { return dp [ posA ] [ posB ] [ canSkip ] ; } int op1 = 0 ; int op2 = 0 ; int op3 = 0 ; if ( A . charAt ( posA ) == B . charAt ( posB ) ) { op1 = 1 + findLongestSubstring ( posA + 1 , posB + 1 , A , B , 0 , n , dp ) ; } if ( canSkip == 1 ) { op2 = findLongestSubstring ( posA + 1 , posB , A , B , canSkip , n , dp ) ; } op3 = findLongestSubstring ( posA , posB + 1 , A , B , canSkip , n , dp ) ; return dp [ posA ] [ posB ] [ canSkip ] = Math . max ( op1 , Math . max ( op2 , op3 ) ) ; } static void minOperations ( String A , String B , int N ) { int [ ] [ ] [ ] dp = new int [ N ] [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( N - findLongestSubstring ( 0 , 0 , A , B , 1 , N , dp ) ) ; } public static void main ( String [ ] args ) { String A = " abab " ; String B = " baba " ; int N = A . length ( ) ; minOperations ( A , B , N ) ; } }