codeparrot/xlcost-text-to-code huggingface dataset:
Minimum length of subsequence having unit GCD | Java program for the above approach ; Function that finds the prime factors of a number ; To store the prime factor ; 2 s that divide n ; N must be odd at this point Skip one element ; Update the prime factor ; If n is a prime number greater than 2 ; Function that finds the shortest subsequence ; Check if the prime factor of first number , is also the prime factor of the rest numbers in array ; Set corresponding bit of prime factor to 1 , it means both these numbers have the same prime factor ; If no states encountered so far continue for this combination of bits ; Update this state with minimum ways to reach this state ; Function that print the minimum length of subsequence ; Find the prime factors of the first number ; Initialize the array with maximum steps , size of the array + 1 for instance ; Total number of set bits is equal to the total number of prime factors ; Indicates there is one way to reach the number under consideration ; State 0 corresponds to gcd of 1 ; If not found such subsequence then print " - 1" ; Else print the length ; Driver Code ; Given array arr [ ] ; Function Call
import java . io . * ; import java . util . * ; class GFG { private static int [ ] findPrimeFactors ( int n ) { int [ ] primeFactors = new int [ 9 ] ; int j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; return Arrays . copyOfRange ( primeFactors , 0 , j ) ; } private static void findShortestSubsequence ( int [ ] dp , int [ ] a , int index , int [ ] primeFactors ) { int n = a . length ; for ( int j = index ; j < n ; j ++ ) { int bitmask = 0 ; for ( int p = 0 ; p < primeFactors . length ; p ++ ) { if ( a [ j ] % primeFactors [ p ] == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( int i = 0 ; i < dp . length ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = Math . min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } private static void printMinimumLength ( int [ ] a ) { int min = a . length + 1 ; for ( int i = 0 ; i < a . length - 1 ; i ++ ) { int [ ] primeFactors = findPrimeFactors ( a [ i ] ) ; int n = primeFactors . length ; int [ ] dp = new int [ 1 << n ] ; Arrays . fill ( dp , a . length + 1 ) ; int setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; min = Math . min ( dp [ 0 ] , min ) ; } if ( min == a . length + 1 ) System . out . println ( - 1 ) ; else System . out . println ( min ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 12 , 3 } ; printMinimumLength ( arr ) ; } }
Maximum sum of Bitwise XOR of all elements of two equal length subsets | Java program for the above approach ; Function that finds the maximum Bitwise XOR sum of the two subset ; Check if the current state is already computed ; Initialize answer to minimum value ; Iterate through all possible pairs ; Check whether ith bit and jth bit of mask is not set then pick the pair ; For all possible pairs find maximum value pick current a [ i ] , a [ j ] and set i , j th bits in mask ; Store the maximum value and return the answer ; Driver Code ; Given array arr [ ] ; Declare Initialize the dp states ; Function Call
import java . util . * ; import java . io . * ; public class GFG { public static int xorSum ( int a [ ] , int n , int mask , int [ ] dp ) { if ( dp [ mask ] != - 1 ) { return dp [ mask ] ; } int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = Math . max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } public static void main ( String args [ ] ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; int dp [ ] = new int [ ( 1 << n ) + 5 ] ; Arrays . fill ( dp , - 1 ) ; System . out . println ( xorSum ( arr , n , 0 , dp ) ) ; } }
Count of ways in which N can be represented as sum of Fibonacci numbers without repetition | Java program for the above approach ; Function to generate the fibonacci number ; First two number of fibonacci sqequence ; Function to find maximum ways to represent num as the sum of fibonacci number ; Generate the Canonical form of given number ; Reverse the number ; Base condition of dp1 and dp2 ; Iterate from 1 to cnt ; Calculate dp1 [ ] ; Calculate dp2 [ ] ; Return final ans ; Driver code ; Function call to generate the fibonacci numbers ; Given number ; Function call
import java . util . * ; class GFG { static long [ ] fib = new long [ 101 ] ; static long [ ] dp1 = new long [ 101 ] ; static long [ ] dp2 = new long [ 101 ] ; static long [ ] v = new long [ 101 ] ; static void fibonacci ( ) { fib [ 1 ] = 1 ; fib [ 2 ] = 2 ; for ( int i = 3 ; i <= 87 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static long find ( int num ) { int cnt = 0 ; for ( int i = 87 ; i > 0 ; i -- ) { if ( num >= fib [ i ] ) { v [ cnt ++ ] = i ; num -= fib [ i ] ; } } for ( int i = 0 ; i < cnt / 2 ; i ++ ) { long t = v [ i ] ; v [ i ] = v [ cnt - i - 1 ] ; v [ cnt - i - 1 ] = t ; } dp1 [ 0 ] = 1 ; dp2 [ 0 ] = ( v [ 0 ] - 1 ) / 2 ; for ( int i = 1 ; i < cnt ; i ++ ) { dp1 [ i ] = dp1 [ i - 1 ] + dp2 [ i - 1 ] ; dp2 [ i ] = ( ( v [ i ] - v [ i - 1 ] ) / 2 ) * dp2 [ i - 1 ] + ( ( v [ i ] - v [ i - 1 ] - 1 ) / 2 ) * dp1 [ i - 1 ] ; } return ( dp1 [ cnt - 1 ] + dp2 [ cnt - 1 ] ) ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int num = 13 ; System . out . print ( find ( num ) ) ; } }
Count of N | Java program for the above approach ; Function to find count of N - digit numbers with single digit XOR ; Range of numbers ; Calculate XOR of digits ; If XOR <= 9 , then increment count ; Print the count ; Driver Code ; Given Number ; Function Call
class GFG { public static void countNums ( int N ) { int l = ( int ) Math . pow ( 10 , N - 1 ) , r = ( int ) Math . pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int xor = 0 , temp = i ; while ( temp > 0 ) { xor = xor ^ ( temp % 10 ) ; temp /= 10 ; } if ( xor <= 9 ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 2 ; countNums ( N ) ; } }
Maximize length of Non | Java program to implement the above approach ; Function to find the maximum length non decreasing subarray by reversing at most one subarray ; dp [ i ] [ j ] be the longest subsequence of a [ 0. . . i ] with first j parts ; Maximum length sub - sequence of ( 0. . ) ; Maximum length sub - sequence of ( 0. . 1. . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . . ) ; Maximum length sub - sequence of ( 0. . 1. .0 . .1 . . ) ; Find the max length subsequence ; Print the answer ; Driver code
import java . util . * ; class GFG { static void main_fun ( int arr [ ] , int n ) { int [ ] [ ] dp = new int [ 4 ] [ n ] ; if ( arr [ 0 ] == 0 ) dp [ 0 ] [ 0 ] = 1 ; else dp [ 1 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; else dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) dp [ 1 ] [ i ] = Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ; else dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { dp [ 2 ] [ i ] = Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ; } else dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { dp [ 3 ] [ i ] = Math . max ( dp [ 3 ] [ i - 1 ] + 1 , Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ) ; } else dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ] ; } int ans = Math . max ( dp [ 2 ] [ n - 1 ] , Math . max ( dp [ 1 ] [ n - 1 ] , Math . max ( dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ) ) ) ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 0 , 1 , 0 , 1 } ; main_fun ( arr , n ) ; } }
Print the Maximum Subarray Sum | Java program for the above approach ; Function to print the elements of Subarray with maximum sum ; Initialize currMax and globalMax with first value of nums ; Iterate for all the elemensts of the array ; Update currMax ; Check if currMax is greater than globalMax ; Traverse in left direction to find start Index of subarray ; Decrement the start index ; Printing the elements of subarray with max sum ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; class GFG { static void SubarrayWithMaxSum ( Vector < Integer > nums ) { int endIndex = 0 , currMax = nums . get ( 0 ) ; int globalMax = nums . get ( 0 ) ; for ( int i = 1 ; i < nums . size ( ) ; ++ i ) { currMax = Math . max ( nums . get ( i ) , nums . get ( i ) + currMax ) ; if ( currMax > globalMax ) { globalMax = currMax ; endIndex = i ; } } int startIndex = endIndex ; while ( startIndex >= 0 ) { globalMax -= nums . get ( startIndex ) ; if ( globalMax == 0 ) break ; startIndex -- ; } for ( int i = startIndex ; i <= endIndex ; ++ i ) { System . out . print ( nums . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( - 2 ) ; arr . add ( - 5 ) ; arr . add ( 6 ) ; arr . add ( - 2 ) ; arr . add ( - 3 ) ; arr . add ( 1 ) ; arr . add ( 5 ) ; arr . add ( - 6 ) ; SubarrayWithMaxSum ( arr ) ; } }
Count of distinct Primonacci Numbers in a given range [ L , R ] | Java Program to implement the above approach ; Stores list of all primes ; Function to find all primes ; To mark the prime ones ; Initially all indices as prime ; If i is prime ; Set all multiples of i as non - prime ; Adding all primes to a list ; Function to return the count of Primonacci Numbers in the range [ l , r ] ; dp [ i ] contains ith Primonacci Number ; Stores the Primonacci Numbers ; Iterate over all smaller primes ; If Primonacci number lies within the range [ L , R ] ; Count of Primonacci Numbers ; Driver Code
import java . util . * ; class GFG { static ArrayList < Integer > primes ; static int M = 100005 ; static void sieve ( ) { primes = new ArrayList < > ( ) ; boolean mark [ ] = new boolean [ M ] ; for ( int i = 2 ; i < M ; i ++ ) mark [ i ] = true ; for ( int i = 2 ; i * i < M ; i ++ ) { if ( mark [ i ] ) { for ( int j = i * i ; j < M ; j += i ) mark [ j ] = false ; } } for ( int i = 2 ; i < M ; i ++ ) if ( mark [ i ] ) primes . add ( i ) ; } static void countPrimonacci ( int l , int r ) { ArrayList < Integer > dp = new ArrayList < > ( ) ; dp . add ( 1 ) ; dp . add ( 1 ) ; int i = 2 ; HashSet < Integer > s = new HashSet < > ( ) ; while ( true ) { int x = 0 ; for ( int j = 0 ; j < primes . size ( ) ; j ++ ) { int p = primes . get ( j ) ; if ( p >= i ) break ; x += dp . get ( i - p ) ; } if ( x >= l && x <= r ) s . add ( x ) ; if ( x > r ) break ; dp . add ( x ) ; i ++ ; } System . out . println ( s . size ( ) ) ; } public static void main ( String [ ] args ) { sieve ( ) ; int L = 1 , R = 10 ; countPrimonacci ( L , R ) ; } }
Queries to count distinct Binary Strings of all lengths from N to M satisfying given properties | Java program to implement the above approach ; Function to calculate the count of possible Strings ; Initialize dp [ 0 ] ; dp [ i ] represents count of Strings of length i ; Add dp [ i - k ] if i >= k ; Update Prefix Sum Array ; Driver Code
import java . util . * ; class GFG { static int N = ( int ) ( 1e5 + 5 ) ; static int MOD = 1000000007 ; static int [ ] dp = new int [ N ] ; static void countStrings ( int K , int [ ] [ ] Q ) { dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( i >= K ) dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD ; } for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD ; } for ( int i = 0 ; i < Q . length ; i ++ ) { int ans = dp [ Q [ i ] [ 1 ] ] - dp [ Q [ i ] [ 0 ] - 1 ] ; if ( ans < 0 ) ans = ans + MOD ; System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { int K = 3 ; int [ ] [ ] Q = { { 1 , 4 } , { 3 , 7 } } ; countStrings ( K , Q ) ; } }
Minimum possible sum of prices of a Triplet from the given Array | Java Program to implement the above approach ; Function to minimize the sum of price by taking a triplet ; Initialize a dp [ ] array ; Stores the final result ; Iterate for all values till N ; Check if num [ j ] > num [ i ] ; Update dp [ j ] if it is greater than stored value ; Update the minimum sum as ans ; If there is no minimum sum exist then print - 1 else print the ans ; Driver Code
import java . util . * ; import java . io . * ; public class Main { public static long minSum ( int n , int num [ ] , int price [ ] ) { long dp [ ] = new long [ n ] ; Arrays . fill ( dp , Integer . MAX_VALUE ) ; long ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = ( long ) Math . min ( ( long ) dp [ j ] , ( long ) price [ i ] + ( long ) price [ j ] ) ; ans = Math . min ( ans , ( long ) dp [ i ] + ( long ) price [ j ] ) ; } } } return ans != Integer . MAX_VALUE ? ans : - 1 ; } public static void main ( String [ ] args ) { int num [ ] = { 2 , 4 , 6 , 7 , 8 } ; int price [ ] = { 10 , 20 , 100 , 20 , 40 } ; int n = price . length ; System . out . println ( minSum ( n , num , price ) ) ; } }
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s and count of 1 ' s ▁ â ‰ ¥ ▁ count ▁ of ▁ 0' s in each prefix substring | Java program to implement the above approach ; Function to calculate and returns the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the count of all binary strings having equal count of 0 ' s ▁ and ▁ 1' s and each prefix substring having frequency of 1 ' s ▁ > = ▁ frequencies ▁ of ▁ 0' s ; If N is odd ; No such strings possible ; Otherwise ; Calculate value of 2 nCn ; Return 2 nCn / ( n + 1 ) ; Driver code
import java . util . * ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long countStrings ( int N ) { if ( N % 2 == 1 ) return 0 ; else { N /= 2 ; long c = binomialCoeff ( 2 * N , N ) ; return c / ( N + 1 ) ; } } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( countStrings ( N ) + " ▁ " ) ; } }
Number of ways to cut a stick of length N into in even length at most K units long pieces | Java program for the above approach ; Recursive Function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver code
import java . util . * ; class GFG { static int solve ( int n , int k , int mod , int dp [ ] ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int cnt = 0 ; for ( int i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } public static void main ( String [ ] args ) { int mod = ( int ) ( 1e9 + 7 ) ; int n = 4 , k = 2 ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; int ans = solve ( n , k , mod , dp ) ; System . out . println ( ans ) ; } }
Number of ways to select exactly K even numbers from given Array | Java program for the above approach ; Function for calculating factorial ; Factorial of n defined as : n ! = n * ( n - 1 ) * ... * 1 ; Function to find the number of ways to select exactly K even numbers from the given array ; Count even numbers ; Check if the current number is even ; Check if the even numbers to be chosen is greater than n . Then , there is no way to pick it . ; The number of ways will be nCk ; Driver Code ; Given array arr [ ] ; Given count of even elements ; Function call
class GFG { static int [ ] f = new int [ 12 ] ; static void fact ( ) { f [ 0 ] = f [ 1 ] = 1 ; for ( int i = 2 ; i <= 10 ; i ++ ) f [ i ] = i * 1 * f [ i - 1 ] ; } static void solve ( int arr [ ] , int n , int k ) { fact ( ) ; int even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( k > even ) System . out . print ( 0 + "NEW_LINE"); else { System . out . print ( f [ even ] / ( f [ k ] * f [ even - k ] ) ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 1 ; solve ( arr , n , k ) ; } }
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s | Java program for the above approach ; Function to calculate C ( n , r ) % MOD DP based approach ; Corner case ; Stores the last row of Pascal 's Triangle ; Initialize top row of pascal triangle ; Construct Pascal 's Triangle  from top to bottom ; Fill current row with the help of previous row ; C ( n , j ) = C ( n - 1 , j ) + C ( n - 1 , j - 1 ) ; Driver Code
import java . util . * ; class GFG { final static int MOD = 1000000007 ; static int nCrModp ( int n , int r ) { if ( n % 2 == 1 ) { return - 1 ; } int C [ ] = new int [ r + 1 ] ; Arrays . fill ( C , 0 ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD ; } return C [ r ] ; } public static void main ( String s [ ] ) { int N = 6 ; System . out . println ( nCrModp ( N , N / 2 ) ) ; } }
Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | Java program for the above approach ; Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Driver code ; Given number X ; Function call
import java . util . * ; class GFG { static void printMinOperations ( int N ) { int dp [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( int i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } System . out . println ( dp [ N ] ) ; Vector < Integer > seq = new Vector < Integer > ( ) ; while ( N > 1 ) { seq . add ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . add ( 1 ) ; for ( int i = seq . size ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( seq . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int X = 96234 ; printMinOperations ( X ) ; } }
Largest subsequence such that all indices and all values are multiples individually | Java program for the above approach ; Function that print maximum length of array ; dp [ ] array to store the maximum length ; Find all divisors of i ; If the current value is greater than the divisor 's value ; If current value is greater than the divisor 's value  and s is not equal  to current index ; Condition if current value is greater than the divisor 's value ; Computing the greatest value ; Printing maximum length of array ; Driver Code ; Given array arr [ ] ; Function call
import java . util . * ; import java . io . * ; class GFG { static void maxLength ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 1 ; j <= Math . sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { int s = i / j ; if ( s == j ) { if ( arr [ i ] > arr [ s ] ) { dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; } } else { if ( s != i && arr [ i ] > arr [ s ] ) dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; if ( arr [ i ] > arr [ j ] ) { dp [ j ] = Math . max ( dp [ i ] + 1 , dp [ j ] ) ; } } } } } int max = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( dp [ i ] > max ) max = dp [ i ] ; } System . out . println ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 } ; int size = arr . length ; maxLength ( arr , size ) ; } }
Maximum profit by buying and selling a stock at most twice | Set 2 | Java implmenetation to find maximum possible profit with at most two transactions ; Function to find the maximum profit with two transactions on a given list of stock prices ; buy1 - Money lent to buy 1 stock profit1 - Profit after selling the 1 st stock buyed . buy2 - Money lent to buy 2 stocks including profit of selling 1 st stock profit2 - Profit after selling 2 stocks ; Set initial buying values to Integer . MAX_VALUE as we want to minimize it ; Set initial selling values to zero as we want to maximize it ; Money lent to buy the stock should be minimum as possible . buy1 tracks the minimum possible stock to buy from 0 to i - 1. ; Profit after selling a stock should be maximum as possible . profit1 tracks maximum possible profit we can make from 0 to i - 1. ; Now for buying the 2 nd stock , we will integrate profit made from selling the 1 st stock ; Profit after selling a 2 stocks should be maximum as possible . profit2 tracks maximum possible profit we can make from 0 to i - 1. ; Driver Code
import java . util . * ; class GFG { static int maxProfit ( int price [ ] , int n ) { int buy1 , profit1 , buy2 , profit2 ; buy1 = buy2 = Integer . MAX_VALUE ; profit1 = profit2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { buy1 = Math . min ( buy1 , price [ i ] ) ; profit1 = Math . max ( profit1 , price [ i ] - buy1 ) ; buy2 = Math . min ( buy2 , price [ i ] - profit1 ) ; profit2 = Math . max ( profit2 , price [ i ] - buy2 ) ; } return profit2 ; } public static void main ( String [ ] args ) { int price [ ] = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int n = price . length ; System . out . print ( " Maximum ▁ Profit ▁ = ▁ " + maxProfit ( price , n ) ) ; } }
Maximum score of deleting an element from an Array based on given condition | Java implementation to find the maximum score of the deleting a element from an array ; Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function call
import java . util . * ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { @ SuppressWarnings ( " unchecked " ) HashMap < Integer , Integer > freq = new HashMap ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( a [ i ] ) ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . put ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ Arrays . stream ( a ) . max ( ) . getAsInt ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq . get ( 1 ) ; for ( int i = 2 ; i < dp . length ; i ++ ) dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + freq . get ( i ) * i ) ; return dp [ dp . length - 1 ] ; } public static void main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; System . out . print ( findMaximumScore ( a , n ) ) ; } }