Python Program to Find closest number in array
Last Updated :
17 Apr, 2023
Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers.
Examples:
Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9}
Target number = 11
Output : 9
9 is closest to 11 in given array
Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
Target number = 4
Output : 5
A simple solution is to traverse through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolution difference.
Method #1: Using Binary search
Python3
def findClosest(arr, n, target):
if (target < = arr[ 0 ]):
return arr[ 0 ]
if (target > = arr[n - 1 ]):
return arr[n - 1 ]
i = 0
j = n
mid = 0
while (i < j):
mid = (i + j) / / 2
if (arr[mid] = = target):
return arr[mid]
if (target < arr[mid]):
if (mid > 0 and target > arr[mid - 1 ]):
return getClosest(arr[mid - 1 ], arr[mid], target)
j = mid
else :
if (mid < n - 1 and target < arr[mid + 1 ]):
return getClosest(arr[mid], arr[mid + 1 ], target)
i = mid + 1
return arr[mid]
def getClosest(val1, val2, target):
if (target - val1 > = val2 - target):
return val2
else :
return val1
arr = [ 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 ]
n = len (arr)
target = 11
print (findClosest(arr, n, target))
|
Time Complexity: O(log(n))
Auxiliary Space: O(log(n)) (implicit stack is created due to recursion)
Method #2: Using min() function
Python3
def findClosestValue(givenList, target):
def difference(givenList):
return abs (givenList - target)
result = min (givenList, key = difference)
return result
if __name__ = = "__main__" :
givenList = [ 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 ]
target = 11
result = findClosestValue(givenList, target)
print ( "The closest value to the " + str (target) + " is" , result)
|
Output
The closest value to the 11 is 9
Method #3: Using Two Pointers
Another approach to solve this problem is to use two pointers technique, where we maintain two pointers left and right, and move them towards each other based on their absolute difference with target.
Below are the steps:
- Initialize left = 0 and right = n-1, where n is the size of the array.
- Loop while left < right
- If the absolute difference between arr[left] and target is less than or equal to the absolute difference between arr[right] and target, move left pointer one step to the right, i.e. left++
- Else, move right pointer one step to the left, i.e. right–-
- Return arr[left], which will be the element closest to the target.
Below is the implementation of the above approach:
Python3
import sys
def findClosest(arr, n, target):
left, right = 0 , n - 1
while left < right:
if abs (arr[left] - target) < = abs (arr[right] - target):
right - = 1
else :
left + = 1
return arr[left]
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 5 , 6 , 6 , 8 , 8 , 9 ]
n = len (arr)
target = 11
print (findClosest(arr, n, target))
|
Time Complexity: O(N), where n is the length of the array.
Auxiliary Space: O(1)
Please refer complete article on Find closest number in array for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...