codeparrot/xlcost-text-to-code huggingface dataset:
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix | Java program for the above approach ; Function to find the matrix , A [ ] [ ] satisfying the given conditions ; Store the final matrix ; Initialize all the elements of the matrix A with 1 ; Traverse the matrix B [ ] [ ] row - wise ; If B [ i ] [ j ] is equal to 0 ; Mark all the elements of ith row of A [ ] [ ] as 0 ; Mark all the elements of jth column of A [ ] [ ] as 0 ; Check if the matrix B [ ] [ ] can be made using matrix A [ ] [ ] ; Store the bitwise OR of all elements of A [ ] [ ] in ith row and jth column ; Traverse through ith row ; Traverse through jth column ; If B [ i ] [ j ] is not equal to c , print " Not ▁ Possible " ; Print the final matrix A [ ] [ ] ; Driver Code ; Function Call
import java . io . * ; class GFG { static void findOriginalMatrix ( int [ ] [ ] B , int N , int M ) { int [ ] [ ] A = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { A [ i ] [ j ] = 1 ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { if ( B [ i ] [ j ] == 0 ) { for ( int k = 0 ; k < M ; ++ k ) { A [ i ] [ k ] = 0 ; } for ( int k = 0 ; k < N ; ++ k ) { A [ k ] [ j ] = 0 ; } } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { int c = 0 ; for ( int k = 0 ; k < M ; ++ k ) { if ( c == 1 ) break ; c += A [ i ] [ k ] ; } for ( int k = 0 ; k < N ; ++ k ) { if ( c == 1 ) break ; c += A [ k ] [ j ] ; } if ( c != B [ i ] [ j ] ) { System . out . println ( " Not ▁ Possible " ) ; return ; } } } 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 [ ] [ ] B = new int [ ] [ ] { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int N = B . length ; int M = B [ 0 ] . length ; findOriginalMatrix ( B , N , M ) ; } }
Minimize cost to make X and Y equal by given increments | Java program for the above approach ; Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
import java . util . * ; class GFG { static int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } static int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } public static void main ( String [ ] args ) { int x = 5 , y = 17 ; System . out . print ( minimumCost ( x , y ) + "NEW_LINE"); } }
Find GCD between the sum of two given integers raised to the power of N and their difference | Java program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Driver code ; Given p , q and n ; Function Call
import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int a , int n , int d ) { int res = 1 ; while ( n != 0 ) { if ( ( n % 2 ) != 0 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } static int gcd ( int p , int q , int n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } int candidate = 1 ; int num = p - q ; int sq = ( int ) Math . sqrt ( num ) ; for ( int i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { int X = power ( p , n , i ) ; int Y = power ( q , n , i ) ; int temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = Math . max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = Math . max ( candidate , num / i ) ; } } } return candidate % mod ; } public static void main ( String [ ] args ) { int p , q , n ; p = 10 ; q = 6 ; n = 5 ; System . out . println ( gcd ( p , q , n ) ) ; } }
Split given isosceles triangle of height H into N equal parts | Java Code for above approach ; Function to divide the isosceles triangle in equal parts by making N - 1 cuts parallel to the base ; Iterate over the range [ 1 , n - 1 ] ; Driver code ; Given N ; Given H ; Function call
import java . util . * ; class GFG { static void findPoint ( int n , int h ) { for ( int i = 1 ; i < n ; i ++ ) System . out . printf ( " % .2f ▁ " , Math . sqrt ( i / ( n * 1.0 ) ) * h ) ; } public static void main ( String [ ] args ) { int n = 3 ; int h = 2 ; findPoint ( n , h ) ; } }
Count all possible values of K less than Y such that GCD ( X , Y ) = GCD ( X + K , Y ) | Java program for the above approach ; Function to find the gcd of a and b ; Function to find the number of Ks ; Find gcd ; Calculating value of totient function for n ; Driver Code ; Given X and Y
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int calculateK ( int x , int y ) { int g = gcd ( x , y ) ; int n = y / g ; int res = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { res -= ( res / i ) ; while ( n % i == 0 ) n /= i ; } } if ( n != 1 ) res -= ( res / n ) ; return res ; } public static void main ( String [ ] args ) { int x = 3 , y = 15 ; System . out . print ( calculateK ( x , y ) + "NEW_LINE"); } }
Check if a Float value is equivalent to an Integer value | Java program to implement the above approach ; Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; Driver code
import java . util . * ; class GFG { static boolean isInteger ( double N ) { int X = ( int ) N ; double temp2 = N - X ; if ( temp2 > 0 ) { return false ; } return true ; } public static void main ( String [ ] args ) { double N = 1.5 ; if ( isInteger ( N ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Find the nearest power of 2 for every array element | Java program to implement the above approach ; Function to find the nearest power of two for every element of the given array ; Traverse the array ; Calculate log of the current array element ; Find the nearest ; Driver Code
import java . io . * ; class GFG { static void nearestPowerOfTwo ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int lg = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; int a = ( int ) ( Math . pow ( 2 , lg ) ) ; int b = ( int ) ( Math . pow ( 2 , lg + 1 ) ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) System . out . print ( a + " ▁ " ) ; else System . out . print ( b + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 7 , 12 } ; int N = arr . length ; nearestPowerOfTwo ( arr , N ) ; } }
Flip bits of the sum of count of set bits of two given numbers | Java program for the above approach ; Function to count number of set bits in integer ; Variable for counting set bits ; Function to invert bits of a number ; Calculate number of bits of N - 1 ; ; Function to invert the sum of set bits in A and B ; Stores sum of set bits ; Driver Code
import java . util . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int invertBits ( int n ) { int x = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; int m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } static void invertSum ( int A , int B ) { int temp = countSetBits ( A ) + countSetBits ( B ) ; System . out . print ( invertBits ( temp ) ) ; } static public void main ( String args [ ] ) { int A = 5 ; int B = 7 ; invertSum ( A , B ) ; } }
Sum of Bitwise XOR of each array element with all other array elements | Java program for the above approach ; Function to calculate for each array element , sum of its Bitwise XOR with all other array elements ; Declare an array of size 64 to store count of each bit ; Traversing the array ; Check if bit is present of not ; Increase the bit position ; Reduce the number to half ; Traverse the array ; Stores the bit position ; Stores the sum of Bitwise XOR ; Check if bit is present of not ; Reduce the number to its half ; Print the sum for A [ i ] ; Driver code ; Given array ; Given N ; Function Call
import java . util . * ; class GFG { static void XOR_for_every_i ( int A [ ] , int N ) { int frequency_of_bits [ ] = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int M = A [ i ] ; while ( M != 0 ) { if ( ( M & 1 ) != 0 ) { frequency_of_bits [ bit_position ] += 1 ; } bit_position += 1 ; M >>= 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { int M = A [ i ] ; int value_at_that_bit = 1 ; int XOR_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( M & 1 ) != 0 ) { XOR_sum += ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } else { XOR_sum += ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } M >>= 1 ; value_at_that_bit <<= 1 ; } System . out . print ( XOR_sum + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int N = A . length ; XOR_for_every_i ( A , N ) ; } }
Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half | Java program for the above approach ; Function to minimize the maximum difference between any pair of elements of the array by the given operations ; Traverse the array ; If current element is even ; Insert it into even ; Otherwise ; Make it even by multiplying by 2 and insert it into set ; Calculate difference between first and the last element of the set ; Iterate until difference is minimized ; Erase the current element ; Reduce current element by half and insert it into the Set ; Update difference ; Return the resultant difference ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int minimumMaxDiff ( int [ ] nums ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { if ( nums [ i ] % 2 == 0 ) s . add ( nums [ i ] ) ; else s . add ( nums [ i ] * 2 ) ; } int res = s . last ( ) - s . first ( ) ; while ( s . last ( ) % 2 == 0 ) { int x = s . last ( ) ; s . remove ( x ) ; s . add ( x / 2 ) ; res = Math . min ( res , s . last ( ) - s . first ( ) ) ; } return res ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 5 , 9 } ; System . out . print ( minimumMaxDiff ( arr ) ) ; } }
XOR of all even numbers from a given range | Java Implementation of the above approach ; Function to calculate XOR of numbers in the range [ 1 , n ] ; If n is divisible by 4 ; If n mod 4 is equal to 1 ; If n mod 4 is equal to 2 ; Function to find XOR of even numbers in the range [ l , r ] ; Stores XOR of even numbers in the range [ 1 , l - 1 ] ; Stores XOR of even numbers in the range [ 1 , r ] ; Update xor_r ; Update xor_l ; Driver Code
class GFG { static int bitwiseXorRange ( int n ) { if ( n % 4 == 0 ) return n ; if ( n % 4 == 1 ) return 1 ; if ( n % 4 == 2 ) return n + 1 ; return 0 ; } static int evenXorRange ( int l , int r ) { int xor_l ; int xor_r ; xor_r = 2 * bitwiseXorRange ( r / 2 ) ; xor_l = 2 * bitwiseXorRange ( ( l - 1 ) / 2 ) ; return xor_l ^ xor_r ; } public static void main ( String [ ] args ) { int l = 10 ; int r = 20 ; System . out . print ( evenXorRange ( l , r ) ) ; } }
Program to calculate Variance of first N Natural Numbers | Java program to implement the above approach ; Function to calculate Variance of first N natural numbers ; Driver Code
class GFG { static double find_Variance ( int n ) { long numerator = n * n - 1 ; double ans = ( numerator * 1.0 ) / 12 ; return ans ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( find_Variance ( N ) ) ; } }
Count digits present in each element of a given Matrix | Java program for the above approach ; Function to count the number of digits in each element of the given matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Count the number of digits ; Print the result ; Driver Code ; Given matrix
import java . util . * ; class GFG { static int M = 3 ; static int N = 3 ; static void countDigit ( int arr [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int X = arr [ i ] [ j ] ; int d = ( int ) ( Math . floor ( Math . log10 ( X ) * 1.0 ) + 1 ) ; System . out . print ( d + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 27 , 173 , 5 } , { 21 , 6 , 624 } , { 5 , 321 , 49 } } ; countDigit ( arr ) ; } }
Difference between sum of odd and even frequent elements in an Array | Java program to find absolute difference between the sum of all odd frequenct and even frequent elements in an array ; Function to find the sum of all even and odd frequent elements in an array ; Stores the frequency of array elements ; Traverse the array ; Update frequency of current element ; Stores sum of odd and even frequent elements ; Traverse the map ; If frequency is odd ; Add sum of all occurrences of current element to sum_odd ; If frequency is even ; Add sum of all occurrences of current element to sum_even ; Calculate difference between their sum ; Return diff ; Driver Code
import java . util . * ; import java . io . * ; import java . math . * ; class GFG { static int findSum ( int arr [ ] , int N ) { Map < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! map . containsKey ( arr [ i ] ) ) map . put ( arr [ i ] , 1 ) ; else map . replace ( arr [ i ] , map . get ( arr [ i ] ) + 1 ) ; } int sum_odd = 0 , sum_even = 0 ; Set < Map . Entry < Integer , Integer > > hmap = map . entrySet ( ) ; for ( Map . Entry < Integer , Integer > data : hmap ) { int key = data . getKey ( ) ; int val = data . getValue ( ) ; if ( val % 2 != 0 ) sum_odd += ( key ) * ( val ) ; if ( val % 2 == 0 ) sum_even += ( key ) * ( val ) ; } int diff = sum_even - sum_odd ; return diff ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 5 , 2 , 4 , 3 , 3 } ; int N = arr . length ; System . out . println ( findSum ( arr , N ) ) ; } }
Count N | Java program for the above approach ; Function to find the number of arrays following the given condition ; Initialize answer ; Calculate nPm ; Print ans ; Driver Code ; Given N and M ; Function Call
class GFG { static void noOfArraysPossible ( int N , int M ) { int ans = 1 ; for ( int i = 0 ; i < N ; ++ i ) { ans = ans * ( M - i ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; noOfArraysPossible ( N , M ) ; } }
Permutations of an array having sum of Bitwise AND of adjacent elements at least K | Java program for the above approach ; Function to print all permutations of arr [ ] such that the sum of Bitwise AND of all adjacent element is at least K ; To check if any permutation exists or not ; Sort the given array ; Find all possible permutations ; Stores the sum of bitwise AND of adjacent elements of the current permutation ; Traverse the current permutation of arr [ ] ; Update the sum ; If the sum is at least K , then print the current permutation ; Set the flag variable ; Print the current permutation ; If flag is unset , then print - 1 ; Driver Code ; Function Call
import java . util . * ; class GFG { static void printPermutation ( int arr [ ] , int n , int k ) { boolean flag = false ; Arrays . sort ( arr ) ; do { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] & arr [ i + 1 ] ; } if ( sum >= k ) { flag = true ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } } while ( next_permutation ( arr ) ) ; if ( flag == false ) { System . out . print ( " - 1" ) ; } } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 8 ; int N = arr . length ; printPermutation ( arr , N , K ) ; } }
Positive integers up to N that are not present in given Array | Java program for the above approach ; Function to find positive integers from 1 to N that are not present in the array ; Declare bitset ; Iterate from 0 to M - 1 ; Iterate from 0 to len - 1 ; Iterate from bset . _Find_first ( ) to bset . size ( ) - 1 ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void findMissingNumbers ( int [ ] arr , int len ) { int M = 15 ; BitSet bset = new BitSet ( M ) ; for ( int i = 0 ; i < M ; i ++ ) { bset . set ( i ) ; } for ( int i = 0 ; i < len ; i ++ ) { bset . set ( arr [ i ] - 1 , false ) ; } for ( int i = bset . nextSetBit ( 0 ) ; i >= 0 ; i = bset . nextSetBit ( i + 1 ) ) { if ( i + 1 > len ) break ; System . out . println ( i + 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 , 8 , 9 } ; int n = arr . length ; findMissingNumbers ( arr , n ) ; } }
Sum of the first N terms of XOR Fibonacci series | Java program for the above approach ; Function to calculate the sum of the first N terms of XOR Fibonacci Series ; Base Case ; Stores the sum of the first N terms ; Iterate from [ 0 , n - 3 ] ; Store XOR of last 2 elements ; Update sum ; Update the first element ; Update the second element ; Print the final sum ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findSum ( int a , int b , int n ) { if ( n == 1 ) { System . out . println ( a ) ; return ; } int s = a + b ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int x = a ^ b ; s += x ; a = b ; b = x ; } System . out . println ( s ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , N = 8 ; findSum ( a , b , N ) ; } }
Sum of the first N terms of XOR Fibonacci series | Java program for the above approach ; Function to calculate sum of the first N terms of XOR Fibonacci Series ; Store the sum of first n terms ; Store XOR of a and b ; Case 1 : If n is divisible by 3 ; Case 2 : If n % 3 leaves remainder 1 ; Case 3 : If n % 3 leaves remainder 2 on division by 3 ; Print the final sum ; Driver Code ; Function Call
import java . util . * ; class GFG { static void findSum ( int a , int b , int n ) { int sum = 0 ; int x = a ^ b ; if ( n % 3 == 0 ) { sum = ( n / 3 ) * ( a + b + x ) ; } else if ( n % 3 == 1 ) { sum = ( n / 3 ) * ( a + b + x ) + a ; } else { sum = ( n / 3 ) * ( a + b + x ) + a + b ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , N = 8 ; findSum ( a , b , N ) ; } }
Length of the longest subarray whose Bitwise XOR is K | Java program to implement the above approach ; Function to find the length of the longest subarray whose bitwise XOR is equal to K ; Stores prefix XOR of the array ; Stores length of longest subarray having bitwise XOR equal to K ; Stores index of prefix XOR of the array ; Insert 0 into the map ; Traverse the array ; Update prefixXOR ; If ( prefixXOR ^ K ) present in the map ; Update maxLen ; If prefixXOR not present in the Map ; Insert prefixXOR into the map ; Driver Code
import java . util . * ; class GFG { static int LongestLenXORK ( int arr [ ] , int N , int K ) { int prefixXOR = 0 ; int maxLen = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . containsKey ( prefixXOR ^ K ) ) { maxLen = Math . max ( maxLen , ( i - mp . get ( prefixXOR ^ K ) ) ) ; } if ( ! mp . containsKey ( prefixXOR ) ) { mp . put ( prefixXOR , i ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 7 , 2 } ; int N = arr . length ; int K = 1 ; System . out . print ( LongestLenXORK ( arr , N , K ) ) ; } }
Two Dimensional Difference Array | Java program to implement the above approach ; Function to initialize the difference array ; Function to add k to the specified submatrix ( r1 , c1 ) to ( r2 , c2 ) ; Function to print the modified array ; Function to perform the given queries ; Difference array ; Function to initialize the difference array ; Count of queries ; Perform Queries ; Driver Code ; Given Matrix ; Given Queries
import java . util . * ; class GFG { static int N = 3 ; static int M = 3 ; static void intializeDiff ( int D [ ] [ ] , int A [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { D [ i ] [ 0 ] = A [ i ] [ 0 ] ; D [ i ] [ M ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) D [ i ] [ j ] = A [ i ] [ j ] - A [ i ] [ j - 1 ] ; } } static void update ( int D [ ] [ ] , int k , int r1 , int c1 , int r2 , int c2 ) { for ( int i = r1 ; i <= r2 ; i ++ ) { D [ i ] [ c1 ] += k ; D [ i ] [ c2 + 1 ] -= k ; } } static void printArray ( int A [ ] [ ] , int D [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( j == 0 ) A [ i ] [ j ] = D [ i ] [ j ] ; else A [ i ] [ j ] = D [ i ] [ j ] + A [ i ] [ j - 1 ] ; System . out . print ( A [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } static void performQueries ( int A [ ] [ ] , Vector < Vector < Integer > > Queries ) { int D [ ] [ ] = new int [ N ] [ M + 1 ] ; intializeDiff ( D , A ) ; int Q = Queries . size ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { update ( D , Queries . get ( i ) . get ( 0 ) , Queries . get ( i ) . get ( 1 ) , Queries . get ( i ) . get ( 2 ) , Queries . get ( i ) . get ( 3 ) , Queries . get ( i ) . get ( 4 ) ) ; } printArray ( A , D ) ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 1 , 0 } , { 4 , - 2 , 2 } } ; Vector < Vector < Integer > > Queries = new Vector < Vector < Integer > > ( ) ; Vector < Integer > list1 = new Vector < Integer > ( ) ; list1 . add ( 2 ) ; list1 . add ( 0 ) ; list1 . add ( 0 ) ; list1 . add ( 1 ) ; list1 . add ( 1 ) ; Vector < Integer > list2 = new Vector < Integer > ( ) ; list2 . add ( - 1 ) ; list2 . add ( 1 ) ; list2 . add ( 0 ) ; list2 . add ( 2 ) ; list2 . add ( 2 ) ; Queries . add ( list1 ) ; Queries . add ( list2 ) ; performQueries ( A , Queries ) ; } }
Minimum value to be added to maximize Bitwise XOR of the given array | Java program for the above approach ; Function to find complement of an integer ; Count the number of bits of maxElement ; Return 1 's complement ; Function to find the value required to be added to maximize XOR of the given array ; Stores the required value to be added to the array ; Stores the maximum array element ; Traverse the array ; Update XOR of the array ; Find maximum element in array ; Calculate 1 s ' complement ; Return the answer ; Driver Code
import java . util . * ; import java . io . * ; import java . lang . * ; class GFG { static int onesComplement ( int n , int maxElement ) { int bits = ( int ) Math . floor ( ( Math . log ( maxElement ) / Math . log ( 2 ) ) ) + 1 ; return ( ( 1 << bits ) - 1 ) ^ n ; } static int findNumber ( int arr [ ] , int n ) { int res = 0 ; int maxElement = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res = res ^ arr [ i ] ; if ( maxElement < arr [ i ] ) maxElement = arr [ i ] ; } res = onesComplement ( res , maxElement ) ; return ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . print ( findNumber ( arr , N ) ) ; } }