2917. Find the K-or of an Array Easy
1/**
2 * [2917] Find the K-or of an Array
3 *
4 * 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.
5 * Return the K-or of nums.
6 *
7 * <strong class="example">Example 1:
8 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
9 * Input:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [7,12,9,8,9,15], k = 4 </span>
10 * Output:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 9 </span>
11 * Explanation:
12 * Represent numbers in binary:
13 * <table style="text-indent:10px; margin-bottom=20px;">
14 * <tbody>
15 * <tr>
16 * <th>Number</th>
17 * <th>Bit 3</th>
18 * <th>Bit 2</th>
19 * <th>Bit 1</th>
20 * <th>Bit 0</th>
21 * </tr>
22 * <tr>
23 * <td>7</td>
24 * <td>0</td>
25 * <td>1</td>
26 * <td>1</td>
27 * <td>1</td>
28 * </tr>
29 * <tr>
30 * <td>12</td>
31 * <td>1</td>
32 * <td>1</td>
33 * <td>0</td>
34 * <td>0</td>
35 * </tr>
36 * <tr>
37 * <td>9</td>
38 * <td>1</td>
39 * <td>0</td>
40 * <td>0</td>
41 * <td>1</td>
42 * </tr>
43 * <tr>
44 * <td>8</td>
45 * <td>1</td>
46 * <td>0</td>
47 * <td>0</td>
48 * <td>0</td>
49 * </tr>
50 * <tr>
51 * <td>9</td>
52 * <td>1</td>
53 * <td>0</td>
54 * <td>0</td>
55 * <td>1</td>
56 * </tr>
57 * <tr>
58 * <td>15</td>
59 * <td>1</td>
60 * <td>1</td>
61 * <td>1</td>
62 * <td>1</td>
63 * </tr>
64 * <tr>
65 * <td>Result = 9</td>
66 * <td>1</td>
67 * <td>0</td>
68 * <td>0</td>
69 * <td>1</td>
70 * </tr>
71 * </tbody>
72 * </table>
73 * Bit 0 is set in 7, 9, 9, and 15. Bit 3 is set in 12, 9, 8, 9, and 15.<br />
74 * Only bits 0 and 3 qualify. The result is (1001)2 = 9.
75 * </div>
76 * <strong class="example">Example 2:
77 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
78 * Input:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [2,12,1,11,4,5], k = 6 </span>
79 * Output:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 0 </span>
80 * Explanation: No bit appears as 1 in all six array numbers, as required for K-or with k = 6. Thus, the result is 0.
81 * </div>
82 * <strong class="example">Example 3:
83 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
84 * Input:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [10,8,5,9,11,6,8], k = 1 </span>
85 * Output:<span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 15 </span>
86 * 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.
87 * </div>
88 *
89 * Constraints:
90 *
91 * 1 <= nums.length <= 50
92 * 0 <= nums[i] < 2^31
93 * 1 <= k <= nums.length
94 *
95 */
96pub struct Solution {}
97
98// problem: https://leetcode.com/problems/find-the-k-or-of-an-array/
99// discuss: https://leetcode.com/problems/find-the-k-or-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
100
101// submission codes start here
102
103impl Solution {
104 pub fn find_k_or(nums: Vec<i32>, k: i32) -> i32 {
105 0
106 }
107}
108
109// submission codes end
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn test_2917() {
117 }
118}
119
Back
© 2025 bowen.ge All Rights Reserved.