codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to reach end from start stone with at most K jumps at each step | Java program to find total no . of ways to reach nth step ; Function which returns total no . of ways to reach nth step from sth steps ; Initialize dp array ; Initialize ( s - 1 ) th index to 1 ; Iterate a loop from s to n ; starting range for counting ranges ; Calculate Maximum moves to Reach ith step ; For nth step return dp [ n - 1 ] ; Driver Code ; no of steps ; Atmost steps allowed ; starting range
class GFG { static int TotalWays ( int n , int s , int k ) { int [ ] dp = new int [ n ] ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = Math . max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int s = 2 ; System . out . print ( " Total ▁ Ways ▁ = ▁ " + TotalWays ( n , s , k ) ) ; } }
Maximum subsequence sum with adjacent elements having atleast K difference in index | Java implementation to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; Function to find the maximum sum subsequence such that two adjacent element have atleast difference of K in their indices ; DP Array to store the maximum sum obtained till now ; Either select the first element or Nothing ; Either Select the ( i - 1 ) element or let the previous best answer be the current best answer ; Either select the best sum till previous_index or select the current element + best_sum till index - k ; Driver Code
class GFG { static int max_sum ( int arr [ ] , int n , int k ) { int dp [ ] = new int [ n ] ; dp [ 0 ] = Math . max ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 2 , 4 , 3 , 1 } ; int n = arr . length ; int k = 4 ; System . out . println ( max_sum ( arr , n , k ) ) ; } }
Number of binary strings such that there is no substring of length â ‰¥ 3 | Java implementation of the approach ; Function to return the count of all possible binary strings ; Base cases ; dp [ i ] [ j ] is the number of possible strings such that '1' just appeared consecutively j times upto ith index ; Taking previously calculated value ; Taking all the possible cases that can appear at the Nth position ; Driver code
class GFG { final static long MOD = 1000000007 ; static long countStr ( int N ) { long dp [ ] [ ] = new long [ N + 1 ] [ 3 ] ; dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } long ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . println ( countStr ( N ) ) ; } }
Count number of ways to get Odd Sum | Java implementation of above approach ; Count the ways to sum up with odd by choosing one element form each pair ; Initialize two array with 0 ; if element is even ; store count of even number in i 'th pair ; if the element is odd ; store count of odd number in i 'th pair ; Initial state of dp array ; dp [ i ] [ 0 ] = total number of ways to get even sum upto i 'th pair ; dp [ i ] [ 1 ] = total number of ways to odd even sum upto i 'th pair ; dp [ n - 1 ] [ 1 ] = total number of ways to get odd sum upto n 'th pair ; Driver code
class GFG { static int CountOfOddSum ( int a [ ] [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; int [ ] [ ] cnt = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( a [ i ] [ j ] % 2 == 0 ) { cnt [ i ] [ 0 ] ++ ; } else { cnt [ i ] [ 1 ] ++ ; } } } dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) ; } return dp [ n - 1 ] [ 1 ] ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 } , { 3 , 6 } } ; int n = a . length ; int ans = CountOfOddSum ( a , n ) ; System . out . println ( ans ) ; } }
Longest Consecuetive Subsequence when only one insert operation is allowed | Java implementation of above approach ; Function to return the length of longest consecuetive subsequence after inserting an element ; Variable to find maximum value of the array ; Calculating maximum value of the array ; Declaring the DP table ; Variable to store the maximum length ; Iterating for every value present in the array ; Recurrence for dp [ val ] [ 0 ] ; No value can be inserted before 1 , hence the element value should be greater than 1 for this recurrance relation ; Recurrence for dp [ val ] [ 1 ] ; Maximum length of consecutive sequence ending at 1 is equal to 1 ; Update the ans variable with the new maximum length possible ; Return the ans ; Driver code ; Input array
class GFG { static int LongestConsSeq ( int [ ] arr , int N ) { int maxval = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { maxval = Math . max ( maxval , arr [ i ] ) ; } int [ ] [ ] dp = new int [ maxval + 1 ] [ 2 ] ; int ans = 1 ; for ( int i = 0 ; i < N ; i += 1 ) { dp [ arr [ i ] ] [ 0 ] = ( 1 + dp [ arr [ i ] - 1 ] [ 0 ] ) ; if ( arr [ i ] >= 2 ) dp [ arr [ i ] ] [ 1 ] = Math . max ( 1 + dp [ arr [ i ] - 1 ] [ 1 ] , 2 + dp [ arr [ i ] - 2 ] [ 0 ] ) ; else dp [ arr [ i ] ] [ 1 ] = 1 ; ans = Math . max ( ans , dp [ arr [ i ] ] [ 1 ] ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 5 } ; int N = arr . length ; System . out . println ( LongestConsSeq ( arr , N ) ) ; } }
Stepping Numbers | A Java program to find all the Stepping Number in [ n , m ] ; This Method checks if an integer n is a Stepping Number ; Initalize prevDigit with - 1 ; Iterate through all digits of n and compare difference between value of previous and current digits ; Get Current digit ; Single digit is consider as a Stepping Number ; Check if absolute difference between prev digit and current digit is 1 ; A brute force approach based function to find all stepping numbers . ; Iterate through all the numbers from [ N , M ] and check if it is a stepping number . ; Driver code ; Display Stepping Numbers in the range [ n , m ]
class Main { public static boolean isStepNum ( int n ) { int prevDigit = - 1 ; while ( n > 0 ) { int curDigit = n % 10 ; if ( prevDigit != - 1 ) { if ( Math . abs ( curDigit - prevDigit ) != 1 ) return false ; } n /= 10 ; prevDigit = curDigit ; } return true ; } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
Count numbers in given range such that sum of even digits is greater than sum of odd digits | Java code to count number in the range having the sum of even digits greater than the sum of odd digits ; Base Case ; check if condition satisfied or not ; If this result is already computed simply return it ; Maximum limit upto which we can place digit . If tight is 0 , means number has already become smaller so we can place any digit , otherwise num [ pos ] ; if current digit is odd ; } if current digit is even ; Function to convert n into its digit vector and uses memo ( ) function to return the required count ; Initialize DP ; Driver Code
import java . util . * ; class GFG { static Vector < Integer > v = new Vector < > ( ) ; static int [ ] [ ] [ ] [ ] dp = new int [ 18 ] [ 180 ] [ 180 ] [ 2 ] ; static int memo ( int index , int evenSum , int oddSum , int tight ) { if ( index == v . size ( ) ) { if ( evenSum > oddSum ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != - 1 ) { return dp [ index ] [ evenSum ] [ oddSum ] [ tight ] ; } int limit = ( tight > 0 ) ? v . get ( index ) : 9 ; int ans = 0 ; for ( int d = 0 ; d <= limit ; d ++ ) { int currTight = 0 ; if ( d == v . get ( index ) ) { currTight = tight ; } if ( d % 2 != 0 ) { ans += memo ( index + 1 , evenSum , oddSum + d , currTight ) ; else { ans += memo ( index + 1 , evenSum + d , oddSum , currTight ) ; } } dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans ; return ans ; } static int CountNum ( int n ) { v . clear ( ) ; while ( n > 0 ) { v . add ( n % 10 ) ; n = n / 10 ; } Collections . reverse ( v ) ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 180 ; j ++ ) { for ( int k = 0 ; k < 180 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } return memo ( 0 , 0 , 0 , 1 ) ; } public static void main ( String [ ] args ) { int L , R ; L = 2 ; R = 10 ; System . out . println ( CountNum ( R ) - CountNum ( L - 1 ) ) ; } }
Maximum sub | Java implementation of the approach ; Function to return the maximum sum of the sub - sequence such that two consecutive elements have a difference of at least 3 in their indices in the given array ; If there is a single element in the array ; Either select it or don 't ; If there are two elements ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select the first element or don 't ; Either select the first or the second element or don 't select any element ; Either select first , second , third or nothing ; For the rest of the elements ; Either select the best sum till previous_index or select the current element + best_sum till index - 3 ; Driver code
class GFG { static int max_sum ( int a [ ] , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; } else if ( n == 2 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . max ( a [ 1 ] , dp [ 0 ] ) ; } else if ( n >= 3 ) { dp [ 0 ] = Math . max ( 0 , a [ 0 ] ) ; dp [ 1 ] = Math . max ( a [ 1 ] , Math . max ( 0 , a [ 0 ] ) ) ; dp [ 2 ] = Math . max ( a [ 2 ] , Math . max ( a [ 1 ] , Math . max ( 0 , a [ 0 ] ) ) ) ; int i = 3 ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , a [ i ] + dp [ i - 3 ] ) ; i ++ ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 2 , 4 , 3 } ; int n = arr . length ; System . out . println ( max_sum ( arr , n ) ) ; } }
Minimum count of elements that sums to a given number | Java implementation of the above approach ; we will only store min counts of sum upto 100 ; initialize with INT_MAX ; memo [ 0 ] = 0 as 0 is made from 0 elements ; fill memo array with min counts of elements that will constitute sum upto 100 ; min_count will store min count of elements chosen ; starting from end iterate over each 2 digits and add min count of elements to min_count ; Driver code
class GFG { static int minCount ( int K ) { int memo [ ] = new int [ 100 ] ; for ( int i = 0 ; i < 100 ; i ++ ) { memo [ i ] = Integer . MAX_VALUE ; } memo [ 0 ] = 0 ; for ( int i = 1 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 1 ] + 1 , memo [ i ] ) ; } for ( int i = 10 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 10 ] + 1 , memo [ i ] ) ; } for ( int i = 25 ; i < 100 ; i ++ ) { memo [ i ] = Math . min ( memo [ i - 25 ] + 1 , memo [ i ] ) ; } int min_count = 0 ; while ( K > 0 ) { min_count += memo [ K % 100 ] ; K /= 100 ; } return min_count ; } public static void main ( String [ ] args ) { int K = 69 ; System . out . println ( minCount ( K ) ) ; } }
Number of sub | Java implementation of above approach ; Function to return the number of subsequences which have at least one consecutive pair with difference less than or equal to 1 ; Not required sub - sequences which turn required on adding i ; Required sub - sequence till now will be required sequence plus sub - sequence which turns required ; Similarly for not required ; Also updating total required and not required sub - sequences ; Also , storing values in dp ; Driver code
public class GFG { static int N = 10000 ; static int count_required_sequence ( int n , int arr [ ] ) { int total_required_subsequence = 0 ; int total_n_required_subsequence = 0 ; int [ ] [ ] dp = new int [ N ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { int turn_required = 0 ; for ( int j = - 1 ; j <= 1 ; j ++ ) turn_required += dp [ arr [ i ] + j ] [ 0 ] ; int required_end_i = ( total_required_subsequence + turn_required ) ; int n_required_end_i = ( 1 + total_n_required_subsequence - turn_required ) ; total_required_subsequence += required_end_i ; total_n_required_subsequence += n_required_end_i ; dp [ arr [ i ] ] [ 1 ] += required_end_i ; dp [ arr [ i ] ] [ 0 ] += n_required_end_i ; } return total_required_subsequence ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 2 , 1 , 9 } ; int n = arr . length ; System . out . println ( count_required_sequence ( n , arr ) ) ; } }
Iterative approach to print all combinations of an Array | Java implementation of the approach ; Data array for combination ; Number of elements in the combination ; The boolean array ; Starting index of the 1 st tract of trues ; Ending index of the 1 st tract of trues ; Constructor ; Set the 1 st r Booleans to true , initialize Start and End ; Generate the very first combination ; Update the starting ending indices of trues in the boolean array ; Function that returns true if another combination can still be generated ; Function to generate the next combination ; Only one true in the tract ; Move the End and reset the End ; Set all the values to false starting from index Start and ending at index End in the boolean array ; Set the beginning elements to true ; Reset the End ; Function to print the combination generated previouslt ; If current index is set to true in the boolean array then element at current index in the original array is part of the combination generated previously ; Driver code
class Combination { private int [ ] Indices ; private int R ; private boolean [ ] Flags ; private int Start ; private int End ; public Combination ( int [ ] arr , int r ) { this . Indices = arr ; this . R = r ; } public void GetFirst ( ) { Flags = new boolean [ this . Indices . length ] ; for ( int i = 0 ; i < this . R ; ++ i ) { Flags [ i ] = true ; } this . Start = 0 ; this . End = this . R - 1 ; this . Output ( ) ; } public boolean HasNext ( ) { return End < ( this . Indices . length - 1 ) ; } public void Next ( ) { if ( this . Start == this . End ) { this . Flags [ this . End ] = false ; this . Flags [ this . End + 1 ] = true ; this . Start += 1 ; this . End += 1 ; while ( this . End + 1 < this . Indices . length && this . Flags [ this . End + 1 ] ) { ++ this . End ; } } else { if ( this . Start == 0 ) { Flags [ this . End ] = false ; Flags [ this . End + 1 ] = true ; this . End -= 1 ; } else { Flags [ this . End + 1 ] = true ; for ( int i = this . Start ; i <= this . End ; ++ i ) { Flags [ i ] = false ; } for ( int i = 0 ; i < this . End - this . Start ; ++ i ) { Flags [ i ] = true ; } this . End = this . End - this . Start - 1 ; this . Start = 0 ; } } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 , count = 0 ; i < Indices . length && count < this . R ; ++ i ) { if ( Flags [ i ] ) { System . out . print ( Indices [ i ] ) ; System . out . print ( " ▁ " ) ; ++ count ; } } System . out . println ( ) ; } } class GFG { public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 3 } ; int r = 3 ; Combination com = new Combination ( arr , r ) ; com . GetFirst ( ) ; while ( com . HasNext ( ) ) { com . Next ( ) ; } } }
Number of ways to choose elements from the array such that their average is K | Java implementation of the above approach ; This dp array is used to store our values so that we don 't have to calculate same  values again and again ; Base cases Index can 't be less than 0 ; No element is picked hence average cannot be calculated ; If remainder is non zero , we cannot divide the sum by count i . e . the average will not be an integer ; If we find an average return 1 ; If we have already calculated this function simply return it instead of calculating it again ; If we don 't pick the current element  simple recur for index -1 ; If we pick the current element add it to our current sum and increment count by 1 ; Store the value for the current function ; Function to return the number of ways ; Push - 1 at the beginning to make it 1 - based indexing ; Initialize dp array by - 1 ; Call recursive function waysutil to calculate total ways ; Driver code
import java . util . * ; class GFG { static int MAX_INDEX = 51 ; static int MAX_SUM = 2505 ; static int [ ] [ ] [ ] dp = new int [ MAX_INDEX ] [ MAX_SUM ] [ MAX_INDEX ] ; static int waysutil ( int index , int sum , int count , Vector < Integer > arr , int K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } int remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } int average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index ] [ sum ] [ count ] != - 1 ) { return dp [ index ] [ sum ] [ count ] ; } int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr . get ( index ) , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index ] [ sum ] [ count ] = total ; return total ; } static int ways ( int N , int K , int [ ] arr ) { Vector < Integer > Arr = new Vector < > ( ) ; Arr . add ( - 1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . add ( arr [ i ] ) ; } for ( int i = 0 ; i < MAX_INDEX ; i ++ ) { for ( int j = 0 ; j < MAX_SUM ; j ++ ) { for ( int l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i ] [ j ] [ l ] = - 1 ; } } } int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 5 ; System . out . println ( ways ( N , K , arr ) ) ; } }
Subset with sum closest to zero | Java Program for above approach ; Variable to store states of dp ; Function to return the number closer to integer s ; To find the sum closest to zero Since sum can be negative , we will add MAX to it to make it positive ; Base cases ; Checks if a state is already solved ; Recurrence relation ; Returning the value ; Function to calculate the closest sum value ; Calculate the Closest value for every subarray arr [ i - 1 : n ] ; Driver Code ; Input array
import java . io . * ; class GFG { static int arrSize = 51 ; static int maxSum = 201 ; static int MAX = 100 ; static int inf = 999999 ; static int dp [ ] [ ] = new int [ arrSize ] [ maxSum ] ; static int visit [ ] [ ] = new int [ arrSize ] [ maxSum ] ; static int RetClose ( int a , int b , int s ) { if ( Math . abs ( a - s ) < Math . abs ( b - s ) ) return a ; else return b ; } static int MinDiff ( int i , int sum , int arr [ ] , int n ) { if ( i == n ) return 0 ; if ( visit [ i ] [ sum + MAX ] > 0 ) return dp [ i ] [ sum + MAX ] ; visit [ i ] [ sum + MAX ] = 1 ; dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; return dp [ i ] [ sum + MAX ] ; } static void FindClose ( int arr [ ] , int n ) { int ans = inf ; for ( int i = 1 ; i <= n ; i ++ ) ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , - 9 , - 10 , - 4 , - 7 , - 33 } ; int n = arr . length ; FindClose ( arr , n ) ; } }
Paths from entry to exit in matrix and maximum path sum | Java implementation of the approach ; Recursive function to return the total paths from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Recursive function to return the maximum sum along the path from grid [ i ] [ j ] to grid [ n - 1 ] [ n - 1 ] ; Out of bounds ; If the current state hasn 't been solved before ; Only valid move is right ; Only valid move is down ; Right and down , both are valid moves ; Driver code ; Fill the dp [ ] [ ] array with - 1 ; When source and destination are same then there is only 1 path ; Print the count of paths from grid [ 0 ] [ 0 ] to grid [ n - 1 ] [ n - 1 ] ; Fill the dp [ ] [ ] array again with - 1 ; When source and destination are same then the sum is grid [ n - 1 ] [ n - 1 ] ; Print the maximum sum among all the paths from grid [ 0 ] [ 0 ] to grid [ n - 1 ] [ n - 1 ]
import java . util . Arrays ; class GFG { static int totalPaths ( int i , int j , int n , int grid [ ] [ ] , int dp [ ] [ ] ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i ] [ j ] == - 1 ) { if ( grid [ i ] [ j ] == 1 ) dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i ] [ j ] == 2 ) dp [ i ] [ j ] = totalPaths ( i + 1 , j , n , grid , dp ) ; else dp [ i ] [ j ] = totalPaths ( i , j + 1 , n , grid , dp ) + totalPaths ( i + 1 , j , n , grid , dp ) ; } return dp [ i ] [ j ] ; } static int maxSumPath ( int i , int j , int n , int grid [ ] [ ] , int dp [ ] [ ] ) { if ( i < 0 j < 0 i >= n j >= n ) return 0 ; if ( dp [ i ] [ j ] == - 1 ) { if ( grid [ i ] [ j ] == 1 ) dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i , j + 1 , n , grid , dp ) ; else if ( grid [ i ] [ j ] == 2 ) dp [ i ] [ j ] = grid [ i ] [ j ] + maxSumPath ( i + 1 , j , n , grid , dp ) ; else dp [ i ] [ j ] = grid [ i ] [ j ] + Math . max ( maxSumPath ( i , j + 1 , n , grid , dp ) , maxSumPath ( i + 1 , j , n , grid , dp ) ) ; } return dp [ i ] [ j ] ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 1 , 3 , 2 , 1 } , { 3 , 2 , 2 , 1 , 2 } , { 1 , 3 , 3 , 1 , 3 } , { 1 , 2 , 3 , 1 , 2 } , { 1 , 1 , 1 , 3 , 1 } } ; int n = grid . length ; int dp [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) Arrays . fill ( dp [ i ] , - 1 ) ; dp [ n - 1 ] [ n - 1 ] = 1 ; System . out . println ( " Total ▁ paths : ▁ " + totalPaths ( 0 , 0 , n , grid , dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) Arrays . fill ( dp [ i ] , - 1 ) ; dp [ n - 1 ] [ n - 1 ] = grid [ n - 1 ] [ n - 1 ] ; System . out . println ( " Maximum ▁ sum : ▁ " + maxSumPath ( 0 , 0 , n , grid , dp ) ) ; } }
Queries for bitwise OR in the index range [ L , R ] of the given array | Java implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
import java . io . * ; class GFG { static int MAX = 100000 ; static int bitscount = 32 ; static int [ ] [ ] prefix_count = new int [ bitscount ] [ MAX ] ; static void findPrefixCount ( int arr [ ] , int n ) { for ( int i = 0 ; i < bitscount ; i ++ ) { prefix_count [ i ] [ 0 ] = ( ( arr [ 0 ] >> i ) & 1 ) ; for ( int j = 1 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] = ( ( arr [ j ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] += prefix_count [ i ] [ j - 1 ] ; } } } static int rangeOr ( int l , int r ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int x ; if ( l == 0 ) x = prefix_count [ i ] [ r ] ; else x = prefix_count [ i ] [ r ] - prefix_count [ i ] [ l - 1 ] ; if ( x != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 5 , 3 , 5 , 2 , 3 } ; int n = arr . length ; findPrefixCount ( arr , n ) ; int queries [ ] [ ] = { { 1 , 3 } , { 4 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } }