Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Valid Pairing of Numbers
HARD
50 mins
8 upvotes
Graph
Greedy
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Graph
-
-
Greedy
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Dynamic Programming
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Valid Pairing of Numbers

Contributed by
Dhruv Sharma
Hard
yellow-spark
0/120
Avg time to solve 50 mins
Success Rate 50 %
Share
8 upvotes

Problem Statement

You are provided with a list of numbers from ‘0’ to (2 * ’N’ - 1). You have to find the minimum number of swaps needed to make every even number ‘E’ (present in the list) adjacent to (‘E’ + 1).

For Example:
List = [3, 0, 2, 1]

We have to make ‘0’ adjacent to ‘1’ and ‘2’ to ‘3’. And, to achieve this we can swap ‘0’ with ‘2’.

New list = [3, 2, 0, 1].

Therefore, the answer (minimum number of swaps) is equal to 1.
Note:
There will be only distinct numbers present in the given list.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
1
2
3 0 2 1
Sample Output 1:
1
Explanation of sample input 1:
For the first test case, an explanation is given in the description.
Sample Input 2:
2
1
1 0
3
1 0 2 3 5 4
Sample Output 2:
0
0
Explanation for sample input 2:
In the first test case, the required pairing of all the even numbers is already done.

In the second test case, the required pairing of all the even numbers is already done.
Reset Code
Full screen
Auto
copy-code
Console