All Pairs with Given Sum in Two Arrays
We discuss All Pairs with Given Sum in Two Arrays in this class.
Readers can prepare an entire competitive coding course to crack product development companies. Click Here.
The reader should have basic coding skills to work with competitive coding. Click here.
Question:
Array A of length N and B of length M.
Our task is to find all pairs ( one element from each array) such that the sum of the pair to X.
Note: Elements in each array are distinct
Example:
A = [1, 2, 4, 5, 7]
B = [5, 6, 3, 4, 8]
X = 9
Output: [(1, 8), (4, 5), (5, 4)]
The output should be displayed in ascending order of the first element in the pair.
Time complexity: O(NlogN)
Space complexity: O(N)
Logic:
We use a hash table to program within the given time.
The step-by-step explanation is provided in the video.
Code: