193030

HackerRank set vector

Jun 20th, 2021
838
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <set>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. int sockMerchant(int n, vector<int> ar) {
  10.     vector<int> vect;
  11.     int counter = 0;
  12.     for(int i =0; i <n ; i++)
  13.     {
  14.         //cout << i << " " << ar[i] << endl;
  15.         /*
  16.         if(count(vect.begin(), vect.end(), ar[i]) ==2)
  17.         {
  18.             counter++;
  19.  
  20.         }
  21.         */
  22.             vect.push_back(ar[i]);
  23.        
  24.     }
  25.     set<int> s1;
  26.     for(int i =0; i <n ; i++)
  27.     {
  28.            int number =  vect[i];
  29.            int times = count(vect.begin(), vect.end(), ar[i]);
  30.            cout << i << " " << number << " " << times << " " << counter <<endl;
  31.             if(count(s1.begin(), s1.end(), ar[i]) ==0)
  32.             {
  33.                    counter += times/2;
  34.  
  35.             }
  36.             s1.insert(vect[i]);
  37.  
  38.     }
  39.    
  40.     return counter;
  41. }
  42.  
  43. int main()
  44. {
  45.  /* vector <int> v1 { 10, 20, 20, 10, 10, 30, 50, 10, 20 };
  46.  int res =   sockMerchant(9, v1);.
  47.  */
  48.  sockMerchant(
  49.   cout << res << endl;
  50. }
  51.  
RAW Paste Data