hkshakib

Untitled

Jul 28th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int maxCount(int n,int a[])
  4. {
  5. map<int,int> freq;
  6.  
  7. for(int i=0; i<n; ++i)
  8. {
  9. if(freq[a[i]])
  10. freq[a[i]] += 1;
  11. else
  12. freq[a[i]] = 1;
  13. }
  14. int ans = 0, key;
  15.  
  16. map<int,int>:: iterator it=freq.begin();
  17.  
  18. while(it!=freq.end())
  19. {
  20. key = it->first;
  21.  
  22. ++it;
  23.  
  24. if(freq[key+1]!=0)
  25. ans=max(ans,freq[key]+freq[key+1]);
  26.  
  27. }
  28.  
  29. return ans;
  30. }
  31.  
  32. int main()
  33. {
  34. int t;
  35. cin>>t;
  36. while(t--)
  37. {
  38. int n;
  39. cin>>n;
  40. int a[10005];
  41. for(int i=0; i<n; i++)
  42. {
  43. cin>>a[i];
  44. }
  45. cout<<maxCount(n,a)<<endl;
  46. }
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment