codeparrot/xlcost-text-to-code huggingface dataset:
Mobile Numeric Keypad Problem | Set 2 | Java implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 list [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code
class GFG { static int MAX = 10 ; static int getCount ( int n ) { int [ ] [ ] list = new int [ MAX ] [ ] ; list [ 0 ] = new int [ ] { 0 , 8 } ; list [ 1 ] = new int [ ] { 1 , 2 , 4 } ; list [ 2 ] = new int [ ] { 2 , 1 , 3 , 5 } ; list [ 3 ] = new int [ ] { 3 , 6 , 2 } ; list [ 4 ] = new int [ ] { 4 , 1 , 7 , 5 } ; list [ 5 ] = new int [ ] { 5 , 4 , 6 , 2 , 8 } ; list [ 6 ] = new int [ ] { 6 , 3 , 5 , 9 } ; list [ 7 ] = new int [ ] { 7 , 4 , 8 } ; list [ 8 ] = new int [ ] { 8 , 5 , 0 , 7 , 9 } ; list [ 9 ] = new int [ ] { 9 , 6 , 8 } ; int Arr [ ] = new int [ ] { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int Arr2 [ ] = new int [ MAX ] ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x = 0 ; x < list [ j ] . length ; x ++ ) { Arr2 [ list [ j ] [ x ] ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( getCount ( n ) ) ; } }
Optimally accommodate 0 s and 1 s from a Binary String into K buckets | Java implementation of the approach ; 2 - D dp array saving different states dp [ i ] [ j ] = minimum value of accommodation till i 'th index of the string using j+1 number of buckets. ; Function to find the minimum required sum using dynamic programming ; If both start and bucket reached end then return 0 else that arrangement is not possible so return INT_MAX ; Corner case ; If state if already calculated then return its answer ; Start filling zeroes and ones which to be accommodated in jth bucket then ans for current arrangement will be ones * zeroes + recur ( i + 1 , bucket + 1 ) ; If this arrangement is not possible then don 't calculate further ; Function to initialize the dp and call solveUtil ( ) method to get the answer ; Start with 0 - th index and 1 bucket ; Driver code ; K buckets
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] dp ; static int solveUtil ( int start , int bucket , String str , int K ) { int N = str . length ( ) ; if ( start == N ) { if ( bucket == K ) { return 0 ; } return Integer . MAX_VALUE ; } if ( bucket == K ) { return Integer . MAX_VALUE ; } if ( dp [ start ] [ bucket ] != - 1 ) { return dp [ start ] [ bucket ] ; } int zeroes = 0 ; int ones = 0 ; int ans = Integer . MAX_VALUE ; for ( int i = start ; i < N ; ++ i ) { if ( str . charAt ( i ) == '1' ) { ones ++ ; } else { zeroes ++ ; } if ( ones * zeroes > ans ) { break ; } int temp = solveUtil ( i + 1 , bucket + 1 , str , K ) ; if ( temp != Integer . MAX_VALUE ) { ans = Math . min ( ans , temp + ( ones * zeroes ) ) ; } } return dp [ start ] [ bucket ] = ans ; } static int solve ( String str , int K ) { int N = str . length ( ) ; dp = new int [ N ] [ K ] ; for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } int ans = solveUtil ( 0 , 0 , str , K ) ; return ans == Integer . MAX_VALUE ? - 1 : ans ; } public static void main ( String [ ] args ) { String S = "0101" ; int K = 2 ; System . out . println ( solve ( S , K ) ) ; } }
Probability of getting more heads than tails when N biased coins are tossed | Java implementation of the above approach ; Function to return the probability when number of heads is greater than the number of tails ; Declaring the DP table ; Base case ; Iterating for every coin ; j represents the numbers of heads ; If number of heads is equal to zero there there is only one possibility ; When the number of heads is greater than ( n + 1 ) / 2 it means that heads are greater than tails as no of tails + no of heads is equal to n for any permutation of heads and tails ; Driver Code ; 1 based indexing ; Number of coins ; Function call
class GFG { static double Probability ( double p [ ] , int n ) { double [ ] [ ] dp = new double [ n + 1 ] [ n + 1 ] ; dp [ 0 ] [ 0 ] = 1.0 ; for ( int i = 1 ; i <= n ; i += 1 ) { for ( int j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ; } } double ans = 0.0 ; for ( int i = ( n + 1 ) / 2 ; i <= n ; i += 1 ) ans += dp [ n ] [ i ] ; return ans ; } public static void main ( String [ ] args ) { double p [ ] = { 0.0 , 0.3 , 0.4 , 0.7 } ; int n = p . length - 1 ; System . out . println ( Probability ( p , n ) ) ; } }
Number of ways to get a given sum with n number of m | Java function to calculate the number of ways to achieve sum x in n no of throwsVal ; Function to calculate recursively the number of ways to get sum in given throwsVal and [ 1. . m ] values ; Base condition 1 ; Base condition 2 ; If value already calculated dont move into re - computation ; Recursively moving for sum - i in throwsVal - 1 no of throwsVal left ; Inserting present values in dp ; Driver code
class GFG { static int mod = 1000000007 ; static int [ ] [ ] dp = new int [ 55 ] [ 55 ] ; static int NoofWays ( int face , int throwsVal , int sum ) { if ( sum == 0 && throwsVal == 0 ) { return 1 ; } if ( sum < 0 throwsVal == 0 ) { return 0 ; } if ( dp [ throwsVal ] [ sum ] != - 1 ) { return dp [ throwsVal ] [ sum ] ; } int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throwsVal - 1 , sum - i ) ; } return dp [ throwsVal ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int faces = 6 , throwsVal = 3 , sum = 12 ; for ( int i = 0 ; i < 55 ; i ++ ) { for ( int j = 0 ; j < 55 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( NoofWays ( faces , throwsVal , sum ) ) ; } }
Longest sub | Java implementation of the approach ; Function to return the length of the longest required sub - sequence ; Sort the array ; To store the resultant length ; If array contains only one element then it divides itself ; Every element divides itself ; Count for all numbers which are lesser than a [ i ] ; If a [ i ] % a [ j ] then update the maximum subsequence length , dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) where j is in the range [ 0 , i - 1 ] ; If array contains only one element then i = j which doesn 't satisfy the condition ; Driver code
import java . util . Arrays ; import java . io . * ; class GFG { static int find ( int n , int a [ ] ) { Arrays . sort ( a ) ; int res = 1 ; int dp [ ] = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( a [ i ] % a [ j ] == 0 ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) ; } } res = Math . max ( res , dp [ i ] ) ; } return ( res == 1 ) ? - 1 : res ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 6 , 1 , 3 , 11 } ; int n = a . length ; System . out . println ( find ( n , a ) ) ; } }
Maximize the happiness of the groups on the Trip | Java implementation of the approach ; Function to return the maximized happiness ; Two arrays similar to 0 1 knapsack problem ; To store the happiness of the current goup ; Current person is a child ; Woman ; Man ; Old Person ; Group 's happiness is the sum of happiness  of the people in the group multiplie  the number of people ; Solution using 0 1 knapsack ; Driver code ; Number of seats ; Groups
class GFG { static int maxHappiness ( int A , int N , String [ ] v ) { String str ; int [ ] val = new int [ N ] ; int [ ] wt = new int [ N ] ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { str = v [ i ] ; c = 0 ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) { if ( str . charAt ( j ) == ' c ' ) c += 4 ; else if ( str . charAt ( j ) == ' w ' ) c += 3 ; else if ( str . charAt ( j ) == ' m ' ) c += 2 ; else c ++ ; } c *= str . length ( ) ; val [ i ] = c ; wt [ i ] = str . length ( ) ; } int [ ] [ ] k = new int [ N + 1 ] [ A + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int w = 0 ; w <= A ; w ++ ) { if ( i == 0 w == 0 ) k [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) { k [ i ] [ w ] = Math . max ( val [ i - 1 ] + k [ i - 1 ] [ w - wt [ i - 1 ] ] , k [ i - 1 ] [ w ] ) ; } else { k [ i ] [ w ] = k [ i - 1 ] [ w ] ; } } } return k [ N ] [ A ] ; } public static void main ( String [ ] args ) { int A = 5 ; String [ ] v = { " mmo " , " oo " , " cmw " , " cc " , " c " } ; int N = v . length ; System . out . println ( maxHappiness ( A , N , v ) ) ; } }
0 | Java implementation of the approach ; To store states of DP ; To check if a state has been solved ; Function to compute the states ; Base case ; Check if a state has been solved ; Setting a state as solved ; Returning the solved state ; Function to pre - compute the states dp [ 0 ] [ 0 ] , dp [ 0 ] [ 1 ] , . . , dp [ 0 ] [ C_MAX ] ; Function to answer a query in O ( 1 ) ; Driver code ; Performing required pre - computation ; Perform queries
class GFG { static int C_MAX = 30 ; static int max_arr_len = 10 ; static int dp [ ] [ ] = new int [ max_arr_len ] [ C_MAX + 1 ] ; static boolean v [ ] [ ] = new boolean [ max_arr_len ] [ C_MAX + 1 ] ; static int findMax ( int i , int r , int w [ ] , int n ) { if ( r < 0 ) return Integer . MIN_VALUE ; if ( i == n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = true ; dp [ i ] [ r ] = Math . max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i ] [ r ] ; } static void preCompute ( int w [ ] , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } static int ansQuery ( int w ) { return dp [ 0 ] [ w ] ; } public static void main ( String [ ] args ) { int w [ ] = new int [ ] { 3 , 8 , 9 } ; int n = w . length ; preCompute ( w , n ) ; int queries [ ] = new int [ ] { 11 , 10 , 4 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ansQuery ( queries [ i ] ) ) ; } }
Maximise array sum after taking non | Java implementation of the approach ; To store the states of dp ; To check if a given state has been solved ; To store the prefix - sum ; Function to fill the prefix_sum [ ] with the prefix sum of the given array ; Function to find the maximum sum subsequence such that no two elements are adjacent ; Base case ; To check if a state has been solved ; Required recurrence relation ; Returning the value ; Driver code ; Finding prefix - sum ; Finding the maximum possible sum
import java . util . * ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static boolean [ ] v = new boolean [ maxLen ] ; static int [ ] prefix_sum = new int [ maxLen ] ; static void findPrefixSum ( int arr [ ] , int n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } } static int maxSum ( int arr [ ] , int i , int n , int k ) { if ( i + k > n ) { return 0 ; } if ( v [ i ] ) { return dp [ i ] ; } v [ i ] = true ; int x ; if ( i == 0 ) { x = prefix_sum [ k - 1 ] ; } else { x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; } dp [ i ] = Math . max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 , 6 } ; int n = arr . length ; int k = 1 ; findPrefixSum ( arr , n ) ; System . out . println ( maxSum ( arr , 0 , n , k ) ) ; } }
Find maximum path sum in a 2D matrix when exactly two left moves are allowed | Java program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code
import java . io . * ; class GFG { static int N = 3 ; static int M = 3 ; static int findMaxSum ( int arr [ ] [ ] ) { int sum = 0 ; int [ ] [ ] b = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ; b [ i ] [ j ] = Math . max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; System . out . println ( findMaxSum ( arr ) ) ; } }
Queries to check if string B exists as substring in string A | Java implementation of the approach ; Function to return the modular inverse using Fermat 's little theorem ; Function to generate hash ; To store prefix - sum of rolling hash ; Multiplier for different values of i ; Generating hash value for string b ; Generating prefix - sum of hash of a ; Function that returns true if the required sub - string in a is equal to b ; To store hash of required sub - string of A ; If i = 0 then requires hash value ; Required hash if i != 0 ; Comparing hash with hash of B ; Driver code ; Generating hash ; Queries ; Perform queries
import java . util . * ; class GFG { static int mod = 3803 ; static int d = 26 ; static int hash_b ; static int [ ] hash_a ; static int [ ] mul ; static int mi ( int x ) { int p = mod - 2 ; int s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) { s = ( s * x ) % mod ; } x = ( x * x ) % mod ; p /= 2 ; } return ( s * x ) % mod ; } static void genHash ( char [ ] a , char [ ] b ) { hash_a = new int [ a . length ] ; mul = new int [ a . length ] ; for ( int i = b . length - 1 ; i >= 0 ; i -- ) { hash_b = ( hash_b * d + ( b [ i ] - 97 ) ) % mod ; } mul [ 0 ] = 1 ; hash_a [ 0 ] = ( a [ 0 ] - 97 ) % mod ; for ( int i = 1 ; i < a . length ; i ++ ) { mul [ i ] = ( mul [ i - 1 ] * d ) % mod ; hash_a [ i ] = ( hash_a [ i - 1 ] + mul [ i ] * ( a [ i ] - 97 ) ) % mod ; } } static boolean checkEqual ( int i , int len_a , int len_b ) { int x ; if ( i == 0 ) { x = hash_a [ len_b - 1 ] ; } else { x = ( hash_a [ i + len_b - 1 ] - hash_a [ i - 1 ] + 2 * mod ) % mod ; x = ( x * mi ( mul [ i ] ) ) % mod ; } if ( x == hash_b ) { return true ; } return false ; } public static void main ( String [ ] args ) { String a = " abababababa " ; String b = " aba " ; genHash ( a . toCharArray ( ) , b . toCharArray ( ) ) ; int queries [ ] = { 0 , 1 , 2 , 3 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { if ( checkEqual ( queries [ i ] , a . length ( ) , b . length ( ) ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } }
Number of ways to divide an array into K equal sum sub | Java implementation of the approach ; Array to store the states of DP ; Array to check if a state has been solved before ; To store the sum of the array elements ; Function to find the sum of all the array elements ; Function to return the number of ways ; If sum is not divisible by k answer will be zero ; Base case ; To check if a state has been solved before ; Sum of all the numbers from the beginning of the array ; Setting the current state as solved ; Recurrence relation ; Returning solved state ; Driver code ; Function call to find the sum of the array elements ; Print the number of ways
class GFG { static int max_size = 20 ; static int max_k = 20 ; static int [ ] [ ] dp = new int [ max_size ] [ max_k ] ; static boolean [ ] [ ] v = new boolean [ max_size ] [ max_k ] ; static int sum = 0 ; static void findSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } static int cntWays ( int arr [ ] , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i ] [ ck ] ) return dp [ i ] [ ck ] ; curr_sum += arr [ i ] ; v [ i ] [ ck ] = true ; dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i ] [ ck ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . length ; int k = 2 ; findSum ( arr , n ) ; System . out . println ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; } }
Maximum sum in an array such that every element has exactly one adjacent element to it | Java implementation of the approach ; To store the states of dp ; Function to return the maximized sum ; Base case ; Checks if a state is already solved ; Recurrence relation ; Return the result ; Driver code
import java . io . * ; class GFG { static int arrSize = 51 ; static int dp [ ] = new int [ arrSize ] ; static boolean v [ ] = new boolean [ arrSize ] ; static int sumMax ( int i , int arr [ ] , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( sumMax ( 0 , arr , n ) ) ; } }
Maximum Sum Subsequence of length k | Java program to calculate the maximum sum of increasing subsequence of length k ; In the implementation dp [ n ] [ k ] represents maximum sum subsequence of length k and the subsequence is ending at index n . ; Initializing whole multidimensional dp array with value - 1 ; For each ith position increasing subsequence of length 1 is equal to that array ith value so initializing dp [ i ] [ 1 ] with that array value ; Starting from 1 st index as we have calculated for 0 th index . Computing optimized dp values in bottom - up manner ; check for increasing subsequence ; Proceed if value is pre calculated ; Check for all the subsequences ending at any j < i and try including element at index i in them for some length l . Update the maximum value for every length . ; The final result would be the maximum value of dp [ i ] [ k ] for all different i . ; When no subsequence of length k is possible sum would be considered zero ; Driver code
import java . util . * ; class GFG { static int MaxIncreasingSub ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ n ] [ k + 1 ] , ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < k + 1 ; j ++ ) dp [ i ] [ j ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] [ 1 ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { for ( int l = 1 ; l <= k - 1 ; l ++ ) { if ( dp [ j ] [ l ] != - 1 ) { dp [ i ] [ l + 1 ] = Math . max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) ; } } } } } for ( int i = 0 ; i < n ; i ++ ) { if ( ans < dp [ i ] [ k ] ) ans = dp [ i ] [ k ] ; } return ( ans == - 1 ) ? 0 : ans ; } public static void main ( String args [ ] ) { int n = 8 , k = 3 ; int arr [ ] = { 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 } ; int ans = MaxIncreasingSub ( arr , n , k ) ; System . out . println ( ans ) ; } }
Generate all unique partitions of an integer | Set 2 | Java implementation of the approach ; Array to store the numbers used to form the required sum ; Function to print the array which contains the unique partitions which are used to form the required sum ; Function to find all the unique partitions remSum = remaining sum to form maxVal is the maximum number that can be used to make the partition ; If remSum == 0 that means the sum is achieved so print the array ; i will begin from maxVal which is the maximum value which can be used to form the sum ; Store the number used in forming sum gradually in the array ; Since i used the rest of partition cant have any number greater than i hence second parameter is i ; Driver code
import java . util . * ; class GFG { static int [ ] dp = new int [ 200 ] ; static int count = 0 ; static void print ( int idx ) { for ( int i = 1 ; i < idx ; i ++ ) { System . out . print ( dp [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } static void solve ( int remSum , int maxVal , int idx , int count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( int i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } public static void main ( String [ ] args ) { int n = 4 , count = 0 ; solve ( n , n , 1 , count ) ; } }
Queries for bitwise OR in the given matrix | 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 for each row ; Finding column - wise prefix count ; Function to return the result for a query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; If count of variables with ith bit set is greater than 0 ; Driver code
class GFG { final static int bitscount = 32 ; final static int n = 3 ; static int prefix_count [ ] [ ] [ ] = new int [ bitscount ] [ n ] [ n ] ; static void findPrefixCount ( int arr [ ] [ ] ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } static int rangeOr ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int queries [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) ) ; } }