Coder Pairing

Easy
0/40
0 upvote
Asked in company
Expedia Group

Problem statement

You are given the ratings of n coders in an array. To create effective teams, you want to form pairs of coders who have similar ratings. The strategy is to sort all the coders by their rating and then pair up adjacent coders.


Your task is to determine if it's possible to pair up all n coders. This is only possible if n is an even number.


If n is even, you must sort the ratings and form n/2 pairs from adjacent elements. Return a list of these pairs.


If n is odd, it's impossible to pair up everyone. In this case, you should indicate that pairing is not possible.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer N, the number of coders.

The second line contains N space-separated integers, representing the ratings of the coders.


Output Format:
If N is even, print N/2 lines, where each line contains a pair of ratings (the two numbers in the pair separated by a space). The pairs should be printed in increasing order of their ratings.

If N is odd, print a single line with the value -1.


Note:
The core of the problem lies in sorting the initial array to bring coders with similar ratings next to each other.
Sample Input 1:
6
1200 1500 1250 1300 1550 1350


Sample Output 1:
1200 1250
1300 1350
1500 1550


Explanation for Sample 1:
The ratings are sorted: [1200, 1250, 1300, 1350, 1500, 1550].
Adjacent elements are paired: (1200, 1250), (1300, 1350), (1500, 1550).
These pairs are printed, one per line.


Sample Input 2:
5
100 200 300 400 500


Sample Output 2:
-1


Explanation for Sample 2:
The number of coders is 5 (odd). It is impossible to form pairs with all coders, so the output is -1.


Expected Time Complexity:
The expected time complexity is O(N log N).


Constraints:
1 <= N <= 10^5
-10^9 <= rating <= 10^9

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Coder Pairing
Full screen
Console