Problem · Hash Table
Count Distinct Pairs
There is an array of fruits. Your goal is to calculate the amount of distinct pairs (x, y) so that
0 <= x < y < length of fruits. Note that fruits[x] could be gotten from fruits[y] by swapping no more than
2 digits of fruits[y] :)
Note again:: One might not make swapping to any digits at all, so, if x < y and fruits[x] = fruits[y], such pair
should be counted.
Examples
01 · Example 1
fruits = [1, 23, 156, 1650, 651, 165, 32] return = 3
- fruist[1] = 23 could be gotten from fruits[6] = 32 just by swapping its only 2 digits :)
- fruist[2] = 156 could be gotten from fruits[4] = 651 just by swapping six and one :))
- fruist[2] = 156 could be gotten from fruits[5] = 165 just by swapping six & five :))
02 · Example 2
fruits = [123, 321, 123] return = 3
- fruits[1] = 321 could be gotten from fruits[0] = 123, swapping one & three
- fruits[2] = 123 could be gotten from fruits[0] = 123, swapping nothing
- fruits[2] = 123 could be gotten from fruits[1] = 321, swapping three & one
Constraints
1 <= fruits.length <= 1041 <= numbers[i] <= 109More Databricks problems
- Fastest SF CommutePHONE SCREEN · Seen May 2026
- Find First Anagram IndexPHONE SCREEN · Seen Apr 2026
- Minimize CommuteOA · Seen Apr 2026
- Difference Between Sums of PositionsSeen Sep 2024
- Longest Common Prefix of Number PairsSeen Sep 2024
- Subarray CountingSeen Sep 2024
- Write 'L' on MatrixSeen Sep 2024
- Binary String RequestsSeen Aug 2024
public int ddCountPairs(int[] fruits) {
// write your code here
}
fruits[1, 23, 156, 1650, 651, 165, 32]
expected3
checking account