Find the K-or of an Array - Problem
You are given an integer array nums and an integer k. Your task is to compute the K-or of the array, which is a variation of the standard bitwise OR operation.
In the K-or operation, a bit position in the result is set to 1 if and only if at least k numbers in the array have a 1 in that same bit position. Otherwise, the bit is set to 0.
Example: If nums = [7, 12, 9, 8, 9, 15] and k = 4, we need to find which bit positions have at least 4 numbers with a 1 bit. The binary representations are:
- 7 → 0111
- 12 → 1100
- 9 → 1001
- 8 → 1000
- 9 → 1001
- 15 → 1111
Count the 1s in each position and keep only positions where the count ≥ k.
Input & Output
example_1.py — Basic K-or Operation
$
Input:
nums = [7, 12, 9, 8, 9, 15], k = 4
›
Output:
9
💡 Note:
Binary representations: 7→0111, 12→1100, 9→1001, 8→1000, 9→1001, 15→1111. Bit 0 appears in 4 numbers (7,9,9,15), bit 3 appears in 4 numbers (12,9,8,15). Result: 1001₂ = 9
example_2.py — High Threshold
$
Input:
nums = [2, 12, 1, 3, 4], k = 6
›
Output:
0
💡 Note:
No bit position appears in 6 or more numbers since we only have 5 numbers total. Result is 0.
example_3.py — All Bits Set
$
Input:
nums = [15, 15, 15], k = 2
›
Output:
15
💡 Note:
15 in binary is 1111. All bit positions (0,1,2,3) appear in at least 2 numbers. Result: 1111₂ = 15
Constraints
- 1 ≤ nums.length ≤ 50
- 0 ≤ nums[i] < 231
- 1 ≤ k ≤ nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Cast Votes
Each number votes 'yes' on proposals corresponding to its set bits
2
Count Votes
Tally votes for each of the 32 proposals
3
Determine Winners
Proposals with ≥ k votes pass and get set in the result
Key Takeaway
🎯 Key Insight: K-or extends regular OR by requiring a minimum vote threshold, making it perfect for finding commonly set bits across multiple numbers.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code