-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_32.java
More file actions
58 lines (51 loc) · 1.51 KB
/
Copy pathDay_32.java
File metadata and controls
58 lines (51 loc) · 1.51 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Leetcode 3095 Shortest Subarray With OR at Least K I
class Solution {
public int minimumSubarrayLength(int[] nums, int k) {
int minlen = Integer.MAX_VALUE, n = nums.length;
for(int i=0;i<n;i++){
int or = 0;
for(int j=i;j<n;j++){
or|=nums[j];
if(or >= k){
minlen = Math.min(minlen,j-i+1);
}
}
}
return (minlen != Integer.MAX_VALUE) ? minlen : -1;
}
}
// Count the Number of Beautiful Subarrays
class Solution {
public long beautifulSubarrays(int[] nums) {
int n = nums.length,prexor = 0;
long cnt = 0;
Map<Integer,Integer> mpp = new HashMap<>();
mpp.put(0,1);
for(int i=0;i<n;i++){
prexor = prexor ^ nums[i];
if(mpp.containsKey(prexor)) cnt += mpp.get(prexor);
mpp.put(prexor,mpp.getOrDefault(prexor,0)+1);
}
return cnt;
}
}
//Maximum XOR for Each Query
class Solution {
public int[] getMaximumXor(int[] nums, int maxBit) {
int n = nums.length, maxxor = (int)Math.pow(2,maxBit)-1;
int[] prexor = new int[n];
prexor[0] = nums[0];
for(int i=1;i<n;i++){
prexor[i] = prexor[i-1] ^ nums[i];
}
int i = 0, j = n-1;
while(i<=j){
int temp = prexor[i];
prexor[i] = prexor[j] ^ maxxor;
prexor[j] = temp ^ maxxor;
i++;
j--;
}
return prexor;
}
}