codeparrot/xlcost-text-to-code huggingface dataset:
Maximum set bit sum in array without considering adjacent elements | Java program to maximum set bit sum in array without considering adjacent elements ; Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int bit ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } static int maxSumOfBits ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = bit ( arr [ i ] ) ; } int incl = arr [ 0 ] ; int excl = 0 ; int excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 } ; int n = arr . length ; System . out . print ( maxSumOfBits ( arr , n ) ) ; } }
Increment a number without using ++ or + | Java program to increment an unsigned char using bitwise operators . ; function that increment the value . ; Invert bits and apply negative sign ; Driver code
class GFG { static char increment ( char i ) { int i1 = - ( ~ ( int ) ( i ) ) ; return ( char ) ( i1 ) ; } public static void main ( String [ ] args ) { char n = ' a ' ; System . out . println ( increment ( n ) ) ; } }
Count pairs with Bitwise XOR as ODD number | Java program to count pairs with XOR giving a odd number ; Function to count number of odd pairs ; find all pairs ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
class GFG { static int findOddPair ( int A [ ] , int N ) { int i , count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) count ++ ; } return count * ( N - count ) ; } public static void main ( String [ ] arg ) { int a [ ] = { 5 , 4 , 7 , 2 , 1 } ; int n = a . length ; System . out . println ( findOddPair ( a , n ) ) ; } }
Bitwise OR ( or | ) of a range | Java Program to find the bitwise OR of all the integers in range L - R ; Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code
import java . io . * ; class GFG { static int MSBPosition ( long N ) { int msb_p = - 1 ; while ( N > 0 ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } static long findBitwiseOR ( long L , long R ) { long res = 0 ; int msb_p1 = MSBPosition ( L ) ; int msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { long res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = Math . max ( msb_p1 , msb_p2 ) ; for ( int i = msb_p1 ; i >= 0 ; i -- ) { long res_val = ( 1 << i ) ; res += res_val ; } return res ; } public static void main ( String [ ] args ) { int L = 12 , R = 18 ; System . out . println ( findBitwiseOR ( L , R ) ) ; } }
Maximize the bitwise OR of an array | Java program to maximize the Bitwise OR Sum in given array ; Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Drivers code
import java . io . * ; class GFG { public static long maxOR ( long arr [ ] , int n , int k , int x ) { long preSum [ ] = new long [ n + 1 ] ; long suffSum [ ] = new long [ n + 1 ] ; long res = 0 , pow = 1 ; for ( int i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , preSum [ i ] | ( arr [ i ] * pow ) suffSum [ i + 1 ] ) ; return res ; } public static void main ( String args [ ] ) { long arr [ ] = { 1 , 2 , 4 , 8 } ; int n = 4 ; int k = 2 , x = 3 ; long ans = maxOR ( arr , n , k , x ) ; System . out . println ( ans ) ; } }
How to turn on a particular bit in a number ? | Java program to turn on a particular bit ; Returns a number that has all bits same as n except the k 'th  bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver program to test above function
class GFG { static int turnOnK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n | ( 1 << ( k - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; int k = 2 ; System . out . print ( turnOnK ( n , k ) ) ; } }
Minimum sum of two numbers formed from digits of an array | Java program to find minimum sum of two numbers formed from all digits in a given array . ; Returns sum of two numbers formed from all digits in a [ ] ; sort the elements ; Driver code
import java . util . Arrays ; public class AQRQ { static int minSum ( int a [ ] , int n ) { Arrays . sort ( a ) ; int num1 = 0 ; int num2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) num1 = num1 * 10 + a [ i ] ; else num2 = num2 * 10 + a [ i ] ; } return num2 + num1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 0 , 7 , 4 } ; int n = arr . length ; System . out . println ( " The ▁ required ▁ sum ▁ is ▁ " + minSum ( arr , n ) ) ; } }
Find value of k | Java program to find k - th bit from right ; Driver Code ; Function Call
import java . io . * ; class GFG { static void printKthBit ( long n , long k ) { System . out . println ( ( ( n & ( 1 << ( k - 1 ) ) ) >> ( k - 1 ) ) ) ; } public static void main ( String [ ] args ) { long n = 13 , k = 2 ; printKthBit ( n , k ) ; } }
Disjoint Set Union on trees | Set 1 | Java code to find maximum subtree such that all nodes are even in weight ; Structure for Edge ; ' id ' : stores parent of a node . ' sz ' : stores size of a DSU tree . ; Function to assign root ; Function to find Union ; Utility function for Union ; Edge between ' u ' and ' v ' ; 0 - indexed nodes ; If weights of both ' u ' and ' v ' are even then we make union of them . ; Function to find maximum size of DSU tree ; Driver code ; Weights of nodes ; Number of nodes in a tree ; Initializing every node as a tree with single node . ; Find maximum size of DSU tree .
class GFG { static int N = 100010 ; static class Edge { int u , v ; public Edge ( int u , int v ) { this . u = u ; this . v = v ; } } static int [ ] id = new int [ N ] ; static int [ ] sz = new int [ N ] ; static int Root ( int idx ) { int i = idx ; while ( i != id [ i ] ) { id [ i ] = id [ id [ i ] ] ; i = id [ i ] ; } return i ; } static void Union ( int a , int b ) { int i = Root ( a ) , j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i ; sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j ; sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } static void UnionUtil ( Edge e [ ] , int W [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int u , v ; u = e [ i ] . u ; v = e [ i ] . v ; u -- ; v -- ; if ( W [ u ] % 2 == 0 && W [ v ] % 2 == 0 ) Union ( u , v ) ; } } static int findMax ( int n , int W [ ] ) { int maxi = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( W [ i ] % 2 == 0 ) maxi = Math . max ( maxi , sz [ i ] ) ; return maxi ; } public static void main ( String [ ] args ) { int W [ ] = { 1 , 2 , 6 , 4 , 2 , 0 , 3 } ; int n = W . length ; for ( int i = 0 ; i < n ; i ++ ) { id [ i ] = i ; sz [ i ] = 1 ; } Edge e [ ] = { new Edge ( 1 , 2 ) , new Edge ( 1 , 3 ) , new Edge ( 2 , 4 ) , new Edge ( 2 , 5 ) , new Edge ( 4 , 6 ) , new Edge ( 6 , 7 ) } ; int q = e . length ; UnionUtil ( e , W , q ) ; int maxi = findMax ( n , W ) ; System . out . printf ( " Maximum ▁ size ▁ of ▁ the ▁ subtree ▁ with ▁ " ) ; System . out . printf ( "even weighted nodes = %dNEW_LINE", maxi); } }
Odd numbers in N | Java code to find the count of odd numbers in n - th row of Pascal 's Triangle ; Function to get no of set bits in binary representation of positive integer n ; Count number of 1 's in binary  representation of n. ; Number of odd numbers in n - th row is 2 raised to power the count . ; Driver code
import java . io . * ; class GFG { static int countSetBits ( int n ) { long count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return ( int ) count ; } static int countOfOddsPascal ( int n ) { int c = countSetBits ( n ) ; return ( int ) Math . pow ( 2 , c ) ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( countOfOddsPascal ( n ) ) ; } }
Queries on XOR of XORs of all subarrays | JAVA Code for Queries on XOR of XORs of all subarrays ; Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driver program to test above function
import java . util . * ; class GFG { static void ansQueries ( int prefeven [ ] , int prefodd [ ] , int l , int r ) { if ( ( r - l + 1 ) % 2 == 0 ) System . out . println ( "0" ) ; else { if ( l % 2 == 0 ) System . out . println ( prefeven [ r ] ^ prefeven [ l - 1 ] ) ; else System . out . println ( prefodd [ r ] ^ prefodd [ l - 1 ] ) ; } } static void wrapper ( int arr [ ] , int n , int l [ ] , int r [ ] , int q ) { int prefodd [ ] = new int [ 100 ] ; int prefeven [ ] = new int [ 100 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i ) % 2 == 0 ) { prefeven [ i ] = arr [ i - 1 ] ^ prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ; } else { prefeven [ i ] = prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ^ arr [ i - 1 ] ; } } int i = 0 ; while ( i != q ) { ansQueries ( prefeven , prefodd , l [ i ] , r [ i ] ) ; i ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int l [ ] = { 1 , 1 , 2 } ; int r [ ] = { 2 , 3 , 4 } ; int q = l . length ; wrapper ( arr , n , l , r , q ) ; } }
Variation in Nim Game | Java program for the variation in nim game ; Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; Driver code ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( res == 0 ) if G is zero ; else if G is non zero
class GFG { static int solve ( int p [ ] , int n ) { int G = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] % 2 != 0 ) G ^= ( p [ i ] + 1 ) ; G ^= ( p [ i ] - 1 ) ; } return G ; } public static void main ( String [ ] args ) { int n = 3 ; int p [ ] = { 32 , 49 , 58 } ; int res = solve ( p , n ) ; System . out . print ( " Player ▁ 2 ▁ wins " ) ; System . out . print ( " Player ▁ 1 ▁ wins " ) ; } }
Maximum AND value of a pair in an array | Java Program to find maximum XOR value of a pair ; Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; driver function
import java . util . * ; import java . lang . * ; public class GfG { static int checkBit ( int pattern , int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int arr [ ] , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } return res ; } public static void main ( String argc [ ] ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = arr . length ; System . out . println ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }
Minimum flips to make all 1 s in left and 0 s in right | Set 1 ( Using Bitmask ) | Java program to find minimum flips to make all 1 s in left ; function to count minimum number of flips ; This is converting string s into integer of base 2 ( if s = '100' then num = 4 ) ; initialize minXor with n that can be maximum number of flips ; right shift 1 by ( n - 1 ) bits ; calculate bitwise XOR of num and mask ; Math . min ( a , b ) returns minimum of a and b return minimum number of flips till that digit ; function to count number of 1 s ; Driver code
import java . io . * ; class GFG { public static int findMiniFlip ( int [ ] nums ) { int n = nums . length ; String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += nums [ i ] ; long num = Integer . parseInt ( s , 2 ) ; int minXor = n ; long mask = ( 1 << ( n - 1 ) ) ; while ( n - 1 > 0 ) { long temp = ( num ^ mask ) ; minXor = Math . min ( minXor , countones ( temp ) ) ; n -- ; mask = ( mask | ( 1 << n - 1 ) ) ; } return minXor ; } public static int countones ( long n ) { int c = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; c ++ ; } return c ; } public static void main ( String [ ] args ) { int [ ] nums = { 1 , 0 , 1 , 1 , 0 , 0 , 0 } ; int n = findMiniFlip ( nums ) ; System . out . println ( n ) ; } }
Check if a number is power of 8 or not | Java program to check if a number is power of 8 ; function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code
class GFG { static boolean checkPowerof8 ( int n ) { double i = Math . log ( n ) / Math . log ( 8 ) ; return ( i - Math . floor ( i ) < 0.000001 ) ; } public static void main ( String args [ ] ) { int n = 65 ; if ( checkPowerof8 ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the n | Java program to find n - th palindrome ; utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary  palindrome so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver code ; Function Call
import java . io . * ; import java . util . * ; class GFG { public static int convertStringToInt ( String s ) { int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) ans += 1 << i ; } return ans ; } public static int getNthNumber ( int n ) { Queue < String > q = new LinkedList < > ( ) ; if ( n == 1 ) return 1 ; n = n - 1 ; q . add ( "11" ) ; while ( n -- > 0 ) { String curr = q . remove ( ) ; if ( n == 0 ) return convertStringToInt ( curr ) ; int len = curr . length ( ) ; if ( len % 2 == 0 ) { q . add ( curr . substring ( 0 , len / 2 ) + "0" + curr . substring ( len / 2 ) ) ; q . add ( curr . substring ( 0 , len / 2 ) + "1" + curr . substring ( len / 2 ) ) ; } else { char midChar = curr . charAt ( len / 2 ) ; q . add ( curr . substring ( 0 , len / 2 ) + midChar + curr . substring ( len / 2 ) ) ; } } return - 1 ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( getNthNumber ( n ) ) ; } }
Check if binary representation of a given number and its complement are anagram | An efficient Java program to check if binary representations of a number and it 's complement are anagram. ; Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code
class GFG { static byte longSize = 8 ; static int ULL_SIZE = 8 * longSize ; static boolean bit_anagram_check ( long a ) { return ( Integer . bitCount ( ( int ) a ) == ( ULL_SIZE >> 1 ) ) ; } public static void main ( String [ ] args ) { long a = 4294967295L ; System . out . println ( bit_anagram_check ( a ) ) ; } }
Sum of numbers with exactly 2 bits set | Java program to find sum of numbers upto n whose 2 bits are set ; To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver program to test findSum ( )
public class Main { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; 1 << i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { int num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }
Position of rightmost different bit | Java implementation to find the position of rightmost different bit in two number . ; function to find rightmost different bit in two numbers . ; Driver code
import java . util . * ; class GFG { static int posOfRightMostDiffBit ( int m , int n ) { return ( int ) Math . floor ( Math . log10 ( Math . pow ( m ^ n , 2 ) ) ) + 2 ; } public static void main ( String [ ] args ) { int m = 52 , n = 4 ; System . out . println ( " Position ▁ = ▁ " + posOfRightMostDiffBit ( m , n ) ) ; } }
Set the K | Java implementation to set the kth bit of the given number ; function to set the kth bit ; kth bit of n is being set by this operation ; Driver code
class GFG { static int setKthBit ( int n , int k ) { return ( ( 1 << k ) n ) ; } public static void main ( String arg [ ] ) { int n = 10 , k = 2 ; System . out . print ( " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " + setKthBit ( n , k ) ) ; } }
Reverse an array without using subtract sign â €˜ | Java program to reverse an array without using " - " sign ; Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array
class GFG { static void reverseArray ( int arr [ ] , int n ) { int x = ( Integer . MIN_VALUE / Integer . MAX_VALUE ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr , i , n + ( x * i ) + x ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = arr . length ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
Reverse an array without using subtract sign â €˜ | Java program to reverse an array without using " - " sign ; Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + ~ 1 + 1 ; Driver code ; print the reversed array
import java . util . Arrays ; class GFG { static void reverseArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { swap ( arr , i , ( n + ~ i + 1 ) + ~ 1 + 1 ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = arr . length ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
Maximum XOR value of a pair from a range | Java program to get maximum xor value of two numbers in a range ; method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; main function
class Xor { static int maxXORInRange ( int L , int R ) { int LXR = L ^ R ; int msbPos = 0 ; while ( LXR > 0 ) { msbPos ++ ; LXR >>= 1 ; } int maxXOR = 0 ; int two = 1 ; while ( msbPos -- > 0 ) { maxXOR += two ; two <<= 1 ; } return maxXOR ; } public static void main ( String [ ] args ) { int L = 8 ; int R = 20 ; System . out . println ( maxXORInRange ( L , R ) ) ; } }
Numbers whose bitwise OR and sum with N are equal | Java program to count numbers whose bitwise OR and sum with N are equal ; Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code
class GFG { static int CountZeroBit ( int n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) != 0 ) count ++ ; n >>= 1 ; } return count ; } static int CountORandSumEqual ( int N ) { int count = CountZeroBit ( N ) ; return ( 1 << count ) ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( CountORandSumEqual ( N ) ) ; } }
Count smaller numbers whose XOR with n produces greater value | Java program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; Traverse bits from LSB ( least significant bit ) to MSB ; Initialize result ; If the current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
import java . lang . * ; class GFG { static int countNumbers ( int n ) { int k = 0 ; int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += ( int ) ( Math . pow ( 2 , k ) ) ; k += 1 ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . println ( countNumbers ( n ) ) ; } }
Count all pairs with given XOR | Java program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; Driver code
import java . util . * ; class GFG { static int xorPairCount ( int arr [ ] , int n , int x ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( x ^ arr [ i ] ) && ( x ^ arr [ i ] ) == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { result ++ ; } s . add ( arr [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 10 , 15 , 7 , 6 } ; int n = arr . length ; int x = 5 ; System . out . print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " + xorPairCount ( arr , n , x ) ) ; } }
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Driver method ; Printing multiples of 4 using above method
class Test { static boolean isMultipleOf4 ( int n ) { if ( n == 1 ) return false ; int XOR = 0 ; for ( int i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } public static void main ( String [ ] args ) { for ( int n = 0 ; n <= 42 ; n ++ ) System . out . print ( isMultipleOf4 ( n ) ? n : " ▁ " ) ; } }
Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Driver method ; Printing multiples of 4 using above method
class Test { static boolean isMultipleOf4 ( long n ) { if ( n == 0 ) return true ; return ( ( ( n >> 2 ) << 2 ) == n ) ; } public static void main ( String [ ] args ) { for ( int n = 0 ; n <= 42 ; n ++ ) System . out . print ( isMultipleOf4 ( n ) ? n : " ▁ " ) ; } }