Spoj HACKRNDM - Hacking the random number generator Solution
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n,m,i,k;
- cin>>n>>k;
- int a[n+1];
- for(i=0; i<n; i++)
- cin>>a[i];
- sort(a,a+n);
- int c=0;
- for(i=0; i<n; i++)
- {
- int l=0,u=n-1,f=0,m;
- while(l<=u)
- {
- m=(l+u)/2;
- if(a[m]==a[i]+k)
- {
- c++;
- break;
- }
- else if(a[m]>k+a[i])
- u=m-1;
- else
- l=m+1;
- }
- }
- cout<<c<<endl;
- }
No comments