Minimize Hamming Distance Problem
Ninja has strategically formed a team to combat enemies in his city. Each ninja in the team has a specific range of attack, and the team has successfully acquired the attack range information of their enemies. To optimize the attack strategy, ninja allows swaps between team members to minimize the Hamming distance, which is the count of differing positions between elements of the two arrays.
Input:
Given two arrays 'ninja'
and 'enemies'
, both of the same length, and an 'allowedSwaps'
array where each element [ai, bi]
indicates the indices in the 'ninja' array which can be swapped.
Output:
Return the minimum Hamming distance between 'ninja'
and 'enemies'
after performing any number of such allowed swaps.
Example:
Input:
ninja = [1, 2, 3, 4] enemies = [2, 1, 4, 5] allowedSwaps = [[0, 1], [2, 3]]
Output:
1
Explanation:
By swapping indices 0 and 1, and 2 and 3, the ninja array becomes [2, 1, 4, 3]
, minimizing the Hamming distance to 1.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 10^3
1 ≤ ninja[i], enemies[i] < 10^5
0 ≤ allowedSwaps.length ≤ 10^5
Note: Implement the function as specified. Do not handle output or input formatting.
Minimize Hamming distance between two arrays by swapping elements at allowed indices.
Iterate through allowed swaps and swap elements in ninja array accordingly.
Calculate Hamming distance between ninja...read more
Top Cognizant Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Cognizant Software Developer
Reviews
Interviews
Salaries
Users/Month