Spoj HACKRNDM - Hacking the random number generator Solution

 Solution in C++:

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh    

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n,m,i,k;
  6. cin>>n>>k;
  7. int a[n+1];
  8. for(i=0; i<n; i++)
  9. cin>>a[i];
  10.  
  11. sort(a,a+n);
  12. int c=0;
  13. for(i=0; i<n; i++)
  14. {
  15. int l=0,u=n-1,f=0,m;
  16. while(l<=u)
  17. {
  18. m=(l+u)/2;
  19. if(a[m]==a[i]+k)
  20. {
  21. c++;
  22. break;
  23. }
  24. else if(a[m]>k+a[i])
  25. u=m-1;
  26. else
  27. l=m+1;
  28. }
  29. }
  30. cout<<c<<endl;
  31. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.