Find the K-or of an Array - Problem
You are given an integer array nums and an integer k.
Let's introduce K-or operation by extending the standard bitwise OR. In K-or, a bit position in the result is set to 1 if at least k numbers in nums have a 1 in that position.
Return the K-or of nums.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [7,12,9,8,9,15], k = 4
›
Output:
9
💡 Note:
Binary: 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 (8,9,9,12). Result = 1001₂ = 9
Example 2 — Higher Threshold
$
Input:
nums = [2,12,1,3,4,5], k = 3
›
Output:
0
💡 Note:
No bit position appears in 3 or more numbers, so K-or result is 0
Example 3 — Low Threshold
$
Input:
nums = [10,8,5,9,11,6,8], k = 2
›
Output:
15
💡 Note:
Multiple bit positions have count ≥ 2. Result combines all qualifying bits to get 15
Constraints
- 1 ≤ nums.length ≤ 50
- 0 ≤ nums[i] < 231
- 1 ≤ k ≤ nums.length
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [7,12,9,8,9,15] and threshold k=4
2
Count Bits
Count occurrences of each bit position
3
Apply Threshold
Include only bits with count ≥ k
Key Takeaway
🎯 Key Insight: K-or is like democratic voting - each bit position needs at least k votes to win
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code