Beautiful Pairs

Sort by

recency

|

293 Discussions

|

  • + 0 comments
    /*
     * Complete the 'beautifulPairs' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER_ARRAY A
     *  2. INTEGER_ARRAY B
     */
    
    void _print(const multiset<int>& s) {
        for (auto e: s) { cout << e << " "; }
        cout << endl;
    }
    
    int beautifulPairs(vector<int> A, vector<int> B) {
        multiset<int> ms(B.begin(), B.end());
        
        int count = 0;
        for (auto a: A) {
            auto it = ms.find(a);
            if (it != ms.end()) {
                count++;
                ms.erase(it);
            }
        }    
        // cout << count << endl;
        // _print(ms);
        
        if (ms.empty()) { return count - 1; }
        return count + 1;
    }
    
  • + 0 comments

    Here's my java solution

    public static int beautifulPairs(List A, List B) { // Write your code here //frequency map for A Map freqMapA = A.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        // frequency map for B
        Map<Integer, Long> freqMapB = B.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
        // iterate over A's frequency map, filter those are in B only. get min
        //  of both the frequency, that will be count of pairs for that number
        int count = freqMapA.entrySet().stream().filter(en -> freqMapB.containsKey(en.getKey())).map(en -> Math.min(en.getValue(), freqMapB.get(en.getKey()))).mapToInt(Long::intValue).sum();
    
        // adjust count since we're to update one index
        return count == A.size() ? count - 1 : count + 1;
    }
    
  • + 0 comments
    function beautifulPairs(A, B) {
      let mp = {};
      let result = 0;
    
      for (let i = 0; i < A.length; i++) {
        mp[A[i]] = (mp[A[i]] || 0) + 1;
      }
    
      for (let i = 0; i < B.length; i++) {
        if (mp[B[i]] > 0) {
          mp[B[i]]--;
          result++;
        }
      }
      const isEmpty = Object.keys(mp).every(key => mp[key] === 0);
    
      return result === A.length ? (isEmpty ? result : result - 1) : result + 1;
    }
    
    console.log(beautifulPairs([1, 2, 3, 4], [1, 2, 4, 3]));
    
  • + 0 comments

    Here is my Python solution!

    def beautifulPairs(A, B):
        pairs = 0
        for num in B:
            if num in A:
                A.remove(num)
                pairs += 1
        if B:
            if A:
                return pairs + 1
            return pairs - 1
        elif A:
            return pairs
        else:
            return pairs - 1
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/coANgIdBKAk

    int beautifulPairs(vector<int> A, vector<int> B) {
        map<int, int> mp;
        int result = 0;
        for(int i = 0; i < A.size(); i++) mp[A[i]]++;
        for(int i = 0; i < B.size(); i++){
            if(mp[B[i]]){
                mp[B[i]]--;
                result++;
            }
        }
        return (result == A.size()) ? result - 1 : result + 1;
    }