Closest In Sorted Array

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T.

Assumptions

  • There can be duplicate elements in the array, and we can return any of the indices with same value.

Examples

  • A = {1, 2, 3}, T = 2, return 1
  • A = {1, 4, 6}, T = 3, return 1
  • A = {1, 4, 6}, T = 5, return 1 or 2
  • A = {1, 3, 3, 4}, T = 2, return 0 or 1 or 2

Corner Cases

  • What if A is null or A is of zero length? We should return -1 in this case.
 1 public int closest(int[] array, int target) {
 2     // Write your solution here
 3     if(array == null || array.length == 0) return -1 ;
 4     int left = 0 , right = array.length -1 ;
 5     while(left + 1< right){
 6         int mid = left +(right - left )/2 ;
 7       if(array[mid] == target) return mid ;
 8       if(array[mid]<target){
 9           left = mid ;
10       } else{
11           right = mid;
12       }
13     }
14     //post processing
15     if(Math.abs(array[left]-target)<= Math.abs(array[right] - target)){
16         return left ;
17     } else{
18         return right ;
19     }
20   }

原文地址:https://www.cnblogs.com/davidnyc/p/8468521.html

时间: 2024-07-31 11:26:20

Closest In Sorted Array的相关文章

Closest In Sorted Array - Medium

Given a target integer T and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to T. Assumptions There can be duplicate elements in the array, and we can return any of the indices with same value. Examples

K Closest In Sorted Array - Medium

Given a target integer T, a non-negative integer K and an integer array A sorted in ascending order, find the K closest numbers to T in A. Assumptions A is not null K is guranteed to be >= 0 and K is guranteed to be <= A.length Return A size K integ

Closet Number in sorted array

Closest Number in Sorted Array Given a target number and an integer array A sorted in ascending order, find the index i in A such that A[i] is closest to the given target. Return -1 if there is no element in the array. Example Given [1, 2, 3] and tar

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

[LeetCode]Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor