XOR of all elements of array with set bits equal to K | Java program to find Xor of all elements with set bits equal to K ; Function to find Xor of desired elements ; Initialize vector ; push required elements ; Initialize result with first element of vector ; Driver code
import java . util . * ; class GFG { static int xorGivenSetBits ( int arr [ ] , int n , int k ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Integer . bitCount ( arr [ i ] ) == k ) { v . add ( arr [ i ] ) ; } } int result = v . get ( 0 ) ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { result = result ^ v . get ( i ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 13 , 1 , 19 , 7 } ; int n = arr . length ; int k = 3 ; System . out . println ( xorGivenSetBits ( arr , n , k ) ) ; } }