Advertisement
Guest User

pairs in arrays elements

a guest
Sep 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <iterator>
  3. #include <map>
  4. using namespace std;
  5.  
  6. int countFreq(int ar[], int n)
  7. {
  8.  
  9. vector<bool> visited(n, false);
  10.  
  11. int ans;
  12. int div, result=0;
  13.  
  14. map<int, int> mp;
  15.  
  16.  
  17. for (int i = 0; i < n; i++) {
  18.  
  19. if (visited[i] == true)
  20. continue;
  21.  
  22. int count = 1;
  23. for (int j = i + 1; j < n; j++) {
  24. if (ar[i] == ar[j]) {
  25. visited[j] = true;
  26. count++;
  27. }
  28. }
  29. mp.insert({ ar[i] , count });
  30.  
  31.  
  32. }
  33.  
  34. map<int, int>::iterator itr;
  35. for (itr = mp.begin(); itr != mp.end(); ++itr) {
  36. ans = itr->first;
  37. if(ans>=2)
  38. {
  39. div = ans/2;
  40. }
  41. result = result + div;
  42. }
  43.  
  44. return result;
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50. int pairs;
  51. int ar[] = { 10, 20, 20, 10, 10, 20, 5, 20 };
  52. int n = sizeof(ar) / sizeof(ar[0]);
  53. pairs = countFreq(ar, n);
  54. cout<<pairs;
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement