
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for Find the closest pair from two sorted arrays
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given two arrays, we need to find the closest pair from the two sorted arrays
Now let’s observe the solution in the implementation below −
Example
# sys module import sys # pair def print_(ar1, ar2, m, n, x): # difference diff=sys.maxsize # index l = 0 r = n-1 while(l < m and r >= 0): # closest pair if abs(ar1[l] + ar2[r] - x) < diff: res_l = l res_r = r diff = abs(ar1[l] + ar2[r] - x) # pair sum if ar1[l] + ar2[r] > x: r=r-1 else: l=l+1 # Print the result print("The closest pair available is [",ar1[res_l],",",ar2[res_r],"]") # main ar1 = [1, 3, 6, 9] ar2 = [11, 23, 35, 50] m = len(ar1) n = len(ar2) x = 20 print_(ar1, ar2, m, n, x)
Output
The closest pair available is [ 9 , 11 ]
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned about how we can make a Python Program for Find the closest pair from two sorted arrays
- Related Articles
- Java Program to Find the closest pair from two sorted arrays
- Find the closest pair from two sorted arrays in c++
- Find three closest elements from given three sorted arrays in C++
- JavaScript Program for find common elements in two sorted arrays
- Print uncommon elements from two sorted arrays
- Python program to find common elements in three sorted arrays?
- Golang program to find intersection of two sorted arrays using two pointer approach
- Program to find closest room from queries in Python
- C/C++ Program for Median of two sorted arrays of same size?
- Merge two sorted arrays in Python using heapq?
- C++ Program to find the median of two sorted arrays using binary search approach
- C# program to merge two sorted arrays into one
- Find Sum of pair from two arrays with maximum sum in C++
- C++ Program to Find Closest Pair of Points in an Array
- Find relative complement of two sorted arrays in C++

Advertisements