forked from vns9/binarysearch.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBounded Square Sums.cpp
More file actions
43 lines (42 loc) · 917 Bytes
/
Bounded Square Sums.cpp
File metadata and controls
43 lines (42 loc) · 917 Bytes
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
// https://binarysearch.com/problems/Bounded-Square-Sums
int helper(int lower, int upper, vector<int>& v){
int n=v.size();
if(n==0) return 0;
int p1=-1;
int p2=-1;
int lo=0;
int hi=n-1;
while(lo<=hi){
int mid=lo+(hi-lo)/2;
if(v[mid]*v[mid]<lower){
lo=mid+1;
}
else{
p1=mid;
hi=mid-1;
}
}
lo=0;
hi=n-1;
while(lo<=hi){
int mid=lo+(hi-lo)/2;
if(v[mid]*v[mid]>upper){
hi=mid-1;
}
else{
p2=mid;
lo=mid+1;
}
}
if(p1==-1||p2==-1) return 0;
return p2-p1+1;
}
int solve(vector<int>& a, vector<int>& b, int lower, int upper) {
int ans=0;
for(int i=0;i<b.size();i++) b[i]=abs(b[i]);
sort(b.begin(),b.end());
for(auto val:a){
ans+=helper(lower-val*val, upper-val*val, b);
}
return ans;
}