This article is another part of a series on leet code problems solutions, and it provides a solution to the leet code Intersection of Two Arrays II. This is a Leet code problem named two Sum. We will solve it using python and it is the best space and time-optimized solution.
Question of Intersection of Two Arrays
Given two integer arrays nums1
and nums2
, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
Question Explaination
Here in this question, we have to find the intersection of two arrays. So we have to find the numbers of the second array are present in the first array or not and print the numbers which are present in both the array.
Example
Example 1
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
Example 2
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]
Explanation: [9,4] is also accepted.
Example 3
Input: nums =nums1 =[1,3,8,9,3]
nums2 = [1,0]
Output: [1]
Constraints
- 1 <= nums1.length, nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 1000
Approach to solve Intersection of Two Arrays Problem
To solve this question first we will sort both the arrays. After this, we will run the while loop until we reach at the end of any of the arrays. Inside while loop we check three conditions:
- If both the values are equal then save the value in list and increment pointer i and j by 1.
- If value of nums1 at position i is greater than the value of nums2 at j then we increment the value of j by 1.
- If value of nums1 at position i is less than the value of nums2 at j then we increment the value of j by 1.
Variables and its usage
We will only use 3 variables here.
- ret[] of type list to store the same values of both arrays.
- Variable i as a pointer to nums1 array.
- variable j as a pointer to nums2 array.
Python Code for Intersection of Two Arrays
def intersect( nums1, nums2):
nums2.sort()
nums1.sort()
ret=[]
i=0
j=0
while(len(nums1) >i and (len(nums2) >j)):
if(nums1[i] == nums2[j]):
ret.append(nums2[j])
i+=1
j+=1
elif(nums1[i] > nums2[j]):
j+=1
elif(nums1[i] < nums2[j]):
i+=1
return ret
nums1 =[1,2,2,1]
nums2 = [2,2]
print(intersect( nums1, nums2))
nums1 =[1,2]
nums2 = [1,1]
print(intersect( nums1, nums2))
nums1 =[4,9,5]
nums2 = [9,4,9,8,4]
print(intersect( nums1, nums2))
nums1 =[1,3,8,9,3]
nums2 = [1,0]
print(intersect( nums1, nums2))
Output
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Related Posts:
- Find Pivot Index (Leet Code-724)
- Spiral Matrix Problem Solution
- Isomorphic Strings
- Two Sum IV - Input is a BST (Leet Code-653)
- Search in a Binary Search Tree (Leet Code-700)
- Invert Binary Tree (Leet Code-266)
- Symmetric Tree (Leet Code-101)
- Maximum Depth of Binary Tree (Leet Code-104)
- Binary Tree Postorder Traversal (Leet Code-145)
- Binary Tree Inorder Traversal (Leet Code - 94)
- Binary Tree Pre-Order Traversal
- Binary Tree Level Order Traversal (Leet Code-102)
- Valid Parentheses (Leet Code-20)
- Remove Duplicates from Sorted List (Leet Code-83)
- Reverse Linked List (Leet code -206)
- Remove Linked List Elements (Leet Code-203)
- Merge Two Sorted Lists (Leet code-21)
- Ransom Note (Leet code-383)
- First Unique Character in a String (Leet code-387)
- Valid Sudoku (Leet Code-36)
- Search a 2D Matrix (Leet Code-74)
- Linked List Cycle (Leet Code-141)
- Pascal's Triangle (Leet Code - 118)
- Reshape the Matrix (Leet Code-566)
- Best Time to Buy and Sell Stock (Leet code-121)
- Binary Search - Leet Code
- Two Sum - Leet Code