Hello Community!
So problem was like this —
find x in such way that after performing Xor operation with each element of given array the sum of array is maximized and if the there are more than one x which is giving the maximum array sum (after operation) then we have to return the minimum x.
x can be upto 2^63.
input
1
5
10 12 5 7 19
output
92233720368…(some 18 digit number, i don’t remember)
How can i solve this?
thanks for replying! but can you explain what you are doing in your code?
first of all i have checked that how many numbers out of n have their ith bit set .
if more than or equal to half of the numbers have their ith bit set then the ith bit of our ‘X’ will be 0 else the ith bit of our ‘X’ will be 1.
I hope u got it know ,still not dry run the code or ask me again …!!!
2 Likes
You actually have to use equal to here too because if half the numbers have ith bit set then we have to take minimun ‘X’.
1 Like
Very nice explanation . Thanks.
1 Like
can we also do this like, first find the sum of array element then set the 0 bit to one and one bit to 0 that will be our X…
No we can’t do like this .
eg let’s take 3 numbers 1,2 ,4
1 -> 0 0 1
2 -> 0 1 0
4 -> 1 0 0
s -> 1 1 1 (sum of 1,2,4)
according to you the value of all these bits will be ‘0’ but all these will be ‘1’ in the ‘X’.
hope it helps …!!!
2 Likes
code_25
October 25, 2019, 5:02pm
11
Thanks for replying, what is the value of x in this case…
code_25
October 25, 2019, 5:29pm
12
and what is the value of maximum sum in this case
As all the bits will be set to 1 in this case , so value of ‘X’ will be 2^63-1.
1 Like