Find Occurrences of an Element in an Array - Complete Solution Guide
Find Occurrences of an Element in an Array is LeetCode problem 3159, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: For the 1 st query, the first occurrence of 1 is at
Detailed Explanation
The problem asks us to find the indices of specific occurrences of a given integer `x` within an integer array `nums`. For each query in the `queries` array, we need to determine the index of the `queries[i]`-th occurrence of `x` in `nums`. If `x` appears fewer than `queries[i]` times in `nums`, the result for that query should be -1. The output is an array containing the results for each query.
Solution Approach
The provided solution first identifies all indices in the `nums` array where the value `x` is present. These indices are stored in a separate list/array. Then, for each query in the `queries` array, it checks if the requested occurrence number is valid (i.e., not greater than the total number of occurrences of `x`). If the occurrence number is valid, the index corresponding to that occurrence is retrieved from the pre-computed list of indices. Otherwise, -1 is returned, indicating that the requested occurrence does not exist.
Step-by-Step Algorithm
- Step 1: Iterate through the `nums` array.
- Step 2: For each element in `nums`, check if it is equal to `x`.
- Step 3: If an element is equal to `x`, store its index in a list or array (e.g., `x_indices`).
- Step 4: After iterating through `nums`, determine the total number of occurrences of `x` (i.e., the size of the `x_indices` list).
- Step 5: Iterate through the `queries` array.
- Step 6: For each query value `q`, check if `q` is greater than the total number of occurrences of `x`.
- Step 7: If `q` is greater than the number of occurrences, append -1 to the `answer` array.
- Step 8: Otherwise (if `q` is valid), access the `(q - 1)`-th element in the `x_indices` array and append its value (which represents the index of the q-th occurrence of `x` in the original array) to the `answer` array.
- Step 9: Return the `answer` array.
Key Insights
- Insight 1: The core task is to efficiently locate the indices where the target integer `x` appears in the input array `nums`.
- Insight 2: Pre-computing and storing the indices of `x` allows for faster processing of multiple queries.
- Insight 3: Handling the edge case where the query number exceeds the total number of occurrences of `x` is crucial for correctness.
Complexity Analysis
Time Complexity: O(n + m)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: IBM, J.P. Morgan.