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
0
and100
, we can use two fixed-size arrays (v
andc
) of length101
to count the occurrences of each integer in both arrays. -
Counting Occurrences:
- First, iterate through
nums1
and populate thev
array, wherev[i]
stores the count of the numberi
innums1
. - Next, iterate through
nums2
and populate thec
array, wherec[i]
stores the count of the numberi
innums2
.
- First, iterate through
-
Finding the Intersection:
- For each possible integer value
i
from0
to100
, find the minimum count ofi
in both arrays (v[i]
andc[i]
). This represents how many timesi
should appear in the intersection. - Add this minimum count of
i
to the result array.
- For each possible integer value
-
Output the Result:
- The result array is filled with the elements representing the intersection of
nums1
andnums2
. - The
intersect
function modifies thereturnSize
to 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
intersect
takes 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
v
andc
arrays of size101
are initialized to zero. These arrays store the counts of each element innums1
andnums2
.
- The function
-
Populating the Count Arrays:
- The first
for
loop populates thev
array by counting occurrences of each element innums1
. - The second
for
loop populates thec
array by counting occurrences of each element innums2
.
- The first
-
Finding the Intersection:
- The third
for
loop iterates over the possible values (0
to100
), determines the minimum count of each number present in bothnums1
andnums2
, 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
intersect
function to find the intersection and store the result in theans
array. - Finally, it prints the elements of the intersection.
Complexity Analysis
- Time Complexity: O(n + m), where
n
is the size ofnums1
andm
is 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.