Problem Statement
Given two integer arrays, nums1 and nums2, find their intersection and return it. Each element in the result should appear as many times as it shows in both arrays. You can assume the values in nums1 and nums2 are limited to integers ranging from 0 to 100.
Approach
This problem can be solved efficiently using a counting method due to the constraint on the values of the elements (ranging from 0 to 100). Here’s how we can approach the problem:
-
Fixed-Size Arrays for Counting: Since the values in both input arrays are between
0and100, we can use two fixed-size arrays (vandc) of length101to count the occurrences of each integer in both arrays. -
Counting Occurrences:
- First, iterate through
nums1and populate thevarray, wherev[i]stores the count of the numberiinnums1. - Next, iterate through
nums2and populate thecarray, wherec[i]stores the count of the numberiinnums2.
- First, iterate through
-
Finding the Intersection:
- For each possible integer value
ifrom0to100, find the minimum count ofiin both arrays (v[i]andc[i]). This represents how many timesishould appear in the intersection. - Add this minimum count of
ito the result array.
- For each possible integer value
-
Output the Result:
- The result array is filled with the elements representing the intersection of
nums1andnums2. - The
intersectfunction modifies thereturnSizeto indicate the number of elements in the result and returns the array.
- The result array is filled with the elements representing the intersection of
Code Explanation
Here’s the breakdown of the provided code:
-
Counting Occurrences:
- The function
intersecttakes five parameters: two arrays (nums1,nums2), their respective sizes, a pointer to store the result size, and an array to store the intersection result. - The
vandcarrays of size101are initialized to zero. These arrays store the counts of each element innums1andnums2.
- The function
-
Populating the Count Arrays:
- The first
forloop populates thevarray by counting occurrences of each element innums1. - The second
forloop populates thecarray by counting occurrences of each element innums2.
- The first
-
Finding the Intersection:
- The third
forloop iterates over the possible values (0to100), determines the minimum count of each number present in bothnums1andnums2, and adds that many occurrences to the result array.
- The third
-
Main Function:
- The main function reads the sizes of the input arrays and their elements.
- It then calls the
intersectfunction to find the intersection and store the result in theansarray. - Finally, it prints the elements of the intersection.
Complexity Analysis
- Time Complexity: O(n + m), where
nis the size ofnums1andmis the size ofnums2. Counting the elements in each array takes O(n) and O(m) respectively, and finding the intersection involves a fixed iteration of 101 steps (from 0 to 100), which is constant. - Space Complexity: O(1) auxiliary space, not considering the input and output arrays. The use of fixed-size arrays (
v,c, andans) makes the space usage constant.