Beautiful Pairs

Sort by

recency

|

286 Discussions

|

  • + 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

    can anyone help me out with this C# code its failing fro one testcase:

    public static int beautifulPairs(List A, List B) { HashSet setB = new HashSet(B); int bPairs = 0;

    foreach (int i in A)
    {
        if (setB.Contains(i))
        {
            bPairs++;
            setB.Remove(i); // Ensures pairwise disjoint condition
        }
    }
    
    // If all elements are matched, changing any element reduces the count
    if (bPairs == A.Count)
    {
        return bPairs - 1; // Change one element in B to break a pair
    }
    else
    {
        return bPairs + 1; // We can increase the count by changing one element in B
    }
    

    }

  • + 1 comment

    Solution of Beautiul Pairs

    def beautifulPairs(A, B):
        # Write your code here
        count = 0
        
        if len(A) == 1:
            return count
        
        for i in A:
            if i in B:
                count += 1
                B.remove(i)
                
        if (count < len(A)):
            return count + 1 
        else:
             return len(A) - 1
     
    
  • + 0 comments

    Haskell

    The mandatory "must change a value" was kind of bullshit, but otherwise...

    module Main where
    
    import qualified Data.Map  as M
    import qualified Data.Set  as S
    
    dist :: [Int] -> M.Map Int Int
    dist xs = M.fromListWith (+) [(x, 1) | x <- xs]
    
    solve :: [Int] -> [Int] -> Int
    solve as bs = do
        let sab = S.fromList $ as ++ bs
        let mbase = M.fromList [(x, 0) | x <- S.elems sab]
        let ma = M.unionWith (+) mbase $ dist as
        let mb = M.unionWith (+) mbase $ dist bs
        let naturals = sum $ M.unionWith min ma mb
        let deltas = M.unionWith (-) mb ma
        let nonzeros = M.filter (/= 0) deltas
        let (poss, negs) = M.partition (> 0) nonzeros
        let correction = if null poss || null negs then -1 else 1
        naturals + correction
    
    main :: IO ()
    main = do
        _ <- getLine
        as <- map read . words <$> getLine :: IO [Int]
        bs <- map read . words <$> getLine :: IO [Int]
        print $ solve as bs
    
  • + 0 comments

    The Larger Test Cases for this question seems to be incorrect or at the very least, not all scenarios are considered. Here's a basic outline of some of the accepted answers following this logic: 1. Add to a Hasmap 2. Go through the list and reduce the hash value until one of the values is exhaused (add to count on each iteration where you find a value) 3. if(cnt != A.size()) return cnt++; else reutrn cnt--;

    Alternatively, You can remove the used Integers from the List instead of using a hash map. But in principle, the logic remains the same.

    The issue is, using this method, it considers non-disjoint values as well as disjoint values. Take this case for example: 5

    1 2 3 3 3

    1 2 3 1 1

    Now, if you were to exhaust the list one by one and switch one of the 1s to 3, you would end up with 4. Which is the correct number of Beautiful Numbers, but not the number of pairwise disjoint beautiful numbers. The correct number of pairwise disjoint numbers attainable in this scenario is 1: (2, 2) at index [1][1]. Since, no matter how we switch one Exactly 1 element in B, we cannot switch at least 1 in set B and have more than one disjoint beautiful pair. If I have misunderstood any part of the text, feel free to reply to this comment.