2917. Find the K-or of an Array LeetCode Solution
In this guide, you will get 2917. Find the K-or of an Array LeetCode Solution with the best time and space complexity. The solution to Find the K-or of an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Find the K-or of an Array solution in C++
- Find the K-or of an Array solution in Java
- Find the K-or of an Array solution in Python
- Additional Resources

Problem Statement of Find the K-or of an Array
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 <= 50
0 <= nums[i] < 231
1 <= k <= nums.length
Complexity Analysis
- Time Complexity: O(30n) = O(n)
- Space Complexity: O(n)
2917. Find the K-or of an Array LeetCode Solution in C++
/* code provided by PROGIEZ */
2917. Find the K-or of an Array LeetCode Solution in Java
class Solution {
public int findKOr(int[] nums, int k) {
final int kMaxBit = 30;
int ans = 0;
for (int i = 0; i <= kMaxBit; ++i) {
final int finalI = i;
final int count = (int) Arrays.stream(nums)
.filter(num -> (num >> finalI & 1) == 1) //
.count();
if (count >= k)
ans += Math.pow(2, i);
}
return ans;
}
}
// code provided by PROGIEZ
2917. Find the K-or of an Array LeetCode Solution in Python
class Solution:
def findKOr(self, nums: list[int], k: int) -> int:
kMaxBit = 30
return sum(2**i
for i in range(kMaxBit + 1)
if sum(num >> i & 1 for num in nums) >= k)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.