Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define MAX 100001
- int a[MAX];
- int n,i,q,k,j,result,temp1,temp2;
- int findPairs() {
- j=0;
- result=0;
- temp1=INT_MIN;
- temp2=INT_MIN;
- for(int i=1;i<n;){
- int diff = a[i]-a[j];
- if(i==j)
- ++i;
- else if(diff==k)
- {
- if(temp1!=a[i]||temp2!=a[j])
- {
- temp1 = a[i];
- temp2 = a[j];
- ++result;
- }
- ++i;
- }
- else if(diff<k)
- ++i;
- else ++j;
- }
- return result;
- }
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(NULL);
- cout.tie(NULL);
- cin>>n;
- for(i=0;i<n;++i){
- cin>>a[i];
- }
- sort(a,a+n);
- cin>>q;
- for(long long i=0;i<q;++i){
- cin>>k;
- cout<<findPairs()<<'\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement