You are at your friend’s birthday party and one of your friends decided to play a card game with some special type of cards. These cards can have numbers written on both sides.
You are given ‘N’ cards, each card contains some integer number between ‘1’ to ‘10’ written on both sides. You are allowed to flip a card such that the number which was previously on the top now comes at the bottom and vice versa.
Find the minimum flips that need to be performed to make all the numbers equal on either the top sides or the bottom sides. If it is impossible to make all the cards the same on either side then return ‘-1’.
For Example :If N = 4 and the numbers on the top sides are: { 1, 2, 3, 2 } and the numbers on the bottom sides are: { 2, 4, 2, 2}
Then the minimum number of flips required is equal to 1.
We can flip the 2nd card, the top sides now become: { 1, 4, 3, 2 } and the bottom sides are: { 2, 2, 2, 2}. This results in getting the same numbers on the bottom side.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the number of cards.
The second line of each test case contains the N integers ‘Top[i]’ denoting the number on the top side of the ith card.
The third line of each test case contains the N integers ‘Bottom[i]’ denoting the number on the bottom side of the ith card.
Output Format :
For each test case, print a single integer denoting the minimum number of flips required.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 200
1 <= Bottom[i], Top[i] <= 10
Time limit: 1 sec
2
4
1 2 3 2
2 4 2 2
1
3
3
1
0
For test case 1 :
We will return 2, because:
We can flip the 2nd card, the top sides now become: { 1, 4, 3, 2 } and the bottom sides are: { 2, 2, 2, 2}. This results in getting the same numbers on the bottom side.
For test case 2 :
We will return 0, because:
There is only one card, resulting in the top side of all the cards having the same numbers, so no flips are required.
2
4
9 8 9 8
8 9 8 9
2
5 10
9 8
2
-1
Can we check if it is possible to make all the numbers on one side equal to a number between ‘1’ to ‘10’?
Do we actually need to check for all the values between ‘1’ to ‘10’? Checking just for the numbers on the top and bottom side of the first card will be sufficient?
We can simply find the number of flips required to make all the numbers on the top sides of the cards equal to ‘i’ where ‘i’ can range from ‘1’ to ‘10’. We can repeat the same thing for the bottom side, and finally, return the minimum number of flips out of them.
But note that for all cards to have the same value on either of the sides, the value should be either equal to the number on the top or on the bottom side of the first card.
The steps are as follows :
O( N ), where N denotes the number of cards
We iterate through each card a maximum of four times in the worst case.
Hence the time complexity is O( N ).
O( 1 )
Extra space is not used. Hence the space complexity is O( 1 ).