codeparrot/xlcost-text-to-code huggingface dataset:
Count the numbers with N digits and whose suffix is divisible by K | Java implementation to Count the numbers with N digits and whose suffix is divisible by K ; Suffix of length pos with remainder rem and Z representing whether the suffix has a non zero digit until now ; Base case ; If count of digits is less than n ; Placing all possible digits in remaining positions ; If remainder non zero and suffix has n digits ; If the subproblem is already solved ; Placing all digits at MSB of suffix and increasing it 's length by 1 ; Non zero digit is placed ; Store and return the solution to this subproblem ; Function to Count the numbers with N digits and whose suffix is divisible by K ; Since we need powers of 10 for counting , it 's better to  pre store them along with their  modulo with 1e9 + 7 for counting ; Since at each recursive step we increase the suffix length by 1 by placing digits at its leftmost position , we need powers of 10 modded with k , in order to fpos the new remainder efficiently ; Initialising dp table values - 1 represents subproblem hasn 't  been solved yet ; Driver Code
import java . util . * ; import java . util . Arrays ; class GFG { static int mod = 1000000007 ; static int dp [ ] [ ] [ ] = new int [ 1005 ] [ 105 ] [ 2 ] ; static int powers [ ] = new int [ 1005 ] ; static int powersModk [ ] = new int [ 1005 ] ; static int calculate ( int pos , int rem , int z , int k , int n ) { if ( rem == 0 && z != 0 ) { if ( pos != n ) return ( powers [ n - pos - 1 ] * 9 ) % mod ; else return 1 ; } if ( pos == n ) return 0 ; if ( dp [ pos ] [ rem ] [ z ] != - 1 ) return dp [ pos ] [ rem ] [ z ] ; int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i == 0 ) count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , z , k , n ) ) ) % mod ; else count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , 1 , k , n ) ) ) % mod ; } return dp [ pos ] [ rem ] [ z ] = count ; } static int countNumbers ( int n , int k ) { int st = 1 ; int i ; for ( i = 0 ; i <= n ; i ++ ) { powers [ i ] = st ; st *= 10 ; st %= mod ; } st = 1 ; for ( i = 0 ; i <= n ; i ++ ) { powersModk [ i ] = st ; st *= 10 ; st %= mod ; } for ( int [ ] [ ] row : dp ) { for ( int [ ] innerRow : row ) { Arrays . fill ( innerRow , - 1 ) ; } } ; return calculate ( 0 , 0 , 0 , k , n ) ; } public static void main ( String [ ] args ) { int N = 2 ; int K = 2 ; System . out . print ( countNumbers ( N , K ) ) ; } }
Shortest path with exactly k edges in a directed and weighted graph | Set 2 | Java implementation of the above approach ; Function to find the smallest path with exactly K edges ; Array to store dp ; Initialising the array ; Loop to solve DP ; Initialising next state ; Recurrence relation ; Returning final answer ; Driver code ; Input edges ; Source and Destination ; Number of edges in path ; Calling the function
import java . util . ArrayList ; import java . util . Arrays ; class GFG { static class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } } static int inf = 100000000 ; static int smPath ( int s , int d , ArrayList < Pair < Pair < Integer , Integer > , Integer > > ed , int n , int k ) { int [ ] dis = new int [ n + 1 ] ; Arrays . fill ( dis , inf ) ; dis [ s ] = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int [ ] dis1 = new int [ n + 1 ] ; Arrays . fill ( dis1 , inf ) ; for ( Pair < Pair < Integer , Integer > , Integer > it : ed ) dis1 [ it . first . second ] = Math . min ( dis1 [ it . first . second ] , dis [ it . first . first ] + it . second ) ; for ( int j = 0 ; j <= n ; j ++ ) dis [ j ] = dis1 [ j ] ; } if ( dis [ d ] == inf ) return - 1 ; else return dis [ d ] ; } public static void main ( String [ ] args ) { int n = 4 ; ArrayList < Pair < Pair < Integer , Integer > , Integer > > ed = new ArrayList < > ( Arrays . asList ( new Pair < Pair < Integer , Integer > , Integer > ( new Pair < Integer , Integer > ( 0 , 1 ) , 10 ) , new Pair < Pair < Integer , Integer > , Integer > ( new Pair < Integer , Integer > ( 0 , 2 ) , 3 ) , new Pair < Pair < Integer , Integer > , Integer > ( new Pair < Integer , Integer > ( 0 , 3 ) , 2 ) , new Pair < Pair < Integer , Integer > , Integer > ( new Pair < Integer , Integer > ( 1 , 3 ) , 7 ) , new Pair < Pair < Integer , Integer > , Integer > ( new Pair < Integer , Integer > ( 2 , 3 ) , 7 ) ) ) ; int s = 0 , d = 3 ; int k = 2 ; System . out . println ( smPath ( s , d , ed , n , k ) ) ; } }
Path with smallest product of edges with weight > 0 | Java implementation of the approach ; Function to return the smallest product of edges ; If the source is equal to the destination ; Array to store distances ; Initialising the array ; Bellman ford algorithm ; Loop to detect cycle ; Returning final answer ; Driver code ; Input edges ; Source and Destination ; Bellman ford
import java . util . ArrayList ; import java . util . Arrays ; import java . util . PriorityQueue ; class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } } class GFG { static final float inf = Float . POSITIVE_INFINITY ; static float bellman ( int s , int d , ArrayList < Pair < Pair < Integer , Integer > , Float > > ed , int n ) { if ( s == d ) return 0 ; float [ ] dis = new float [ n + 1 ] ; Arrays . fill ( dis , inf ) ; dis [ s ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( Pair < Pair < Integer , Integer > , Float > it : ed ) dis [ it . first . second ] = Math . min ( dis [ it . first . second ] , dis [ it . first . first ] * it . second ) ; for ( Pair < Pair < Integer , Integer > , Float > it : ed ) { if ( dis [ it . first . second ] > dis [ it . first . first ] * it . second ) return - 2 ; } if ( dis [ d ] == inf ) return - 1 ; else return dis [ d ] ; } public static void main ( String [ ] args ) { int n = 3 ; ArrayList < Pair < Pair < Integer , Integer > , Float > > ed = new ArrayList < > ( Arrays . asList ( new Pair < Pair < Integer , Integer > , Float > ( new Pair < Integer , Integer > ( 1 , 2 ) , 0.5f ) , new Pair < Pair < Integer , Integer > , Float > ( new Pair < Integer , Integer > ( 1 , 3 ) , 1.9f ) , new Pair < Pair < Integer , Integer > , Float > ( new Pair < Integer , Integer > ( 2 , 3 ) , 3f ) ) ) ; int s = 1 , d = 3 ; float get = bellman ( s , d , ed , n ) ; if ( get == - 2 ) System . out . println ( " Cycle ▁ Detected " ) ; else System . out . println ( get ) ; } }
Find Maximum Length Of A Square Submatrix Having Sum Of Elements At | Java implementation of the above approach ; Function to return maximum length of square submatrix having sum of elements at - most K ; Matrix to store prefix sum ; Current maximum length ; Variable for storing maximum length of square ; Calculating prefix sum ; Checking whether there exits square with length cur_max + 1 or not ; Returning the maximum length ; Driver code
import java . util . * ; class GFG { public static int maxLengthSquare ( int row , int column , int [ ] [ ] arr , int k ) { int sum [ ] [ ] = new int [ row + 1 ] [ column + 1 ] ; int cur_max = 1 ; int max = 0 ; for ( int i = 1 ; i <= row ; i ++ ) { for ( int j = 1 ; j <= column ; j ++ ) { sum [ i ] [ j ] = sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] + arr [ i - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; if ( i >= cur_max && j >= cur_max && sum [ i ] [ j ] - sum [ i - cur_max ] [ j ] - sum [ i ] [ j - cur_max ] + sum [ i - cur_max ] [ j - cur_max ] <= k ) { max = cur_max ++ ; } } } return max ; } public static void main ( String args [ ] ) { int row = 4 , column = 4 ; int matrix [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } } ; int k = 6 ; int ans = maxLengthSquare ( row , column , matrix , k ) ; System . out . println ( ans ) ; } }
Sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times | Java program to find sum of all numbers formed having 4 atmost X times , 5 atmost Y times and 6 atmost Z times ; exactsum [ i ] [ j ] [ k ] stores the sum of all the numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; exactnum [ i ] [ j ] [ k ] stores numbers of numbers having exact i 4 ' s , ▁ j ▁ 5' s and k 6 's ; Utility function to calculate the sum for x 4 ' s , ▁ y ▁ 5' s and z 6 's ; Computing exactsum [ i ] [ j ] [ k ] as explained above ; Driver code
class GFG { static int N = 101 ; static int mod = ( int ) 1e9 + 7 ; static int exactsum [ ] [ ] [ ] = new int [ N ] [ N ] [ N ] ; static int exactnum [ ] [ ] [ ] = new int [ N ] [ N ] [ N ] ; static int getSum ( int x , int y , int z ) { int ans = 0 ; exactnum [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i <= x ; ++ i ) { for ( int j = 0 ; j <= y ; ++ j ) { for ( int k = 0 ; k <= z ; ++ k ) { if ( i > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i - 1 ] [ j ] [ k ] * 10 + 4 * exactnum [ i - 1 ] [ j ] [ k ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i - 1 ] [ j ] [ k ] % mod ; } if ( j > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j - 1 ] [ k ] * 10 + 5 * exactnum [ i ] [ j - 1 ] [ k ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j - 1 ] [ k ] % mod ; } if ( k > 0 ) { exactsum [ i ] [ j ] [ k ] += ( exactsum [ i ] [ j ] [ k - 1 ] * 10 + 6 * exactnum [ i ] [ j ] [ k - 1 ] ) % mod ; exactnum [ i ] [ j ] [ k ] += exactnum [ i ] [ j ] [ k - 1 ] % mod ; } ans += exactsum [ i ] [ j ] [ k ] % mod ; ans %= mod ; } } } return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 1 , z = 1 ; System . out . println ( getSum ( x , y , z ) % mod ) ; } }
Maximum value obtained by performing given operations in an Array | Java implementation of the above approach ; A function to calculate the maximum value ; basecases ; Loop to iterate and add the max value in the dp array ; Driver Code
class GFG { static void findMax ( int [ ] a , int n ) { int dp [ ] [ ] = new int [ n ] [ 2 ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = 0 ; dp [ 0 ] [ 0 ] = a [ 0 ] + a [ 1 ] ; dp [ 0 ] [ 1 ] = a [ 0 ] * a [ 1 ] ; for ( i = 1 ; i <= n - 2 ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i + 1 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] - a [ i ] + a [ i ] * a [ i + 1 ] ; } System . out . println ( Math . max ( dp [ n - 2 ] [ 0 ] , dp [ n - 2 ] [ 1 ] ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 1 , - 5 , - 3 , 2 , 9 , - 4 } ; findMax ( arr , 7 ) ; } }
Optimal strategy for a Game with modifications | Java implementation of the above approach ; Function to return sum of subarray from l to r ; calculate sum by a loop from l to r ; dp to store the values of sub problems ; if length of the array is less than k return the sum ; if the value is previously calculated ; else calculate the value ; select all the sub array of length len_r ; get the sum of that sub array ; check if it is the maximum or not ; store it in the table ; Driver code
class GFG { static int sum ( int arr [ ] , int l , int r ) { int s = 0 ; for ( int i = l ; i <= r ; i ++ ) { s += arr [ i ] ; } return s ; } static int dp [ ] [ ] [ ] = new int [ 101 ] [ 101 ] [ 101 ] ; static int solve ( int arr [ ] , int l , int r , int k ) { if ( r - l + 1 <= k ) return sum ( arr , l , r ) ; if ( dp [ l ] [ r ] [ k ] != 0 ) return dp [ l ] [ r ] [ k ] ; int sum_ = sum ( arr , l , r ) ; int len_r = ( r - l + 1 ) - k ; int len = ( r - l + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len - len_r + 1 ; i ++ ) { int sum_sub = sum ( arr , i + l , i + l + len_r - 1 ) ; ans = Math . max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; } dp [ l ] [ r ] [ k ] = ans ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 15 , 20 , 9 , 2 , 5 } , k = 2 ; int n = arr . length ; System . out . println ( solve ( arr , 0 , n - 1 , k ) ) ; } }
Find the minimum difference path from ( 0 , 0 ) to ( N | Java implementation of the approach ; Function to return the minimum difference path from ( 0 , 0 ) to ( N - 1 , M - 1 ) ; Terminating case ; Base case ; If it is already visited ; Recursive calls ; Return the value ; Driver code ; Function call
class GFG { final static int MAXI = 50 ; static int dp [ ] [ ] [ ] = new int [ MAXI ] [ MAXI ] [ MAXI * MAXI ] ; static int n , m ; final static int INT_MAX = Integer . MAX_VALUE ; static int minDifference ( int x , int y , int k , int b [ ] [ ] , int c [ ] [ ] ) { if ( x >= n y >= m ) return INT_MAX ; if ( x == n - 1 && y == m - 1 ) { int diff = b [ x ] [ y ] - c [ x ] [ y ] ; return Math . min ( Math . abs ( k - diff ) , Math . abs ( k + diff ) ) ; } int ans = dp [ x ] [ y ] [ k ] ; if ( ans != - 1 ) return ans ; ans = INT_MAX ; int diff = b [ x ] [ y ] - c [ x ] [ y ] ; ans = Math . min ( ans , minDifference ( x + 1 , y , Math . abs ( k + diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x , y + 1 , Math . abs ( k + diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x + 1 , y , Math . abs ( k - diff ) , b , c ) ) ; ans = Math . min ( ans , minDifference ( x , y + 1 , Math . abs ( k - diff ) , b , c ) ) ; return ans ; } public static void main ( String [ ] args ) { n = 2 ; m = 2 ; int b [ ] [ ] = { { 1 , 4 } , { 2 , 4 } } ; int c [ ] [ ] = { { 3 , 2 } , { 3 , 1 } } ; for ( int i = 0 ; i < MAXI ; i ++ ) { for ( int j = 0 ; j < MAXI ; j ++ ) { for ( int k = 0 ; k < MAXI * MAXI ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( minDifference ( 0 , 0 , 0 , b , c ) ) ; } }
Longest subsequence having difference atmost K | Java program for the above approach ; Function to find the longest Special Sequence ; Creating a list with all 0 's of size  equal to the length of String ; Supporting list with all 0 's of size 26 since  the given String consists  of only lower case alphabets ; Converting the ascii value to list indices ; Determining the lower bound ; Determining the upper bound ; Filling the dp array with values ; Filling the max_length array with max length of subsequence till now ; return the max length of subsequence ; Driver Code
class GFG { static int longest_subseq ( int n , int k , String s ) { int [ ] dp = new int [ n ] ; int [ ] max_length = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s . charAt ( i ) - ' a ' ; int lower = Math . max ( 0 , curr - k ) ; int upper = Math . min ( 25 , curr + k ) ; for ( int j = lower ; j < upper + 1 ; j ++ ) { dp [ i ] = Math . max ( dp [ i ] , max_length [ j ] + 1 ) ; } max_length [ curr ] = Math . max ( dp [ i ] , max_length [ curr ] ) ; } int ans = 0 ; for ( int i : dp ) ans = Math . max ( i , ans ) ; return ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; int k = 3 ; System . out . print ( longest_subseq ( n , k , s ) ) ; } }
Maximum possible array sum after performing the given operation | Java implementation of the approach ; Function to return the maximum possible sum after performing the given operation ; Dp vector to store the answer ; Base value ; Return the maximum sum ; Driver code
class GFG { static int max_sum ( int a [ ] , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ 2 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = - 999999 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { dp [ i + 1 ] [ 0 ] = Math . max ( dp [ i ] [ 0 ] + a [ i ] , dp [ i ] [ 1 ] - a [ i ] ) ; dp [ i + 1 ] [ 1 ] = Math . max ( dp [ i ] [ 0 ] - a [ i ] , dp [ i ] [ 1 ] + a [ i ] ) ; } return dp [ n ] [ 0 ] ; } public static void main ( String [ ] args ) { int a [ ] = { - 10 , 5 , - 4 } ; int n = a . length ; System . out . println ( max_sum ( a , n ) ) ; } }
Find the number of ways to reach Kth step in stair case | Java implementation of the approach ; Function to return the number of ways to reach the kth step ; Create the dp array ; Broken steps ; Calculate the number of ways for the rest of the positions ; If it is a blocked position ; Number of ways to get to the ith step ; Return the required answer ; Driver code
class GFG { static final int MOD = 1000000007 ; static int number_of_ways ( int arr [ ] , int n , int k ) { if ( k == 1 ) return 1 ; int dp [ ] = new int [ k + 1 ] ; int i ; for ( i = 0 ; i < k + 1 ; i ++ ) dp [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) dp [ arr [ i ] ] = 0 ; dp [ 0 ] = 1 ; dp [ 1 ] = ( dp [ 1 ] == - 1 ) ? 1 : dp [ 1 ] ; for ( i = 2 ; i <= k ; ++ i ) { if ( dp [ i ] == 0 ) continue ; dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; dp [ i ] %= MOD ; } return dp [ k ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 } ; int n = arr . length ; int k = 6 ; System . out . println ( number_of_ways ( arr , n , k ) ) ; } }
Minimum number of coins that can generate all the values in the given range | Java program to find minimum number of coins ; Function to find minimum number of coins ; Driver code
class GFG { static int findCount ( int n ) { return ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( findCount ( N ) ) ; } }
Calculate the number of set bits for every number from 0 to N | Java implementation of the approach ; Function to find the count of set bits in all the integers from 0 to n ; Driver code
class GFG { static void findSetBits ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) System . out . print ( Integer . bitCount ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; findSetBits ( n ) ; } }
Count number of ways to arrange first N numbers | Java implementation of the above approach ; Function to return the count of required arrangements ; Create a vector ; Store numbers from 1 to n ; To store the count of ways ; Generate all the permutations using next_permutation in STL ; Initialize flag to true if first element is 1 else false ; Checking if the current permutation satisfies the given conditions ; If the current permutation is invalid then set the flag to false ; If valid arrangement ; Generate the next permutation ;  ; Driver code
import java . util . * ; class GFG { static int countWays ( int n ) { Vector < Integer > a = new Vector < > ( ) ; int i = 1 ; while ( i <= n ) a . add ( i ++ ) ; int ways = 0 ; do { boolean flag = ( a . get ( 0 ) == 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { if ( Math . abs ( a . get ( j ) - a . get ( j - 1 ) ) > 2 ) flag = false ; } if ( flag ) ways ++ ; } while ( next_permutation ( a ) ) ; return ways ; } static boolean next_permutation ( Vector < Integer > p ) { for ( int a = p . size ( ) - 2 ; a >= 0 ; -- a ) if ( p . get ( a ) < p . get ( a + 1 ) ) for ( int b = p . size ( ) - 1 ; ; -- b ) if ( p . get ( b ) > p . get ( a ) ) { int t = p . get ( a ) ; p . set ( a , p . get ( b ) ) ; p . set ( b , t ) ; for ( ++ a , b = p . size ( ) - 1 ; a < b ; ++ a , -- b ) { t = p . get ( a ) ; p . set ( a , p . get ( b ) ) ; p . set ( b , t ) ; } return true ; } return false ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . print ( countWays ( n ) ) ; } }
Count number of ways to arrange first N numbers | Java implementation of the approach ; Function to return the count of required arrangements ; Create the dp array ; Initialize the base cases as explained above ; ( 12 ) as the only possibility ; Generate answer for greater values ; dp [ n ] contains the desired answer ; Driver code
class GFG { static int countWays ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; i ++ ) { dp [ i ] = dp [ i - 1 ] + dp [ i - 3 ] + 1 ; } return dp [ n ] ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( countWays ( n ) ) ; } }
Number of shortest paths to reach every cell from bottom | Java program to find number of shortest paths ; Function to find number of shortest paths ; Compute the grid starting from the bottom - left corner ; Print the grid ; Driver code ; Function call
class GFG { static void NumberOfShortestPaths ( int n , int m ) { int [ ] [ ] a = new int [ n ] [ m ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < m ; j ++ ) { if ( j == 0 i == n - 1 ) a [ i ] [ j ] = 1 ; else a [ i ] [ j ] = a [ i ] [ j - 1 ] + a [ i + 1 ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 2 ; NumberOfShortestPaths ( n , m ) ; } }
Maximum sum combination from two arrays | Java program to maximum sum combination from two arrays ; Function to maximum sum combination from two arrays ; To store dp value ; For loop to calculate the value of dp ; Return the required answer ; Driver code ; Function call
class GFG { static int Max_Sum ( int arr1 [ ] , int arr2 [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { dp [ i ] [ 0 ] = arr1 [ i ] ; dp [ i ] [ 1 ] = arr2 [ i ] ; continue ; } dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] + arr1 [ i ] ) ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] + arr2 [ i ] ) ; } return Math . max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 9 , 3 , 5 , 7 , 3 } ; int arr2 [ ] = { 5 , 8 , 1 , 4 , 5 } ; int n = arr1 . length ; System . out . println ( Max_Sum ( arr1 , arr2 , n ) ) ; } }
Partition the array in K segments such that bitwise AND of individual segment sum is maximized | Java program to find maximum possible AND ; Function to check whether a k segment partition is possible such that bitwise AND is ' mask ' ; dp [ i ] [ j ] stores whether it is possible to partition first i elements into j segments such that all j segments are ' good ' ; Initialising dp ; Filling dp in bottom - up manner ; Finding a cut such that first l elements can be partitioned into j - 1 ' good ' segments and arr [ l + 1 ] + ... + arr [ i ] is a ' good ' segment ; Function to find maximum possible AND ; Array to store prefix sums ; Maximum no of bits in the possible answer ; This will store the final answer ; Constructing answer greedily selecting from the higher most bit ; Checking if array can be partitioned such that the bitwise AND is ans | ( 1 << i ) ; if possible , update the answer ; Return the final answer ; Driver code ; n = 11 , first element is zero to make array 1 based indexing . So , number of elements are 10 ; Function call
class GFG { static boolean checkpossible ( int mask , int arr [ ] , int prefix [ ] , int n , int k ) { int i , j ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ k + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < k + 1 ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= k ; j ++ ) { for ( int l = i - 1 ; l >= 0 ; -- l ) { if ( dp [ l ] [ j - 1 ] && ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) { dp [ i ] [ j ] = true ; break ; } } } } return dp [ n ] [ k ] ; } static int Partition ( int arr [ ] , int n , int k ) { int prefix [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; } int LOGS = 20 ; int ans = 0 ; for ( int i = LOGS ; i >= 0 ; -- i ) { if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) { ans = ans | ( 1 << i ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 } , k = 2 ; int n = arr . length - 1 ; System . out . println ( Partition ( arr , n , k ) ) ; } }