NINJA ATTACK
Ninja has built his team of ninjas to fight with the enemies in his city. Ninja made a plan of attacking all his enemies. In his team, every ninja has his own range of hitting and they had secretly got the hitting range of their enemies as well. So Ninja allowed some swaps between his ninjas so that they can minimize the hamming distance that is the number of positions where the hitting range of enemies and ninjas are different.
Your task is to write a code that can minimize the hamming distance. You are being provided with two arrays ‘ninja’ and ‘enemies’ both of the same size and an array ‘allowedSwaps’ where each allowedSwaps[i] = [ ai, bi ] indicates that you are allowed to swap the elements at index ai and index bi.
The Hamming distance of two arrays of the same length, ninja, and enemies, is the number of positions where the elements are different.
Example :
Consider the case ‘ninja’array is [ 1, 2, 3, 4 ], ‘enemies’array is [ 2, 1, 4, 5 ] and ‘allowedSwaps’ are = [ [ 0, 1 ], [ 2, 3 ] ] so after swapping in best manner according to ‘allowedSwaps’ our ‘ninja’ array becomes [ 2, 1, 4, 3 ]. So minimum Hamming distance is ‘1’ as now there is only one different element as compared to ‘ninja’ and ‘enemies’ array index.
Note :
1. You are allowed to do as many swap operations on the ‘ninja’ array as you want but according to the ‘allowedSwap’ array.
2. You are not required to print anything explicitly. It has already been taken care of. Just implement the function.
Input Format :
The first line of input contains a ‘T’ number of test cases.
The first line of each test case contains an integer ‘n’, which represents the size of the array ‘ninja’ and ‘enemies’.
The second line of each test case contains the ‘n’ space-separated integer of array ‘ninja’.
The third line of each test case contains the ‘n’ space-separated integer of array ‘enemies’.
The fourth line of each test case contains an integer ‘m’ denoting the number of rows in array ‘allowedSwap’. Then, ‘m’ lines follow.
Each line contains two space-separated integers denoting the array values.
Output Format :
For each test case, return the minimum hamming distance of the ‘ninja’ and ‘enemies’ array.
Constraints :
1 <= T <= 100
1 <= N <= 10^3
1 <= ninja[i], enemies[i] < 10^5
0 <= allowedSwaps[i] <=10^5
Where ‘T’ represents the number of test cases, ‘N’ represents the size of the ‘ninja’ and ‘enemies’ array and ninja[i], enemies[i], and allowedSwaps[i] represents the element in the array.
Time Limit: 1 second
CodingNinjas
author
2y
Step 1 : Calculate the XOR of two numbers.
Strp 2 : Count the number of set bits.
CodingNinjas
author
2y
Sorting
The logic used here is, for ex-
‘arr = [1, 2, 3, 4]’
If swap(1, 2) allowed, and swap(1, 4) allowed and we are allowed to do any number of swaps,
then 1, 2, 4 can be rearranged into any possible co...read more
CodingNinjas
author
2y
HASHMAP
The idea here is to use union and try to find all the connected groups (allowedSwaps). In each group find the number of elements that appeared in the ninja array but not in the enemies array.
Th...read more
Anonymous
4mo
def max_enemies_defeated(ninjaRanges, enemyPositions): # Sort ninja ranges and enemy positions ninja...read more
Add answer anonymously...
Top Cognizant Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Cognizant Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app