![]() |
VOOZH | about |
N-Base modified Binary Search is an algorithm based on number bases that can be used to find an element in a sorted array arr[]. This algorithm is an extension of Bitwise binary search and has a similar running time.
Examples:
Input: arr[] = {1, 4, 5, 8, 11, 15, 21, 45, 70, 100}, target = 45
Output: 7
Explanation: The value 45 is present at index 7Input: arr[] = {1, 6, 8, 10}, target = 9
Output: -1
Explanation: The value 9 is not present in the given array.
Intuition: All numbers of a number system can be expressed in another number systems having any number (e.g. 2, 3, 7) as base of that number system. For example, 7 of decimal number system can be expressed as (21)3 in a number system having base as 3. Therefore the concept of bitwise binary search can be implemented using any number N as base of a number system.
Approach: The index for the target element is searched by adding powers of base (any positive integer starting from 2) to a sum (initially 0). When such a power is found, calculation is made to count the number of times it can be used to find the target index, and that value is added with the sum. The part of counting how many times a power can be used is similar to the "Bitwise binary search" from the binary search article.
- finId + power of base < array size(M)
- value at finId + power of base ≤ target
While iteration is complete check if the value at final index is same as target or not. If it is not, then the value is not present in the array.
Illustration: See the illustration below for better understanding.
Illustration: arr[] = {1, 4, 5, 8, 11, 15, 21, 45, 70, 100}, array size(M) = 10, target = 45, base(N) = 3
Steps:
- Compute first power(power) of 3 (Base) greater than 10 (M), power = 27 in this case.
- Initialize a final index(finId) as 0 (position in arr[] where target value should be at the end).
- Now iterate through the powers of 3 (Base) that are less or equal to 27 and add them to the index as fallowing:
- 1st iteration : finId = 0. power is 27 and can't be used because finId + power > M. So, finId = 0.
- 2nd iteration : finId = 0. power is 9 and can't be used because element at finId + power > target
i.e. arr[finId + power] > target. So finId = 0.- 3rd iteration : finId = 0. power is 3, this time power can be used because arr[finId + power] < target. Now count how many times 3 can be added to finId. In this case 3 can sum two times. finId after 3rd iteration will be 6 (finId += 3 + 3). 3 can't be added more than 2 times because finId will pass the index where target value is. So finId = 0 + 3 + 3 = 6.
- 4th iteration : finId = 6. power is 1, power can be used because arr[finId + power] ≤ target(45). Again count how many times 1 can be used. In this case 1 can be used only once. So add 1 only once with finId. So, finId = 6+1 = 7.
- after 4th iteration power is 0 so exit the loop.
- arr[7] = target. So the value is found at index 7.
Notes:
Below is the implementation of the above approach (in comparison with a classic binary search algorithm):
7
Time Complexity: O(log N M * log 2 N)
Auxiliary Space: O(1)