Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to x.
Input : arr1 = [-1, -2, 4, -6, 5, 7] arr2 = [6, 3, 4, 0] x = 8 Output : [(5, 3), (4, 4)] Input : arr1 = [1, 2, 4, 5, 7] arr2 = [5, 6, 3, 4, 8] x = 9 Output : [(1, 8), (4, 5), (5, 4)]
We have existing solution for this problem please refer Given two unsorted arrays, find all pairs whose sum is x link. We can solve this problem quickly in python using List comprehension. Approach is very simple, we will consider all those pairs for which if k lies in arr2 then x-k should lie in arr1, so pair will be (x-k,k).
Complexity : O(n*n)
[(5, 3), (4, 4)]
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python List Comprehension and Slicing
- Python | List comprehension vs * operator
- Count set bits using Python List comprehension
- Move all zeroes to end of array using List Comprehension in Python
- Python List Comprehension | Three way partitioning of an array around a given range
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python | Pair and combine nested list to tuple list
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Find Sum of pair from two arrays with maximum sum
- Find the closest pair from two sorted arrays
- Find a pair of elements swapping which makes sum of two arrays same
- Python | Pair iteration in list
- Python | Pair summation of list elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.