Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [7,12,9,8,9,15], k = 4
Output: 9
Explanation:
Represent numbers in binary:
| Number | Bit 3 | Bit 2 | Bit 1 | Bit 0 |
|---|---|---|---|---|
| 7 | 0 | 1 | 1 | 1 |
| 12 | 1 | 1 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 |
| 8 | 1 | 0 | 0 | 0 |
| 9 | 1 | 0 | 0 | 1 |
| 15 | 1 | 1 | 1 | 1 |
| Result = 9 | 1 | 0 | 0 | 1 |
Bit 0 is set in 7, 9, 9, and 15. Bit 3 is set in 12, 9, 8, 9, and 15.
Only bits 0 and 3 qualify. The result is (1001)2 = 9.
Example 2:
Input: nums = [2,12,1,11,4,5], k = 6
Output: 0
Explanation: No bit appears as 1 in all six array numbers, as required for K-or with k = 6. Thus, the result is 0.
Example 3:
Input: nums = [10,8,5,9,11,6,8], k = 1
Output: 15
Explanation: Since k == 1, the 1-or of the array is equal to the bitwise OR of all its elements. Hence, the answer is 10 OR 8 OR 5 OR 9 OR 11 OR 6 OR 8 = 15.
Constraints:
1 <= nums.length <= 500 <= nums[i] < 2311 <= k <= nums.lengthProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Bit Manipulation
[7,12,9,8,9,15] 4
counting-bits)sum-of-values-at-indices-with-k-set-bits)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2917: Find the K-or of an Array
class Solution {
public int findKOr(int[] nums, int k) {
int ans = 0;
for (int i = 0; i < 32; ++i) {
int cnt = 0;
for (int x : nums) {
cnt += (x >> i & 1);
}
if (cnt >= k) {
ans |= 1 << i;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2917: Find the K-or of an Array
func findKOr(nums []int, k int) (ans int) {
for i := 0; i < 32; i++ {
cnt := 0
for _, x := range nums {
cnt += (x >> i & 1)
}
if cnt >= k {
ans |= 1 << i
}
}
return
}
# Accepted solution for LeetCode #2917: Find the K-or of an Array
class Solution:
def findKOr(self, nums: List[int], k: int) -> int:
ans = 0
for i in range(32):
cnt = sum(x >> i & 1 for x in nums)
if cnt >= k:
ans |= 1 << i
return ans
// Accepted solution for LeetCode #2917: Find the K-or of an Array
fn find_k_or(nums: Vec<i32>, k: i32) -> i32 {
let mut ret = 0;
for i in 0..32 {
let bit = 1 << i;
let count = nums.iter().filter(|&num| num & bit != 0).count();
if count >= k as usize {
ret += bit;
}
}
ret
}
fn main() {
let nums = vec![7, 12, 9, 8, 9, 15];
let ret = find_k_or(nums, 4);
println!("ret={ret}");
}
#[test]
fn test_find_k_or() {
{
let nums = vec![7, 12, 9, 8, 9, 15];
let ret = find_k_or(nums, 4);
assert_eq!(ret, 9);
}
{
let nums = vec![2, 12, 1, 11, 4, 5];
let ret = find_k_or(nums, 6);
assert_eq!(ret, 0);
}
{
let nums = vec![10, 8, 5, 9, 11, 6, 8];
let ret = find_k_or(nums, 1);
assert_eq!(ret, 15);
}
}
// Accepted solution for LeetCode #2917: Find the K-or of an Array
function findKOr(nums: number[], k: number): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
let cnt = 0;
for (const x of nums) {
cnt += (x >> i) & 1;
}
if (cnt >= k) {
ans |= 1 << i;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.