RainX_69

Number of pairs from two array with kth bit set on XOR

Mar 21st, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | Source Code | 0 0
  1. https://practice.geeksforgeeks.org/contest/job-a-thon-18-hiring-challenge/problems/#
  2.  
  3. Given two arrays A1, A2 both of size N, Q queries. Each query contains 5 elements, [k,l1,l2,r1,r2], where l1, l2 is the 1 based indexed range of A1, and r1, r2 is the 1 based indexed range of A2. Task is to find number of pairs such that A1[i]^A2[j] has the kth bit set, where l1<=i<=l2 and r1<=j<=r2.
  4. Input
  5. A1={1,2,3,4,5}
  6. A2={1,2,3,4,5}
  7. Queries=[[2,2,4,1,3] , [1,1,1,3,5]]
  8. Output
  9. 4 1
  10. Explaination
  11. For 1st query
  12. Segment from A1->{2,3,4}
  13. Segment from A2->{1,2,3}
  14. Possible pairs (arr[i],arr[j])=(2,1), (3,1), (4,2), (4,3) are the only pairs who have kth bit set when pair is XORed.
  15. For 2nd query
  16. Only pair will be (1,4)
  17.  
  18.  
  19. ---------------------------------------------------------------------------------------------------------------------------------------
  20.  
  21. class Solution {
  22.   public:
  23.     vector<long long int> xorPairs(int N, vector<int> &A1, vector<int> &A2, int Q, vector<vector<int>> &query) {
  24.         vector<vector<pair<long long,long long>>> dp1(N,vector<pair<long long,long long>>(31,{0,0}));  // zero,ones bit set
  25.         vector<vector<pair<long long,long long>>> dp2(N,vector<pair<long long,long long>>(31,{0,0}));
  26.  
  27.         for(int i=0;i<N;i++){
  28.             for(int bit=0;bit<=30;bit++){
  29.                 if(A1[i] & (1<<bit)){
  30.                     dp1[i][bit].second++;
  31.                 }
  32.                 else{
  33.                     dp1[i][bit].first++;
  34.                 }
  35.                
  36.                 if(A2[i] & (1<<bit)){
  37.                     dp2[i][bit].second++;
  38.                 }
  39.                 else{
  40.                     dp2[i][bit].first++;
  41.                 }
  42.            
  43.                 if(i-1>=0){
  44.                     dp1[i][bit].second+=dp1[i-1][bit].second;
  45.                     dp1[i][bit].first+=dp1[i-1][bit].first;
  46.                    
  47.                     dp2[i][bit].second+=dp2[i-1][bit].second;
  48.                     dp2[i][bit].first+=dp2[i-1][bit].first;
  49.                 }
  50.             }
  51.         }
  52.        
  53.         vector<long long int> res;
  54.        
  55.         for(auto q: query){
  56.             int k=q[0]-1;
  57.             int l1=q[1]-1;
  58.             int l2=q[2]-1;
  59.             int r1=q[3]-1;
  60.             int r2=q[4]-1;
  61.            
  62.             long long zerosL=dp1[l2][k].first;
  63.             long long onesL=dp1[l2][k].second;
  64.             if(l1-1>=0){
  65.                 zerosL-=dp1[l1-1][k].first;
  66.                 onesL-=dp1[l1-1][k].second;
  67.             }
  68.            
  69.             long long zerosR=dp2[r2][k].first;
  70.             long long onesR=dp2[r2][k].second;
  71.             if(r1-1>=0){
  72.                 onesR-=dp2[r1-1][k].second;
  73.                 zerosR-=dp2[r1-1][k].first;
  74.             }
  75.            
  76.             long long int ans=onesR*zerosL+zerosR*onesL;
  77.             res.push_back(ans);
  78.         }
  79.         return res;
  80.     }
  81. };
Advertisement
Add Comment
Please, Sign In to add comment