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
K-or Problem: Bitwise Voting with ThresholdInput Array[7, 12, 9, 8, 9, 15], k=4Count BitsBit 0: 4 votes, Bit 3: 4 votesResult9 (1001₂)Binary Analysis:7 = 0111, 12 = 1100, 9 = 10018 = 1000, 9 = 1001, 15 = 1111Threshold Check (k=4):✓ Bit 0: appears in [7,9,9,15] = 4 numbers✗ Bit 1: appears in [7,12] = 2 numbers✗ Bit 2: appears in [7,12] = 2 numbers✓ Bit 3: appears in [8,9,9,12] = 4 numbers
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
Asked in
Amazon 15 Microsoft 12
8.9K Views
Medium Frequency
~15 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen