Given array of sorted integer, search key and search preferences find array position. A search preferences can be:
1) EQUAL β search only for equal key or -1 if not found. Itβs a regular binary search.
2) EQUAL_OR_SMALLER β search only for equal key or the closest smaller. -1 if not found.
3) EQUAL_OR_BIGGER β search only for equal key or the closest bigger. -1 if not found.
Examples:
Input : { 0, 2, 4, 6 }, key -1, EQUAL Output : -1 Input : { 0, 2, 4, 6 }, key -1, EQUAL_OR_BIGGER Output : 0 Input : { 0, 2, 4, 6 }, key 7, EQUAL_OR_BIGGER Output : -1 Input : { 0, 2, 4, 6 }, key 7, EQUAL_OR_SMALLER Output : 3
In regular binary search algorithm evaluation and division perform as far as subarray size is bigger than 0.
In our case if we want to keep single function we need to perform final evaluation in subarray of size=2. Only in subarray size==2 is possible to check both EQUAL_OR_SMALLER and EQUAL_OR_BIGGER conditions.
In below code, SC stands for Search Criteria.
public class BinarySearchEqualOrClose { Β Β Β Β Β Β private static void printResult( int key, int pos, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β SC sC) Β Β Β Β { Β Β Β Β Β Β Β Β System.out.println( "" + key + ", " + sC + ":" + pos); Β Β Β Β } Β Β Β Β Β Β enum SC { Β Β Β Β Β Β Β Β EQUAL, Β Β Β Β Β Β Β Β EQUAL_OR_BIGGER, Β Β Β Β Β Β Β Β EQUAL_OR_SMALLER Β Β Β Β }; Β Β Β Β Β Β public static int searchEqualOrClose( int key, int [] arr, SC sC) Β Β Β Β { Β Β Β Β Β Β Β Β if (arr == null || arr.length == 0 ) { Β Β Β Β Β Β Β Β Β Β Β Β return - 1 ; Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β if (arr.length == 1 ) { // just eliminate case of length==1 Β Β Β Β Β Β Β Β Β Β Β Β Β Β // since algorithm needs min array size==2 Β Β Β Β Β Β Β Β Β Β Β Β // to start final evaluations Β Β Β Β Β Β Β Β Β Β Β Β if (arr[ 0 ] == key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return 0 ; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β if (arr[ 0 ] < key && sC == SC.EQUAL_OR_SMALLER) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return 0 ; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β if (arr[ 0 ] > key && sC == SC.EQUAL_OR_BIGGER) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return 0 ; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β return - 1 ; Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β return searchEqualOrClose(arr, key, 0 , arr.length - 1 , sC); Β Β Β Β } Β Β Β Β Β Β private static int searchEqualOrClose( int [] arr, int key, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β int start, int end, SC sC) Β Β Β Β { Β Β Β Β Β Β Β Β int midPos = (start + end) / 2 ; Β Β Β Β Β Β Β Β int midVal = arr[midPos]; Β Β Β Β Β Β Β Β if (midVal == key) { Β Β Β Β Β Β Β Β Β Β Β Β return midPos; // equal is top priority Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β if (start >= end - 1 ) { Β Β Β Β Β Β Β Β Β Β Β Β if (arr[end] == key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return end; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β if (sC == SC.EQUAL_OR_SMALLER) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // find biggest of smaller element Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[start] > key && start != 0 ) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // even before if "start" is not a first Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return start - 1 ; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[end] < key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return end; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[start] < key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return start; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return - 1 ; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β if (sC == SC.EQUAL_OR_BIGGER) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // find smallest of bigger element Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[end] < key && end != arr.length - 1 ) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // even after if "end" is not a last Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return end + 1 ; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[start] > key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return start; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (arr[end] > key) { Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return end; Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return - 1 ; Β Β Β Β Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β Β Β Β Β return - 1 ; Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β if (midVal > key) { Β Β Β Β Β Β Β Β Β Β Β Β return searchEqualOrClose(arr, key, start, midPos - 1 , sC); Β Β Β Β Β Β Β Β } Β Β Β Β Β Β Β Β return searchEqualOrClose(arr, key, midPos + 1 , end, sC); Β Β Β Β } Β Β Β Β Β Β public static void main(String[] args) Β Β Β Β { Β Β Β Β Β Β Β Β int [] arr = new int [] { 0 , 2 , 4 , 6 }; Β Β Β Β Β Β Β Β Β Β // test full range of xs and SearchCriteria Β Β Β Β Β Β Β Β for ( int x = - 1 ; x <= 7 ; x++) { Β Β Β Β Β Β Β Β Β Β Β Β int pos = searchEqualOrClose(x, arr, SC.EQUAL); Β Β Β Β Β Β Β Β Β Β Β Β printResult(x, pos, SC.EQUAL); Β Β Β Β Β Β Β Β Β Β Β Β pos = searchEqualOrClose(x, arr, SC.EQUAL_OR_SMALLER); Β Β Β Β Β Β Β Β Β Β Β Β printResult(x, pos, SC.EQUAL_OR_SMALLER); Β Β Β Β Β Β Β Β Β Β Β Β pos = searchEqualOrClose(x, arr, SC.EQUAL_OR_BIGGER); Β Β Β Β Β Β Β Β Β Β Β Β printResult(x, pos, SC.EQUAL_OR_BIGGER); Β Β Β Β Β Β Β Β } Β Β Β Β } } |
-1, EQUAL:-1 -1, EQUAL_OR_SMALLER:-1 -1, EQUAL_OR_BIGGER:0 0, EQUAL:0 0, EQUAL_OR_SMALLER:0 0, EQUAL_OR_BIGGER:0 1, EQUAL:-1 1, EQUAL_OR_SMALLER:0 1, EQUAL_OR_BIGGER:1 2, EQUAL:1 2, EQUAL_OR_SMALLER:1 2, EQUAL_OR_BIGGER:1 3, EQUAL:-1 3, EQUAL_OR_SMALLER:1 3, EQUAL_OR_BIGGER:2 4, EQUAL:2 4, EQUAL_OR_SMALLER:2 4, EQUAL_OR_BIGGER:2 5, EQUAL:-1 5, EQUAL_OR_SMALLER:2 5, EQUAL_OR_BIGGER:3 6, EQUAL:3 6, EQUAL_OR_SMALLER:3 6, EQUAL_OR_BIGGER:3 7, EQUAL:-1 7, EQUAL_OR_SMALLER:3 7, EQUAL_OR_BIGGER:-1
Time Complexity: O(log n)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!