Help in Codeforces problem b

my code

approach: first I check for the common value in 1st and 2nd pair, and put that in X(array).
After that traverse through pairs, if at any point value of pair is not equal to x and y is not yet been assigned value then y is assumed as y1(first value of current pair), then again check if a pair match x or current y, else change the value of y to y2. Check if an element of current pair is equal to y2 if not then set flag to false.
Then finally check if there exist a True in ans, print “YES” else “NO”.

What am I missing?

1 Like

You are making the same mistake I made. The common value between the first and second pair need not necessarily be x. The uncommon value of these pair can be x and y too. For Example- n=5 and m=4. Following are m pairs.
3 1
3 2
2 5
1 4
Here according to your approach 3 will be x and you won’t be able to find y! So your answer is “No”.
But check if we take x=1 and y=2 we have the answer “Yes” which is correct!
Hope you get it. :slightly_smiling_face:

1 Like

My approach is:- The guys bigger than n/2,I store their occurrences and indices properly. There can be only at most 4 such numbers.
For other numbers with smaller frequencies , I use an adjacency list, now with efficient brute-forcing, my solution passed!?. :slight_smile: