Given an array of integers and two specified numbers, find a subarray from the original array that contains both of these specified numbers, with the requirement that the subarray contains the minimum number of distinct numbers. Return the count of distinct numbers in this subarray.
If the two specified numbers are the same, simply return 1 if the array contains this number, otherwise return 0.
Complete the function findSubarrayWithMinimumDistinctIntegers in the editor.
findSubarrayWithMinimumDistinctIntegers has the following parameters:
- 1.
int[] array: an array of integers - 2.
int series1: the first specified number - 3.
int series2: the second specified number
Returns
int: the count of distinct numbers in the subarray
Examples
01 · Example 1
array = [1, 2, 2, 2, 5, 2] series1 = 1 series2 = 5 return = 3
The subarray must contain the series1 and series2 and having the smallest number of distinct values is [1, 2, 2, 2, 5], so return 3, the number of distinct integers in this subarray.
02 · Example 2
array = [1, 3, 2, 1, 4] series1 = 1 series2 = 2 return = 2
The shortest subarray contains both series1 and series2 is [2, 1], so we return 2, the number of distinct values in this subarray.
03 · Example 3
array = [3, 1, 2] series1 = 1 series2 = 1 return = 1
Because series1 and series2 are the same, so if the array contains series1, that would be good. This array contains series1, so we return 1.
04 · Example 4
array = [2, 4, 9] series1 = 1 series2 = 1 return = 0
Because series1 and series2 are the same, but this array doesn't contain series1, so we return 0.
Constraints
🍍🍍More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public int findSubarrayWithMinimumDistinctIntegers(int[] array, int series1, int series2) {
// write your code here
}
array[1, 2, 2, 2, 5, 2]
series11
series25
expected3
sign in to submit