We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Greedy
- Beautiful Pairs
- Discussions
Beautiful Pairs
Beautiful Pairs
Sort by
recency
|
286 Discussions
|
Please Login in order to post a comment
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;
}
Solution of Beautiul Pairs
Haskell
The mandatory "must change a value" was kind of bullshit, but otherwise...
Here is my c++ solution, you can watch the explanation here : https://youtu.be/coANgIdBKAk
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.