Beautiful Pairs

Sort by

recency

|

292 Discussions

|

  • + 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;
    }
    
  • + 0 comments

    My Java solution with linear time complexity and linear space complexity:

    public static int beautifulPairs(List<Integer> A, List<Integer> B) {
            // create a frequency map to get the count of eacg val
            Map<Integer, Integer> freqMap = new HashMap<>();
            for(int val : A) freqMap.put(val, freqMap.getOrDefault(val, 0) + 1);
            //iterate over each val in b and accoridngly increment the amt of pairs
            int pairs = 0;
            for(int val : B){
                if(freqMap.containsKey(val) && freqMap.get(val) > 0){
                    pairs++;
                    freqMap.put(val, freqMap.getOrDefault(val, 0) - 1);
                }
            }
            //if amt of pairs == a.size(), decrease it because by 1 bc an element must be changed
            //else add 1 bc an element isnt paired
            return (pairs == A.size()) ? pairs - 1 : pairs + 1;
        }