RainX_69

OA question asked in a company | Count number of pairs query | MUST DO | OA | Medium

May 13th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | Source Code | 0 0
  1. Given A list of Pairs {L,R}
  2. An array
  3. Find out the total number of pairs (i, j) where(A[ i ], A[ j ]) should be equal to exactly one of the given Q pairs. And i < j.
  4. Example
  5. Pairs - [[4,10],[10,5],[5,4]]
  6. Array - [1,4,5,10,1,10,5,4]
  7.  
  8. Answer -
  9. 6
  10. 4,10
  11. 4,10
  12. 10,5
  13. 10,5
  14. 5,4
  15. 5,4
  16.  
  17. pairs.length = 10^5
  18. array.lenght = 10^7
  19. max (A[i]) = 10^9
  20.  
  21. ---------------------------------------------------------------------------------------------------------------------------------------
  22. BRUTE FORCE
  23.  
  24. #include  <bits/stdc++.h>
  25. using namespace std;
  26.  
  27. void solve(vector<vector<int>> &pairs, vector<int> &arr){
  28.     unordered_map<int,unordered_map<int,int>> pps;
  29.     unordered_map<int,int> running;
  30.     for(int i=arr.size()-1;i>=0;i--){
  31.         for(auto m: running){
  32.             pps[arr[i]][m.first]+=m.second;
  33.         }
  34.         running[arr[i]]++;
  35.     }
  36.     int res=0;
  37.     for(auto q: pairs){
  38.         cout<<"{"<<q[0]<<","<<q[1]<<"} -> ";
  39.         if(pps.find(q[0])==pps.end()){
  40.             cout<<0<<endl;
  41.         }
  42.         else{
  43.             cout<<pps[q[0]][q[1]]<<endl;
  44.             res+=pps[q[0]][q[1]];
  45.         }
  46.     }
  47.     cout<<"res= "<<res<<endl;
  48. }
  49.  
  50. int main() {
  51.    
  52.     // vector<vector<int>> pairs = {{4,10},{10,5},{5,4}};
  53.     // vector<int> arr = {1,4,5,10,1,10,5,4};
  54.     // solve(pairs,arr);
  55.    
  56.     vector<vector<int>> pairs = {{4,3},{3,4},{4,4},{3,3},{1,4},{1,3},{4,0}};
  57.     vector<int> arr = {4,3,4,3,1,3,3,3,4,3,4};
  58.     solve(pairs,arr);
  59.    
  60.     return 0;
  61. }
  62. TC - O(N*N)
  63. Each query can be answered in O(1) time. Though, I doubt it will pass. We can also do it in Nq time, which is more of the right approach considering the constraints.
  64.  
  65. ------------------------------------------------------------------------------------------------------------------------------------
  66.  
  67. Time : O(N*q)
  68. Space : O(1)
  69.  
  70. maintains an ans variable
  71. for every query (consider the values as left and right)
  72. traverse the array and keep a counter for left value , initalize to zero .
  73. if the element is left increment the counter
  74. if element is right add the counter to ans
  75.  
  76.  
  77.  
  78.  
  79. P.S. THIS IS NOT THE PERFECT ANSWER YET. I AM STILL WORKING ON IT USING SEGMENT TREE/ SQRT DECOMPOSITION ETC. IF YOU ARE READING THIS, PLEASE WAIT
  80.  
  81.  
  82. The main idea of my solution is sqrt-decomposition.
  83. 0. Remove duplicates from the list of (L, R) pairs. This way, if (A[i], A[j]) is equal to at least one pair then it is equal to exactly one pair.
  84.  
  85. For each R store a set of paired Ls. I call such set small if its size is <= sqrt(q) and large otherwise. Note that there are at most sqrt(q) large sets.
  86. Scan through the array A and keep track of already encountered values. When looking at A[j], you want to count A[i] that are in the set paired to R.
  87. 3.1. If the set of Ls paired to R is small then you can process them in O(sqrt(q)). n such steps will take O(n sqrt(q)) total which is acceptable.
  88. 3.2. If the set of Ls paired to R is large then lookup the answer (computed in the next step).
  89. 3.3. When looking at A[j], loop through all large sets to which it belongs, and record the fact that you've seen it. There are at most sqrt(q) large sets, so n such steps will take O(n sqrt(q)) total which is acceptable.
  90. Update: fixed C++ code (no I/O, no dedup)
  91.  
  92. [#include](https://leetcode.com/problems/minimum-interval-to-include-each-query) <bits/stdc++.h>
  93. using namespace std;
  94.  
  95. constexpr int SQRT = 300;  // other values may work faster.
  96.  
  97. int64_t count_pairs(const vector<pair<int, int>>& lr, const vector<int>& a) {
  98.  map<int, vector<int>> r2ls;  // unordered_map may work faster
  99.  for (const auto& [l, r] : lr) {
  100.    r2ls[r].push_back(l);
  101.  }
  102.  
  103.  map<int, vector<int>> l2rs;  // unordered_map may work faster
  104.  for (const auto& [l, r] : lr) {
  105.    if (r2ls[r].size() > SQRT) {
  106.      l2rs[l].push_back(r);
  107.    }
  108.  }
  109.  
  110.  int64_t answer = 0;
  111.  map<int, int> small, large;  // unordered_map may work faster
  112.  
  113.  for (const auto ak : a) {
  114.    if (r2ls[ak].size() <= SQRT) {
  115.      for (const auto l : r2ls[ak]) {
  116.        answer += small[l];
  117.      }
  118.    } else {
  119.      answer += large[ak];
  120.    }
  121.  
  122.    ++small[ak];
  123.    for (const auto r : l2rs[ak]) {
  124.      ++large[r];
  125.    }
  126.  }
  127.  return answer;
  128. }
  129.  
  130. int main() {
  131.  {
  132.    vector<pair<int, int>> lr{{4, 10}, {10, 5}, {5, 4}};
  133.    vector<int> a{1, 4, 5, 10, 1, 10, 5, 4};
  134.    cout << count_pairs(lr, a) << '\n';  // 6
  135.  }
  136.  
  137.  {
  138.    vector<pair<int, int>> lr{{1, 3}};
  139.    vector<int> a1{1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1,
  140.                   1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3};
  141.    cout << count_pairs(lr, a1) << '\n';  // 46
  142.    vector<int> a2{1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3,
  143.                   3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
  144.    cout << count_pairs(lr, a2) << '\n';  // 39
  145.  }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment