Find k Closest Elements of a Value in Sorted Array

package org.wesome.dsalgo;

public class ClosestElement {
    /*  binary search to find the index of the required element */
    public static int binarySearch(int[] arr, int start, int end, int find) {
        if (start > end) {
            return -1;
        }
        int mid = start + (end - start) / 2;
        if (arr[mid] == find) {
            return mid;
        }
        // If the element is present at the middle itself
        if (arr[mid] >= find) {
            // If element is smaller than mid, then it can only be present in left subarray
            return binarySearch(arr, start, mid - 1, find);
        }
        // Else the element can only be present in right subarray
        else return binarySearch(arr, mid + 1, end, find);
    }

    public static int[] findKthClosest(int[] arr, int element, int kthClosest) {
        int size = arr.length;
        /*  get the index of the element    */
        int index = binarySearch(arr, 0, size - 1, element);
        /*  if left indx is -1, ie element is not present in array  */
        if (index == -1) {
            System.out.println("element " + element + " is not present in array");
            return new int[0];
        }
        int[] result = new int[kthClosest];
        int rightIndx = index + 1, leftIndx = index - 1, count = 0;
        while (leftIndx >= 0 && rightIndx < size && count < kthClosest) {
            int leftMin = element - arr[leftIndx];
            int rightMin = arr[rightIndx] - element;
            if (leftMin < rightMin) {
                System.out.print(arr[leftIndx] + " ");
                result[count] = arr[leftIndx];
                leftIndx--;
            } else {
                System.out.print(arr[rightIndx] + " ");
                result[count] = arr[rightIndx];
                rightIndx++;
            }
            count++;
        }

        /*  if element is at the right or rightIndx reached to array end, then print left elements */
        while (count < kthClosest && leftIndx >= 0) {
            System.out.print(arr[leftIndx] + " ");
            result[count] = arr[leftIndx];
            leftIndx--;
            count++;
        }
        /*  if element is at the left or leftIndx reached to 0, then print right elements */
        while (count < kthClosest && rightIndx < size) {
            System.out.print(arr[rightIndx] + " ");
            result[count] = arr[rightIndx];
            rightIndx++;
            count++;
        }
        return result;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.ClosestElement.findKthClosest;

public class ClosestElementTest {
    @Test
    void findKthClosestTest1() {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int element = 0, kthClosest = 4;
        Assertions.assertArrayEquals(new int[]{}, findKthClosest(arr, element, kthClosest));
    }

    @Test
    void findKthClosestTest2() {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int element = 10, kthClosest = 4;
        Assertions.assertArrayEquals(new int[]{}, findKthClosest(arr, element, kthClosest));
    }

    @Test
    void findKthClosestTest3() {
        int arr[] = {1, 3, 5, 6, 8};
        int element = 5, kthClosest = 4;
        Assertions.assertArrayEquals(new int[]{6, 3, 8, 1}, findKthClosest(arr, element, kthClosest));
    }

    @Test
    void findKthClosestTest4() {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int element = 1, kthClosest = 4;
        Assertions.assertArrayEquals(new int[]{2, 3, 4, 5}, findKthClosest(arr, element, kthClosest));
    }

    @Test
    void findKthClosestTest5() {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int element = 9, kthClosest = 4;
        Assertions.assertArrayEquals(new int[]{8, 7, 6, 5}, findKthClosest(arr, element, kthClosest));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on