codeparrot/xlcost-text-to-code huggingface dataset:
Count possible N | Java program for the above approach ; Macros for modulus ; DP array for memoization ; Utility function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutively ; If number with N digits is generated ; Create a reference variable ; Check if the current state is already computed before ; Initialize ans as zero ; Check if count of previous digit has reached zero or not ; Fill current position only with digits that are unequal to previous digit ; Else set the value of count for this new digit accordingly from max_digit [ ] ; Function to count N digit numbers with digit i not appearing more than max_digit [ i ] consecutive times ; Stores the final count ; Print the total count ; Driver Code ; Initialize the dp array with - 1 Fill each row with - 1. ; Function Call
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int dp [ ] [ ] [ ] = new int [ 5005 ] [ 12 ] [ 12 ] ; static int findCountUtil ( int N , int maxDigit [ ] , int position , int previous , int count ) { if ( position == N ) { return 1 ; } int ans = dp [ position ] [ previous ] [ count ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( count == 0 && previous != i ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } else if ( count != 0 ) { ans = ( ans + ( findCountUtil ( N , maxDigit , position + 1 , i , ( previous == i && position != 0 ) ? count - 1 : maxDigit [ i ] - 1 ) ) % MOD ) % MOD ; } } return ans ; } static void findCount ( int N , int maxDigit [ ] ) { int position = 0 ; int previous = 0 ; int count = 1 ; int ans = findCountUtil ( N , maxDigit , position , previous , count ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 2 ; int [ ] maxDigit = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int [ ] [ ] row : dp ) { for ( int [ ] rowColumn : row ) { Arrays . fill ( rowColumn , - 1 ) ; } } findCount ( N , maxDigit ) ; } }
Find minimum difference between any two elements | Java program to find minimum difference between any pair in an unsorted array ; Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver method to test the above function
import java . util . Arrays ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; 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 ) ) ; } }
Space optimization using bit manipulations | Java program to mark numbers as multiple of 2 or 5 ; Driver code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1
import java . lang . * ; class GFG { public static void main ( String [ ] args ) { int a = 2 , b = 10 ; int size = Math . abs ( b - a ) + 1 ; int array [ ] = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; System . out . println ( " MULTIPLES ▁ of ▁ 2" + " ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) System . out . printf ( i + " ▁ " ) ; } }
Remove all nodes which don 't lie in any path with sum>= k | Java program to implement the above approach ; A utility function to get maximum of two integers ; A Binary Tree Node ; A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Initialize left and right sums as sum from root to this node ( including this node ) ; Recursively prune left and right subtrees ; Get the maximum of left and right sums ; If maximum is smaller than k , then this node must be deleted ; A wrapper over pruneUtil ( ) ; Driver Code ; k is 45
import java . util . * ; class GFG { static int max ( int l , int r ) { return ( l > r ? l : r ) ; } static class Node { int data ; Node left , right ; } ; static class INT { int v ; INT ( int a ) { v = a ; } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return node ; } static void print ( Node root ) { if ( root != null ) { print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } static Node pruneUtil ( Node root , int k , INT sum ) { if ( root == null ) return null ; INT lsum = new INT ( sum . v + ( root . data ) ) ; INT rsum = new INT ( lsum . v ) ; root . left = pruneUtil ( root . left , k , lsum ) ; root . right = pruneUtil ( root . right , k , rsum ) ; sum . v = max ( lsum . v , rsum . v ) ; if ( sum . v < k ) { root = null ; } return root ; } static Node prune ( Node root , int k ) { INT sum = new INT ( 0 ) ; return pruneUtil ( root , k , sum ) ; } public static void main ( String args [ ] ) { int k = 45 ; Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 12 ) ; root . right . right . left = newNode ( 10 ) ; root . right . right . left . right = newNode ( 11 ) ; root . left . left . right . left = newNode ( 13 ) ; root . left . left . right . right = newNode ( 14 ) ; root . left . left . right . right . left = newNode ( 15 ) ; System . out . println ( "Tree before truncationNEW_LINE");  print ( root ) ; root = prune ( root , k ) ; System . out . println ( " Tree after truncation ");  print ( root ) ; } }
Tree Traversals ( Inorder , Preorder and Postorder ) | Class containing left and right child of current node and key value ; Root of Binary Tree ; Given a binary tree , print its nodes according to the " bottom - up " postorder traversal . ; first recur on left subtree ; then recur on right subtree ; now deal with the node ; Given a binary tree , print its nodes in inorder ; first recur on left child ; then print the data of node ; now recur on right child ; Given a binary tree , print its nodes in preorder ; first print data of node ; then recur on left sutree ; now recur on right subtree ; Wrappers over above recursive functions ; Driver method
class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( ) { root = null ; } void printPostorder ( Node node ) { if ( node == null ) return ; printPostorder ( node . left ) ; printPostorder ( node . right ) ; System . out . print ( node . key + " ▁ " ) ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . key + " ▁ " ) ; printInorder ( node . right ) ; } void printPreorder ( Node node ) { if ( node == null ) return ; System . out . print ( node . key + " ▁ " ) ; printPreorder ( node . left ) ; printPreorder ( node . right ) ; } void printPostorder ( ) { printPostorder ( root ) ; } void printInorder ( ) { printInorder ( root ) ; } void printPreorder ( ) { printPreorder ( root ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ " ) ; tree . printPreorder ( ) ; System . out . println ( " Inorder traversal of binary tree is "); tree . printInorder ( ) ; System . out . println ( " Postorder traversal of binary tree is "); tree . printPostorder ( ) ; } }
Rearrange array by interchanging positions of even and odd elements in the given array | Java program for the above approach ; Function to replace odd elements with even elements and vice versa ; Push the first element to stack ; iterate the array and swap even and odd ; pop and swap ; print the arr [ ] ; Driver code ; Given array arr ; length of the arr ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void swapEvenOdd ( int arr [ ] , int N ) { Stack < int [ ] > stack = new Stack < > ( ) ; stack . push ( new int [ ] { 0 , arr [ 0 ] } ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( ! stack . isEmpty ( ) ) { if ( arr [ i ] % 2 != stack . peek ( ) [ 1 ] % 2 ) { int pop [ ] = stack . pop ( ) ; int index = pop [ 0 ] , val = pop [ 1 ] ; arr [ index ] = arr [ i ] ; arr [ i ] = val ; } else stack . push ( new int [ ] { i , arr [ i ] } ) ; } else stack . push ( new int [ ] { i , arr [ i ] } ) ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int N = arr . length ; swapEvenOdd ( arr , N ) ; } }
Maximum subarray sum possible after removing at most K array elements | Java program to implement the above approach ; Function to find the maximum subarray sum greater than or equal to 0 by removing K array elements ; Base case ; If overlapping subproblems already occurred ; Include current element in the subarray ; If K elements already removed from the subarray ; Remove current element from the subarray ; Utility function to find the maximum subarray sum by removing at most K array elements ; Stores overlapping subproblems of the recurrence relation ; Initialize dp [ ] [ ] to - 1 ; Stores maximum subarray sum by removing at most K elements ; Calculate maximum element in dp [ ] [ ] ; Update res ; If all array elements are negative ; Update res ; Driver Code
import java . util . * ; class GFG { static final int M = 100 ; static int mxSubSum ( int i , int [ ] arr , int j , int dp [ ] [ ] ) { if ( i == 0 ) { return dp [ i ] [ j ] = Math . max ( 0 , arr [ i ] ) ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } int X = Math . max ( 0 , arr [ i ] + mxSubSum ( i - 1 , arr , j , dp ) ) ; if ( j == 0 ) { return dp [ i ] [ j ] = X ; } int Y = mxSubSum ( i - 1 , arr , j - 1 , dp ) ; return dp [ i ] [ j ] = Math . max ( X , Y ) ; } static int MaximumSubarraySum ( int n , int [ ] arr , int k ) { int [ ] [ ] dp = new int [ M ] [ M ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) dp [ i ] [ j ] = - 1 ; mxSubSum ( n - 1 , arr , k , dp ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { res = Math . max ( res , dp [ i ] [ j ] ) ; } } if ( Arrays . stream ( arr ) . max ( ) . getAsInt ( ) < 0 ) { res = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 1 , 3 , - 2 , 4 , - 7 , 20 } ; int K = 1 ; int N = arr . length ; System . out . print ( MaximumSubarraySum ( N , arr , K ) + "NEW_LINE"); } }
Minimum number of operations required to make all elements of at least one row of given Matrix prime | Java program to implement the above approach ; Function to generate all prime numbers using Sieve of Eratosthenes ; Function to check if a number is prime or not ; prime [ i ] : Check if i is a prime number or not ; Initialize prime [ ] array to true ; Iterate over the range [ 2 , sqrt ( n ) ] ; If p is a prime number ; Mark all multiples of i to false ; Update i ; Function to find minimum operations to make all elements of at least one row of the matrix as prime numbers ; dp [ i ] : Stores minimum operations to get i prime numbers in a row ; Initialize dp [ ] array to ( n + 1 ) ; Traverse the array ; Stores count of prime numbers in a i - th row ; Iterate over the range [ ( 1 << m ) - 1 , 0 ] ; If a row exist which contains j prime numbers ; Update dp [ j bitmask ] ; Update dp [ bitmask ] ; Return minimum operations to get a row of the matrix with all prime numbers ; Function to count prime numbers in a row ; i - th bit of bitmask check if i - th column is a prime or not ; Traverse the array ; if a [ i ] is a prime number ; Update bitmask ; Driver Code ; Stores count of columns in the matrix ; Stores length ; Calculate all prime numbers in range [ 1 , max ] using sieve ; Function Call
import java . io . * ; import java . util . * ; class GFG { private static boolean [ ] prime ; private static void sieve ( int n ) { prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } private static int MinWays ( int [ ] [ ] a , int n , int m ) { int [ ] dp = new int [ 1 << m ] ; Arrays . fill ( dp , n + 1 ) ; for ( int i = 0 ; i < a . length ; i ++ ) { int bitmask = BitMask ( a [ i ] ) ; for ( int j = ( 1 << m ) - 1 ; j >= 0 ; j -- ) { if ( dp [ j ] != n + 1 ) { dp [ j bitmask ] = Math . min ( dp [ j bitmask ] , dp [ j ] + 1 ) ; } } dp [ bitmask ] = 1 ; } return ( dp [ ( 1 << m ) - 1 ] - 1 ) == ( n + 1 ) ? - 1 : ( dp [ ( 1 << m ) - 1 ] - 1 ) ; } private static int BitMask ( int [ ] a ) { int bitmask = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( prime [ a [ i ] ] ) { bitmask |= ( 1 << i ) ; } } return bitmask ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 4 , 6 , 5 , 8 } , { 2 , 9 , 12 , 14 } , { 32 , 7 , 18 , 16 } , { 12 , 4 , 35 , 17 } } ; int m = mat [ 0 ] . length ; int n = mat . length ; int max = 10000 ; sieve ( max ) ; System . out . println ( MinWays ( mat , n , m ) ) ; } }
Minimize cost to empty given array where cost of removing an element is its absolute difference with Time instant | Java program for the above approach ; Function to find the minimum cost to delete all array elements ; Sort the input array ; Store the maximum time to delete the array in the worst case ; Store the result in cost [ ] [ ] table ; Initialize the table cost [ ] [ ] ; Base Case ; Store the minimum of all cost values of the previous index ; Iterate from range [ 1 , n ] using variable i ; Update prev ; Iterate from range [ 1 , m ] using variable j ; Update cost [ i ] [ j ] ; Update the prev ; Store the minimum cost to delete all elements ; Find the minimum of all values of cost [ n ] [ j ] ; Print minimum cost ; Driver Code ; Function Call
import java . util . * ; import java . io . * ; class GFG { static int INF = 10000 ; static void minCost ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int m = 2 * n ; int cost [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { cost [ i ] [ j ] = INF ; } } cost [ 0 ] [ 0 ] = 0 ; int prev = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { prev = cost [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= m ; j ++ ) { cost [ i ] [ j ] = Math . min ( cost [ i ] [ j ] , prev + Math . abs ( j - arr [ i - 1 ] ) ) ; prev = Math . min ( prev , cost [ i - 1 ] [ j ] ) ; } } int minCost = INF ; for ( int j = 1 ; j <= m ; j ++ ) { minCost = Math . min ( minCost , cost [ n ] [ j ] ) ; } System . out . print ( minCost ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 4 , 4 , 5 , 2 } ; int N = arr . length ; minCost ( arr , N ) ; } }
Space optimization using bit manipulations | Java code to for marking multiples ; index >> 5 corresponds to dividing index by 32 index & 31 corresponds to modulo operation of index by 32 Function to check value of bit position whether it is zero or one ; Sets value of bit for corresponding index ; Driver code ; Size that will be used is actual_size / 32 ceil is used to initialize the array with positive number ; Array is dynamically initialized as we are calculating size at run time ; Iterate through every index from a to b and call setbit ( ) if it is a multiple of 2 or 5
import java . io . * ; import java . util . * ; class GFG { static boolean checkbit ( int array [ ] , int index ) { int val = array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; if ( val == 0 ) return false ; return true ; } static void setbit ( int array [ ] , int index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 10 ; int size = Math . abs ( b - a ) ; size = ( int ) Math . ceil ( ( double ) size / 32 ) ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; System . out . println ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) System . out . print ( i + " ▁ " ) ; } }
Longest Span with same Sum in two Binary arrays | A Simple Java program to find longest common subarray of two binary arrays with same sum ; Returns length of the longest common sum in arr1 [ ] and arr2 [ ] . Both are of same size n . ; Initialize result ; One by one pick all possible starting points of subarrays ; Initialize sums of current subarrays ; Conider all points for starting with arr [ i ] ; Update sums ; If sums are same and current length is more than maxLen , update maxLen ; Driver method to test the above function
class Test { static int arr1 [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; static int arr2 [ ] = new int [ ] { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; static int longestCommonSum ( int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLen ) maxLen = len ; } } } return maxLen ; } public static void main ( String [ ] args ) { System . out . print ( " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ with ▁ same ▁ sum ▁ is ▁ " ) ; System . out . println ( longestCommonSum ( arr1 . length ) ) ; } }
Number of Longest Increasing Subsequences | Java program for the above approach ; Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Driver code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int findNumberOfLIS ( int [ ] nums ) { if ( nums . length == 0 ) return 0 ; int n = nums . length ; int [ ] dp_l = new int [ n ] ; Arrays . fill ( dp_l , 1 ) ; int [ ] dp_c = new int [ n ] ; Arrays . fill ( dp_c , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } int max_length = 0 ; for ( int i : dp_l ) max_length = Math . max ( i , max_length ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 4 , 7 } ; System . out . print ( findNumberOfLIS ( arr ) + "NEW_LINE"); } }
Length of the longest increasing subsequence which does not contain a given sequence as Subarray | Java program for the above approach ; Initialize dp and KMP array ; Length of the given sequence [ ] ; Function to find the max - length subsequence that does not have subarray sequence [ ] ; Stores the subsequence explored so far ; Base Case ; Using memoization to avoid re - computation ; Using KMP array to find corresponding index in arr1 [ ] ; Incrementing k2 as we are including this element in the subsequence ; Possible answer for current state ; Maximum answer for current state ; Memoizing the answer for the corresponding state ; Return the answer for current state ; Function that generate KMP Array ; Previous longest prefix suffix ; KMPArray [ 0 ] is a always 0 ; The loop calculates KMPArray [ i ] for i = 1 to M - 1 ; If current character is same ; Update the KMP array ; Otherwise ; Update the KMP array ; Function to print the maximum possible length ; Length of the given sequence ; Initialize kmp array ; Generate KMP array ; Initialize dp ; Initialize the states to - 1 ; Get answer ; Print answer ; Driver code ; Given array ; Give arr1 ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] [ ] dp ; static int [ ] KMPArray ; static int m ; private static int findSubsequence ( int [ ] a , int [ ] sequence , int i , int prev , int k ) { if ( k == m ) return Integer . MIN_VALUE ; if ( i == a . length ) return 0 ; if ( prev != - 1 && dp [ i ] [ prev ] [ k ] != - 1 ) { return dp [ i ] [ prev ] [ k ] ; } int include = 0 ; if ( prev == - 1 a [ i ] >= a [ prev ] ) { int k2 = k ; while ( k2 > 0 && a [ i ] != sequence [ k2 ] ) k2 = KMPArray [ k2 - 1 ] ; if ( a [ i ] == sequence [ k2 ] ) k2 ++ ; include = 1 + findSubsequence ( a , sequence , i + 1 , i , k2 ) ; } int ans = Math . max ( include , findSubsequence ( a , sequence , i + 1 , prev , k ) ) ; if ( prev != - 1 ) { dp [ i ] [ prev ] [ k ] = ans ; } return ans ; } private static void fillKMPArray ( int [ ] pattern ) { int j = 0 ; int i = 1 ; KMPArray [ 0 ] = 0 ; while ( i < m ) { if ( pattern [ i ] == pattern [ j ] ) { j ++ ; KMPArray [ i ] = j ; i ++ ; } else { if ( j != 0 ) j = KMPArray [ j - 1 ] ; else { KMPArray [ i ] = j ; i ++ ; } } } } static void printAnswer ( int a [ ] , int sequence [ ] ) { m = sequence . length ; KMPArray = new int [ m ] ; fillKMPArray ( sequence ) ; dp = new int [ a . length ] [ a . length ] [ a . length ] ; for ( int i = 0 ; i < a . length ; i ++ ) for ( int j = 0 ; j < a . length ; j ++ ) Arrays . fill ( dp [ i ] [ j ] , - 1 ) ; int ans = findSubsequence ( a , sequence , 0 , - 1 , 0 ) ; System . out . println ( ( ans < 0 ) ? 0 : ans ) ; } public static void main ( String [ ] args ) throws Exception { int [ ] arr = { 5 , 3 , 9 , 3 , 4 , 7 } ; int [ ] arr1 = { 3 , 4 } ; printAnswer ( arr , arr1 ) ; } }