Google Onsite Question
Anonymous User
2994

Given an array with intergers nums, count the number of distinct pairs where 0 <= i < j < length of array and nums[i] == nums[j] where you're allowed to swap two positions of nums[j] to make it equal to nums[i].

Example:
[1, 23, 156, 4738, 321, 72992, 231, 651, 32]
Result = 3
Explanation:
i = 1, nums[i] = 23, j = 8, nums[j] = 32, you can get 23 by swaping 3 and 2 postions in 32 (remeber only two swaps allowed)
i = 2, nums[i] = 156, j = 7, nums[j] = 651, you can get 156 by swaping 1 and 6 postions in 651(remeber only two swaps allowed)
i = 4, nums[i] = 321, j = 6, nums[j] = 231, you can get 321 by swaping 3 and 2 postions in 231(remeber only two swaps allowed)

Solutions much appreciated

Comments (13)