Advertisement
Guest User

Untitled

a guest
Aug 4th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 100001
  4. int a[MAX];
  5. int n,i,q,k,j,result,temp1,temp2;
  6. int findPairs() {
  7.     j=0;
  8.     result=0;
  9.     temp1=INT_MIN;
  10.     temp2=INT_MIN;
  11.        for(int i=1;i<n;){
  12.             int diff = a[i]-a[j];
  13.             if(i==j)
  14.                 ++i;
  15.              else if(diff==k)
  16.             {
  17.                 if(temp1!=a[i]||temp2!=a[j])
  18.                 {
  19.                    temp1 = a[i];
  20.                    temp2 = a[j];
  21.                    ++result;
  22.                 }
  23.                 ++i;
  24.                
  25.             }
  26.                
  27.            
  28.             else if(diff<k)
  29.                 ++i;
  30.            
  31.             else ++j;
  32.         }
  33.         return result;
  34. }
  35. int main(){
  36.     ios::sync_with_stdio(0);
  37.     cin.tie(NULL);
  38.     cout.tie(NULL);
  39.     cin>>n;
  40.     for(i=0;i<n;++i){
  41.         cin>>a[i];
  42.     }
  43.     sort(a,a+n);
  44.     cin>>q;
  45.     for(long long i=0;i<q;++i){
  46.         cin>>k;
  47.         cout<<findPairs()<<'\n';
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement