You are given an integer array nums
, an integer array queries
, and an integer x
.
For each queries[i]
, you need to find the index of the queries[i]th
occurrence of x
in the nums
array. If there are fewer than queries[i]
occurrences of x
, the answer should be -1 for that query.
Return an integer array answer
containing the answers to all queries.
Example 1:
Input: nums = [1,3,1,7], queries = [1,3,2,4], x = 1
Output: [0,-1,2,-1]
Explanation:
nums
, so the answer is -1.nums
, so the answer is -1.Example 2:
Input: nums = [1,2,3], queries = [10], x = 5
Output: [-1]
Explanation:
nums
, so the answer is -1.Constraints:
1 <= nums.length, queries.length <= 105
1 <= queries[i] <= 105
1 <= nums[i], x <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to find out how many times a specific thing appears in a collection is to simply look at each thing, one by one. We will check each item to see if it matches what we are looking for. If it does, we count it.
Here's how the algorithm would work step-by-step:
def find_occurrences_brute_force(data_series, target_element):
occurrence_count = 0
for element_index in range(len(data_series)):
#Iterate through each element in dataseries
current_element = data_series[element_index]
# Compare current element to target
if current_element == target_element:
#Increment if both are equal
occurrence_count += 1
return occurrence_count
To quickly find how many times a specific element appears in a sorted collection, we avoid checking each item individually. Instead, we use a technique similar to looking up a word in a dictionary to rapidly narrow down the possible locations of the element.
Here's how the algorithm would work step-by-step:
def find_occurrences(sorted_array, target_element):
def find_first_occurrence(sorted_array, target_element):
left_index = 0
right_index = len(sorted_array) - 1
first_occurrence = -1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
if sorted_array[middle_index] == target_element:
# Found the target, but check for earlier occurrences.
first_occurrence = middle_index
right_index = middle_index - 1
elif sorted_array[middle_index] < target_element:
left_index = middle_index + 1
else:
right_index = middle_index - 1
return first_occurrence
def find_last_occurrence(sorted_array, target_element):
left_index = 0
right_index = len(sorted_array) - 1
last_occurrence = -1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
if sorted_array[middle_index] == target_element:
# Found the target, check for later occurrences.
last_occurrence = middle_index
left_index = middle_index + 1
elif sorted_array[middle_index] < target_element:
left_index = middle_index + 1
else:
right_index = middle_index - 1
return last_occurrence
# Find the index of the first occurrence.
first_index = find_first_occurrence(sorted_array, target_element)
# Find the index of the last occurrence.
last_index = find_last_occurrence(sorted_array, target_element)
if first_index == -1 or last_index == -1:
return 0
# Calculate the number of occurrences.
number_of_occurrences = last_index - first_index + 1
return number_of_occurrences
Case | How to Handle |
---|---|
Null or undefined array input | Throw an IllegalArgumentException or return an empty list, depending on the requirements, to avoid NullPointerException. |
Empty array input | Return an empty list or an appropriate error code as there are no elements to search. |
Array with a single element | Check if the single element matches the search target and return a list containing its index if it does, otherwise return an empty list. |
Large array size (memory constraints) | Consider a streaming approach or divide-and-conquer strategy if memory becomes a bottleneck. |
Array containing a very large number of occurrences of the target element | The algorithm's performance could degrade if the target element occurs very often, but it should still function correctly with correct time complexity. |
Array contains integer overflow values | The element comparison should handle potential overflow errors by either checking for overflow prior to the comparison or using a data type with a larger range. |
Target element is null or undefined | Throw an IllegalArgumentException or treat null/undefined as a specific value, depending on the problem constraints. |
The array contains NaN (Not a Number) values if the array type is floating point | Handle NaN values appropriately, as NaN != NaN, so a direct comparison would fail; typically, treat them as not equal to the target. |