At WaveForm Fest, every attendee wristband is stamped with an access code: a non-negative integer whose bits mark which perks (VIP lounge, backstage pass, free parking, and so on) that wristband unlocks. The organizers want a single combo code to hand to the gate staff that captures exactly the perks popular enough to be worth checking for at the gate: a perk bit should be turned on in the combo code precisely when at least k of the n issued wristbands have that bit turned on. Given the n wristband codes and the threshold k, compute the combo code.
n and k separated by a space.n integers code[1] ... code[n] separated by spaces — the wristband access codes.Print a single integer: the combo code, i.e. the non-negative integer whose bit b is set (for every 0 <= b <= 30) exactly when at least k of the n wristband codes have bit b set.
1 <= n <= 501 <= k <= n0 <= code[i] < 2^31 for every iExample 1
Input
3 2 7 12 9
Expected
13
Explanation
Codes are 7=0111b, 12=1100b, 9=1001b. Bit 0 is set in 7 and 9 (count 2 >= 2) so it's included (+1). Bit 1 is set only in 7 (count 1) so it's excluded. Bit 2 is set in 7 and 12 (count 2 >= 2) so it's included (+4). Bit 3 is set in 12 and 9 (count 2 >= 2) so it's included (+8). Total combo code = 1 + 4 + 8 = 13.
Example 2
Input
3 3 5 5 5
Expected
5
Explanation
All three wristbands carry the identical code 5 (=101b), so every bit set in 5 is set in all 3 >= k = 3 wristbands. The combo code is simply 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →