Find k closest elements to a given value

时间:2022-11-29 19:27:06

Given a sorted array arr[] and a value x, find the k closest elements to x in arr[].

input: k=4, x=35

arr[] = {12,16,22,30,35,39,42,45,48,50,53,55,56}

output:30 39 42 45

解法:二分查找,找到crossover point,然后从这个交叉点开始继续二分比较。若x的值与数组中某元素相等,则不输出,假设该数组中没有与x相等的重复元素。若有重复元素,则增加计数器,若计数器为0,则不输出当前值,若不为0,则输出该值。

package p_2;

public class Main {
	public static int[] a = {12,16,22,30,35,39,42,45,48,50,53,55,56};
	public static void crossPoint(int left, int right, int[] a,int data,int level)
	{
		if(left == right || left+1 == right)
		{
			print(left,right,data,level);
			return;
		}else
		{
			int mid = (left+right)/2;
			if(a[mid]>data)
			{
				right = mid;
				crossPoint(left,right,a,data,level);
			}else if(a[mid]<data)
			{
				left = mid;
				crossPoint(left,right,a,data,level);
			}else
			{
				left = mid;
				right = mid;
				crossPoint(left,right,a,data,level);
			}
		}
	}	
	public static void print(int left,int right,int data,int level){
		int count =0;
		if(left == right)
		{
			left--;
			right++;
			while(count!=4)
			{
				if(left>=0&&right<a.length)
				{
					if((data-a[left])>(a[right]-data))
					{
						System.out.println(a[right]);
						right++;
						count++;
					}else if((data-a[left])<(a[right]-data))
					{
						System.out.println(a[left]);
						left--;
						count++;
					}else if((data-a[left])==(a[right]-data)){
						System.out.println(a[right]);
						right++;
						count++;
					}
				}else if(left<0)
				{
					System.out.println(a[right]);
					right++;
					count++;
				}else if(right>=a.length)
				{
					System.out.println(a[left]);
					left--;
					count++;
				}
			}
		}else{
			while(count!=level)
			{
				if(left>0&&right<a.length)
				{
					if((data-a[left])>(a[right]-data))
					{
						System.out.println(a[right]);
						right++;
						count++;
					}else if((data-a[left])<(a[right]-data))
					{
						System.out.println(a[left]);
						left--;
						count++;
					}else if((data-a[left])==(a[right]-data)){
						System.out.println(a[right]);
						right++;
						count++;
					}
				}else if(left<=0)
				{
					System.out.println(a[right]);
					right++;
					count++;
				}else if(right>=a.length)
				{
					System.out.println(a[left]);
					left--;
					count++;
				}
			}
		}
	}
	public static void main(String args[])
	{
		int level = 4;
		int left = 0;
		int right = a.length;
		int data = 49;
		crossPoint(left,right,a,data,level);
	}
}