1549. Strongest AND Team
A coach rates each athlete with a skill code in the array nums. A team of exactly k athletes has team power equal to the bitwise AND of its members' codes. Return the maximum team power over all ways to choose k athletes (any positions, not necessarily neighbours).
Trying all subsets is hopeless. Instead build the answer from the highest bit downward: tentatively add a bit to the current answer and keep it if at least k athletes have all of the answer's bits set. Each decision only needs one scan of the array.
Required complexity: O(30 * n) time and O(1) extra space. Return the power as an int.
Example 1
- Input:
- nums = [12,10,9,15], k = 2
- Output:
- 12
- Explanation:
The best pair is 12 and 15, whose AND is 12; no other pair does better.
Example 2
- Input:
- nums = [6,5,3], k = 3
- Output:
- 0
- Explanation:
All three must be chosen and 6&5&3 equals 0.
Constraints
1 ≤ k ≤ n ≤ 105
0 ≤ nums[i] ≤ 230 - 1
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n log M)
- Space
- O(1)