codeparrot/xlcost-text-to-code huggingface dataset:
Minimum cost of reducing Array by merging any adjacent elements repetitively | Java program for the above approach ; Function to find the total minimum cost of merging two consecutive numbers ; Find the size of numbers [ ] ; If array is empty , return 0 ; To store the prefix Sum of numbers array numbers [ ] ; Traverse numbers [ ] to find the prefix sum ; dp table to memoised the value ; Iterate for length >= 1 ; Find sum in range [ i . . j ] ; Initialise dp [ i ] [ j ] to Integer . MAX_VALUE ; Iterate for all possible K to find the minimum cost ; Update the minimum sum ; Return the final minimum cost ; Driver Code ; Given set of numbers ; Function Call
import java . util . * ; class GFG { static int mergeTwoNumbers ( int [ ] numbers ) { int len , i , j , k ; int n = numbers . length ; if ( numbers . length == 0 ) { return 0 ; } int [ ] prefixSum = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + numbers [ i - 1 ] ; } int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( len = 2 ; len <= n ; len ++ ) { for ( i = 1 ; i <= n - len + 1 ; i ++ ) { j = i + len - 1 ; int sum = prefixSum [ j ] - prefixSum [ i - 1 ] ; dp [ i ] [ j ] = Integer . MAX_VALUE ; for ( k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i ] [ k ] + dp [ k + 1 ] [ j ] + sum ) ; } } } return dp [ 1 ] [ n ] ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 6 , 4 , 4 , 6 } ; System . out . print ( mergeTwoNumbers ( arr1 ) + "NEW_LINE"); } }
Maximum sum possible for every node by including it in a segment of N | Java program to calculate the maximum sum possible for every node by including it in a segment of the N - Ary Tree ; Stores the maximum sum possible for every node by including them in a segment with their successors ; Stores the maximum sum possible for every node by including them in a segment with their ancestors ; Store the maximum sum for every node by including it in a segment with its successors ; Update the maximum sums for each node by including them in a sequence with their ancestors ; Condition to check , if current node is not root ; Add edges ; Function to find the maximum answer for each node ; Compute the maximum sums with successors ; Store the computed maximums ; Update the maximum sums by including their ancestors ; Print the desired result ; Number of nodes ; Graph ; Add edges ; Weight of each node ; Compute the max sum of segments for each node ; Print the answer for every node
import java . util . * ; public class Main { static int [ ] dp1 = new int [ 100005 ] ; static int [ ] dp2 = new int [ 100005 ] ; static void dfs1 ( int u , int par , Vector < Vector < Integer > > g , int [ ] weight ) { dp1 [ u ] = weight [ u ] ; for ( int c = 0 ; c < g . get ( u ) . size ( ) ; c ++ ) { if ( g . get ( u ) . get ( c ) != par ) { dfs1 ( g . get ( u ) . get ( c ) , u , g , weight ) ; dp1 [ u ] += Math . max ( 0 , dp1 [ g . get ( u ) . get ( c ) ] ) ; } } } static void dfs2 ( int u , int par , Vector < Vector < Integer > > g , int [ ] weight ) { if ( par != 0 ) { int maxSumAncestors = dp2 [ par ] - Math . max ( 0 , dp1 [ u ] ) ; dp2 [ u ] = dp1 [ u ] + Math . max ( 0 , maxSumAncestors ) ; } for ( int c = 0 ; c < g . get ( u ) . size ( ) ; c ++ ) { if ( g . get ( u ) . get ( c ) != par ) { dfs2 ( g . get ( u ) . get ( c ) , u , g , weight ) ; } } } static void addEdge ( int u , int v , Vector < Vector < Integer > > g ) { g . get ( u ) . add ( v ) ; g . get ( v ) . add ( u ) ; } static void maxSumSegments ( Vector < Vector < Integer > > g , int [ ] weight , int n ) { dfs1 ( 1 , 0 , g , weight ) ; for ( int i = 1 ; i < n + 1 ; i ++ ) dp2 [ i ] = dp1 [ i ] ; dfs2 ( 1 , 0 , g , weight ) ; } static void printAns ( int n ) { for ( int i = 1 ; i < n ; i ++ ) System . out . print ( dp2 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 7 ; Vector < Vector < Integer > > g = new Vector < Vector < Integer > > ( ) ; for ( int i = 0 ; i < 100005 ; i ++ ) { g . add ( new Vector < Integer > ( ) ) ; } addEdge ( 1 , 2 , g ) ; addEdge ( 1 , 3 , g ) ; addEdge ( 2 , 4 , g ) ; addEdge ( 2 , 5 , g ) ; addEdge ( 3 , 6 , g ) ; addEdge ( 4 , 7 , g ) ; int [ ] weight = new int [ n + 1 ] ; weight [ 1 ] = - 8 ; weight [ 2 ] = 9 ; weight [ 3 ] = 7 ; weight [ 4 ] = - 4 ; weight [ 5 ] = 5 ; weight [ 6 ] = - 10 ; weight [ 7 ] = - 6 ; maxSumSegments ( g , weight , n ) ; printAns ( n ) ; } }
Count of pairs in an Array with same number of set bits | Java program to count possible number of pairs of elements with same number of set bits ; Function to return the count of Pairs ; Get the maximum element ; Array to store count of bits of all elements upto maxm ; Store the set bits for powers of 2 ; Compute the set bits for the remaining elements ; Store the frequency of respective counts of set bits ; Driver code
import java . util . * ; import java . lang . * ; class GFG { static int countPairs ( int [ ] arr , int N ) { int maxm = arr [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) { if ( maxm < arr [ j ] ) { maxm = arr [ j ] ; } } int i , k = 0 ; int [ ] bitscount = new int [ maxm + 1 ] ; Arrays . fill ( bitscount , 0 ) ; for ( i = 1 ; i <= maxm ; i *= 2 ) bitscount [ i ] = 1 ; for ( i = 1 ; i <= maxm ; i ++ ) { if ( bitscount [ i ] == 1 ) k = i ; if ( bitscount [ i ] == 0 ) { bitscount [ i ] = bitscount [ k ] + bitscount [ i - k ] ; } } Map < Integer , Integer > setbits = new HashMap < > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { setbits . put ( bitscount [ arr [ j ] ] , setbits . getOrDefault ( bitscount [ arr [ j ] ] , 0 ) + 1 ) ; } int ans = 0 ; for ( int it : setbits . values ( ) ) { ans += it * ( it - 1 ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int N = 12 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ; System . out . println ( countPairs ( arr , N ) ) ; } }
Count of Binary strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times | Java program to find the count of Binary Strings of length N having atmost M consecutive 1 s or 0 s alternatively exactly K times ; Array to contain the final result ; Function to get the number of desirable binary strings ; If we reach end of string and groups are exhausted , return 1 ; If length is exhausted but groups are still to be made , return 0 ; If length is not exhausted but groups are exhausted , return 0 ; If both are negative just return 0 ; If already calculated , return it ; Initialise answer for each state ; Loop through every possible m ; Driver code
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1000 ] [ 1000 ] ; static int solve ( int n , int k , int m ) { if ( n == 0 && k == 0 ) return 1 ; if ( n == 0 && k != 0 ) return 0 ; if ( n != 0 && k == 0 ) return 0 ; if ( n < 0 k < 0 ) return 0 ; if ( dp [ n ] [ k ] > 0 ) return dp [ n ] [ k ] ; int ans = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { ans += solve ( n - j , k - 1 , m ) ; } return dp [ n ] [ k ] = ans ; } public static void main ( String [ ] args ) { int N = 7 , K = 4 , M = 3 ; System . out . print ( solve ( N , K , M ) ) ; } }
Maximum neighbor element in a matrix within distance K | Java implementation to find the maximum neighbor element within the distance of less than K ; Function to print the matrix ; Loop to iterate over the matrix ; Utility function ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code
import java . util . * ; class GFG { static void printMatrix ( int [ ] [ ] A ) { for ( int i = 0 ; i < A . length ; i ++ ) { for ( int j = 0 ; j < A [ 0 ] . length ; j ++ ) System . out . print ( A [ i ] [ j ] + " ▁ " ) ; System . out . print ( 'NEW_LINE'); } } static void copyMatrix ( int [ ] [ ] A , int [ ] [ ] B ) { int row = B . length ; int col = B [ 0 ] . length ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { A [ i ] [ j ] = B [ i ] [ j ] ; } } } static int [ ] [ ] getMaxNeighbour ( int [ ] [ ] A , int K ) { int [ ] [ ] ans = new int [ A . length ] [ A [ 0 ] . length ] ; copyMatrix ( ans , A ) ; for ( int q = 1 ; q <= K ; q ++ ) { for ( int i = 0 ; i < A . length ; i ++ ) { for ( int j = 0 ; j < A [ 0 ] . length ; j ++ ) { int maxi = ans [ i ] [ j ] ; if ( i > 0 ) maxi = Math . max ( maxi , ans [ i - 1 ] [ j ] ) ; if ( j > 0 ) maxi = Math . max ( maxi , ans [ i ] [ j - 1 ] ) ; if ( i < A . length - 1 ) maxi = Math . max ( maxi , ans [ i + 1 ] [ j ] ) ; if ( j < A [ 0 ] . length - 1 ) maxi = Math . max ( maxi , ans [ i ] [ j + 1 ] ) ; A [ i ] [ j ] = maxi ; } } copyMatrix ( ans , A ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] B = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printMatrix ( getMaxNeighbour ( B , 2 ) ) ; } }
Number of distinct ways to represent a number as sum of K unique primes | Java program to count the number of distinct ways to represent a number as K different primes ; Prime vector ; Sieve array of prime ; DP array ; Initialise all numbers as prime ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Driver code ; Precompute primes by sieve
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; static boolean [ ] isprime = new boolean [ 1000 ] ; static int [ ] [ ] [ ] dp = new int [ 200 ] [ 20 ] [ 1000 ] ; static void sieve ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) isprime [ i ] = true ; for ( int i = 2 ; i * i < 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( int j = i * i ; j < 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( int i = 2 ; i < 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . add ( i ) ; } } } static int CountWays ( int i , int j , int sum , int n , int k ) { if ( i >= prime . size ( ) - 1 sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i ] [ j ] [ sum ] != 0 ) return dp [ i ] [ j ] [ sum ] ; int inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime . get ( i ) , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i ] [ j ] [ sum ] = inc + exc ; } public static void main ( String [ ] args ) { sieve ( ) ; int N = 100 , K = 5 ; System . out . println ( CountWays ( 0 , 0 , 0 , N , K ) ) ; } }
Count minimum factor jumps required to reach the end of an Array | Java program for bottom up approach ; Vector to store factors of each integer ; Initialize the dp array ; Precompute all the factors of every integer ; Function to count the minimum factor jump ; Initialise minimum jumps to reach each cell as Integer . MAX_VALUE ; 0 jumps required to reach the first cell ; Iterate over all cells ; Calculating for each jump ; If a cell is in bound ; Return minimum jumps to reach last cell ; Driver code ; Pre - calculating the factors ; Function call
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] factors = new Vector [ 100005 ] ; static int [ ] dp = new int [ 100005 ] ; static void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) factors [ j ] . add ( i ) ; } } static int solve ( int arr [ ] , int n ) { for ( int i = 0 ; i < 100005 ; i ++ ) { dp [ i ] = Integer . MAX_VALUE ; } dp [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j : factors [ arr [ i ] ] ) { if ( i + j < n ) dp [ i + j ] = Math . min ( dp [ i + j ] , 1 + dp [ i ] ) ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < factors . length ; i ++ ) factors [ i ] = new Vector < Integer > ( ) ; precompute ( ) ; int arr [ ] = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = arr . length ; System . out . print ( solve ( arr , n ) ) ; } }
Find the length of the Largest subset such that all elements are Pairwise Coprime | Java implementation to find the length of the largest subset such that all elements are Pairwise Coprime ; Dynamic programming table ; Function to obtain the mask for any integer ; List of prime numbers till 50 ; Iterate through all prime numbers to obtain the mask ; Set this prime 's bit ON in the mask ; Return the mask value ; Function to count the number of ways ; Check if subproblem has been solved ; Excluding current element in the subset ; Check if there are no common prime factors then only this element can be included ; Calculate the new mask if this element is included ; Store and return the answer ; Function to find the count of subarray with all digits unique ; Driver code
import java . util . * ; class GFG { static int dp [ ] [ ] = new int [ 5000 ] [ 1029 ] ; static int getmask ( int val ) { int mask = 0 ; int prime [ ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 } ; for ( int i = 0 ; i < 15 ; i ++ ) { if ( val % prime [ i ] == 0 ) { mask = mask | ( 1 << i ) ; } } return mask ; } static int calculate ( int pos , int mask , int a [ ] , int n ) { if ( pos == n || mask == ( int ) ( 1 << n - 1 ) ) return 0 ; if ( dp [ pos ] [ mask ] != - 1 ) return dp [ pos ] [ mask ] ; int size = 0 ; size = Math . max ( size , calculate ( pos + 1 , mask , a , n ) ) ; if ( ( getmask ( a [ pos ] ) & mask ) == 0 ) { int new_mask = ( mask | ( getmask ( a [ pos ] ) ) ) ; size = Math . max ( size , 1 + calculate ( pos + 1 , new_mask , a , n ) ) ; } return dp [ pos ] [ mask ] = size ; } static int largestSubset ( int a [ ] , int n ) { for ( int i = 0 ; i < 5000 ; i ++ ) Arrays . fill ( dp [ i ] , - 1 ) ; return calculate ( 0 , 0 , a , n ) ; } public static void main ( String args [ ] ) { int A [ ] = { 2 , 3 , 13 , 5 , 14 , 6 , 7 , 11 } ; int N = A . length ; System . out . println ( largestSubset ( A , N ) ) ; } }
Maximum sum such that exactly half of the elements are selected and no two adjacent | Java program to find maximum sum possible such that exactly Math . floor ( N / 2 ) elements are selected and no two selected elements are adjacent to each other ; Function return the maximum sum possible under given condition ; Base case ; When i is odd ; When i is even ; Maximum of if we pick last element or not ; Driver code
import java . util . * ; class GFG { static int MaximumSum ( int a [ ] , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ 2 ] ; dp [ 2 ] [ 1 ] = a [ 1 ] ; dp [ 2 ] [ 0 ] = a [ 0 ] ; for ( int i = 3 ; i < n + 1 ; i ++ ) { if ( i % 2 == 1 ) { int temp = Math . max ( ( Math . max ( dp [ i - 3 ] [ 1 ] , dp [ i - 3 ] [ 0 ] ) ) , Math . max ( dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] ) ) ; dp [ i ] [ 1 ] = a [ i - 1 ] + temp ; dp [ i ] [ 0 ] = Math . max ( ( Math . max ( a [ i - 2 ] + dp [ i - 2 ] [ 0 ] , a [ i - 2 ] + dp [ i - 3 ] [ 1 ] ) ) , Math . max ( a [ i - 2 ] + dp [ i - 3 ] [ 0 ] , a [ i - 3 ] + dp [ i - 3 ] [ 0 ] ) ) ; } else { dp [ i ] [ 1 ] = a [ i - 1 ] + ( Math . max ( ( Math . max ( dp [ i - 2 ] [ 1 ] , dp [ i - 2 ] [ 0 ] ) ) , dp [ i - 1 ] [ 0 ] ) ) ; dp [ i ] [ 0 ] = a [ i - 2 ] + dp [ i - 2 ] [ 0 ] ; } } return Math . max ( dp [ n ] [ 1 ] , dp [ n ] [ 0 ] ) ; } static int max ( int [ ] arr ) { return 1 ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = A . length ; System . out . print ( MaximumSum ( A , N ) ) ; } }
Optimal Strategy for the Divisor game using Dynamic Programming | Java program for implementation of optimal strategy for the divisor game using dynamic programming ; Recursive function to find the winner ; Check if N = 1 or N = 3 then player B wins ; Check if N = 2 then player A wins ; Check if current state already visited then return the previously obtained ans ; Check if currently it is player A 's turn  then initialise the ans to 0 ; Traversing across all the divisors of N which are less than N ; Check if current value of i is a divisor of N ; Check if it is player A 's turn  then we need at least one true ; Else if it is player B 's turn  then we need at least one false ; Return the current ans ; Driver code ; Initialise N
import java . util . * ; class GFG { static int divisorGame ( int N , int A , int dp [ ] [ ] ) { if ( N == 1 N == 3 ) return 0 ; if ( N == 2 ) return 1 ; if ( dp [ N ] [ A ] != - 1 ) return dp [ N ] [ A ] ; int ans = ( A == 1 ) ? 0 : 1 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( A == 1 ) ans |= divisorGame ( N - i , 0 , dp ) ; else ans &= divisorGame ( N - i , 1 , dp ) ; } } return dp [ N ] [ A ] = ans ; } public static void main ( String [ ] args ) { int N = 3 ; int [ ] [ ] dp = new int [ N + 1 ] [ 2 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } if ( divisorGame ( N , 1 , dp ) == 1 ) System . out . print ( " Player ▁ A ▁ wins " ) ; else System . out . print ( " Player ▁ B ▁ wins " ) ; } }
Find the count of mountains in a given Matrix | Java program find the count of mountains in a given Matrix ; Function to count number of mountains in a given matrix of size n ; form another matrix with one extra layer of border elements . Border elements will contain Integer . MIN_VALUE value . ; For border elements , set value as Integer . MIN_VALUE ; For rest elements , just copy it into new matrix ; Check for mountains in the modified matrix ; check for all directions ; Driver code
import java . util . * ; class GFG { static int MAX = 100 ; static int countMountains ( int a [ ] [ ] , int n ) { int [ ] [ ] A = new int [ n + 2 ] [ n + 2 ] ; int count = 0 ; for ( int i = 0 ; i < n + 2 ; i ++ ) { for ( int j = 0 ; j < n + 2 ; j ++ ) { if ( ( i == 0 ) || ( j == 0 ) || ( i == n + 1 ) || ( j == n + 1 ) ) { A [ i ] [ j ] = Integer . MIN_VALUE ; } else { A [ i ] [ j ] = a [ i - 1 ] [ j - 1 ] ; } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( ( A [ i ] [ j ] > A [ i - 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j ] ) && ( A [ i ] [ j ] > A [ i ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j - 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i - 1 ] [ j + 1 ] ) && ( A [ i ] [ j ] > A [ i + 1 ] [ j - 1 ] ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int n = 3 ; System . out . print ( countMountains ( a , n ) ) ; } }
Minimum length of the reduced Array formed using given operations | Java implementation to find the minimum length of the array ; Function to find the length of minimized array ; Creating the required dp tables ; Initialising the dp table by - 1 ; base case ; Check if the two subarray can be combined ; Initialising dp1 table with max value ; Check if the subarray can be reduced to a single element ; Minimal partition of [ 1 : j - 1 ] + 1 ; Driver code
import java . util . * ; class GFG { static int minimalLength ( int a [ ] , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; int [ ] dp1 = new int [ n ] ; int i , j , k ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( int size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i ] [ j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i ] [ k ] != - 1 && dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = ( int ) 1e7 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] != - 1 ) { if ( j == 0 ) dp1 [ i ] = 1 ; else dp1 [ i ] = Math . min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 7 ; int a [ ] = { 3 , 3 , 4 , 4 , 4 , 3 , 3 } ; System . out . print ( minimalLength ( a , n ) ) ; } }
Maximum score possible after performing given operations on an Array | Java program to find the maximum Score after given operations ; Memoizing by the use of a table ; Function to calculate maximum score ; Bse case ; If the same state has already been computed ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , and storing maximum value in DP table to avoid further repetitive recursive calls ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Initialising the DP table , - 1 represents the subproblem hasn 't been solved yet ; Driver code
class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int MaximumScoreDP ( int l , int r , int prefix_sum [ ] , int num ) { if ( l > r ) return 0 ; if ( dp [ l ] [ r ] [ num ] != - 1 ) return dp [ l ] [ r ] [ num ] ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; dp [ l ] [ r ] [ num ] = current_sum + Math . max ( MaximumScoreDP ( l + 1 , r , prefix_sum , num + 1 ) , MaximumScoreDP ( l , r - 1 , prefix_sum , num + 1 ) ) ; return dp [ l ] [ r ] [ num ] ; } static int findMaxScore ( int a [ ] , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int l = 0 ; l < 100 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return MaximumScoreDP ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; int A [ ] = { 1 , 2 , 3 , 4 , 2 , 6 } ; System . out . print ( findMaxScore ( A , n ) ) ; } }
Count number of binary strings without consecutive 1 â €™ s : Set 2 | Java program to count number of binary Strings without consecutive 1 aTMs ; Table to store the solution of every sub problem ; Here , pos : keeps track of current position . f1 : is the flag to check if current number is less than N or not . pr : represents the previous digit ; Base case ; Check if this subproblem has already been solved ; Placing 0 at the current position as it does not violate the condition ; Here flag will be 1 for the next recursive call ; Placing 1 at this position only if the previously inserted number is 0 ; If the number is smaller than N ; If the digit at current position is 1 ; Storing the solution to this subproblem ; Function to find the number of integers less than or equal to N with no consecutive 1 aTMs in binary representation ; Convert N to binary form ; Loop to convert N from Decimal to binary ; Initialising the table with - 1. ; Calling the function ; Utility reverse ; Driver code
class GFG { static int [ ] [ ] [ ] memo = new int [ 32 ] [ 2 ] [ 2 ] ; static int dp ( int pos , int fl , int pr , String bin ) { if ( pos == bin . length ( ) ) return 1 ; if ( memo [ pos ] [ fl ] [ pr ] != - 1 ) return memo [ pos ] [ fl ] [ pr ] ; int val = 0 ; if ( bin . charAt ( pos ) == '0' ) val = val + dp ( pos + 1 , fl , 0 , bin ) ; else if ( bin . charAt ( pos ) == '1' ) val = val + dp ( pos + 1 , 1 , 0 , bin ) ; if ( pr == 0 ) { if ( fl == 1 ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } else if ( bin . charAt ( pos ) == '1' ) { val += dp ( pos + 1 , fl , 1 , bin ) ; } } return memo [ pos ] [ fl ] [ pr ] = val ; } static int findIntegers ( int num ) { String bin = " " ; while ( num > 0 ) { if ( num % 2 == 1 ) bin += "1" ; else bin += "0" ; num /= 2 ; } bin = reverse ( bin ) ; for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) memo [ i ] [ j ] [ l ] = - 1 ; } } return dp ( 0 , 0 , 0 , bin ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . print ( findIntegers ( N ) ) ; } }