-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.java
More file actions
37 lines (31 loc) · 1.04 KB
/
solution.java
File metadata and controls
37 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Solution for LeetCode Problem #2411: Smallest Subarrays With Maximum Bitwise OR
// Date: 2025-07-29
// Difficulty: Medium
// Language: Java
class Solution {
public int[] smallestSubarrays(int[] nums) {
int n = nums.length;
int[] result = new int[n];
// For each starting position
for (int i = 0; i < n; i++) {
int currentOR = 0;
int minLength = 1;
// Calculate the maximum possible OR value starting from position i
int maxOR = 0;
for (int j = i; j < n; j++) {
maxOR |= nums[j];
}
// Find the smallest subarray that achieves the maximum OR
currentOR = 0;
for (int j = i; j < n; j++) {
currentOR |= nums[j];
if (currentOR == maxOR) {
minLength = j - i + 1;
break;
}
}
result[i] = minLength;
}
return result;
}
}