Binary Search Variations Application

Revision en8, by Supersidd, 2024-07-11 15:15:02

My first Codeforces blog post! Hooray!

This post was inspired by the problem: E. Iva & Pav of Codeforces round 900 (https://codeforces.me/contest/1878/problem/E). This problem requires us to find the last element in the array such that the Bitwise AND values of all elements starting from the query index are greater than or equal to key value. The important takeaway of this problem is that the Bitwise AND value can only decrease after including another element. This permits us to use Binary Search, which brings us to our topic.

The Problem

Unfortunately being the newbie that I am, I tried applying basic binary search, lower bound and upper bound but didn't succeed. Here we are required to find the last element which satisfies the given condition thus requiring a slightly different binary search algorithm. This sparked my urge to dive deep into the bowel of Binary Search. So After years (about 0.01 years) of painstaking research I finally compiled a list of five algorithms that will satisfy most problems regarding binary search. It also lead to better my understanding of the binary search algorithm.

The Algorithms

The Basic Binary search algorithm only applies for arrays with distinct elements. If the array has multiple instances of one element we need to apply a different algorithm. For example you could need any of the following elements for your specific problem.

Ex- [1, 2, 3, 5, 6, 8, 9, 10] key = 5 Ex- [1, 1, 1, 2, 2, 3, 4, 4, 4, 7, 7, 9] key = 4

These are the five algorithms for ascending order respectively in the order of the index of required element in array:

Binary Search for strictly increasing Array (Basic Binary Search)
Binary Search Lower Bound (First Element >= Key)
Binary Search Upper Bound (First Element > Key)
Binary Search for Last element <= Key
Binary Search for Last element < Key

Binary Search for strictly increasing Array (Basic Binary Search)

while(l <= r) { mid = (l+r)/2;

if(arr[mid] == key)
{
    result = mid;
    break;
}
else if(arr[mid] > key)
{
    l = mid+1;
}
else
{
    r = mid-1;
}

} ``` <\spoiler>

Binary Search Last Element > Key

while(l <= r) { mid = (l+r)/2;

if(arr[mid] > key)
{
    result = mid;
    l = mid+1;
}
else
{
    r = mid-1;
}

} ``` <\spoiler>

Binary Search Lower Bound (First Element <= Key)

while(l <= r) { mid = (l+r)/2;

if(arr[mid] <= key)
{
    result = mid;
    r = mid-1;
}
else
{
    l = mid+1;
}

} ``` <\spoiler>

Binary Search Last Element >= Key

while(l <= r) { mid = (l+r)/2;

if(arr[mid] >= key)
{
    result = mid;
    l = mid+1;
}
else
{
    r = mid-1;
}

} ``` <\spoiler>

Binary Search Upper Bound (First Element < Key)

while(l <= r) { mid = (l+r)/2;

if(arr[mid] < key)
{
    result = mid;
    r = mid-1;
}
else
{
    l = mid+1;
}

} ``` <\spoiler>

Tags binary search, specialist

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en15 English Supersidd 2024-07-11 16:18:19 0 (published)
en14 English Supersidd 2024-07-11 15:58:06 2 Tiny change: ' key = 5\nEx- [1, ' -> ' key = 5\n\nEx- [1, '
en13 English Supersidd 2024-07-11 15:56:57 14
en12 English Supersidd 2024-07-11 15:56:03 178
en11 English Supersidd 2024-07-11 15:49:47 636
en10 English Supersidd 2024-07-11 15:36:46 1125
en9 English Supersidd 2024-07-11 15:17:29 16
en8 English Supersidd 2024-07-11 15:15:02 1858 Tiny change: 'oiler>\n\n ' -> 'oiler>\n\n<spoiler summary="">\n```c++\n\n```\n<\spoiler>'
en7 English Supersidd 2024-07-11 14:36:13 0 Tiny change: '^ ^ ^ ^\n\nThe' -> '^ ^ ^\n\nThe'
en6 English Supersidd 2024-07-11 14:34:45 37 Tiny change: ' ^ ^\n\n | | | |\n\nThese ' -> ' ^ ^\n\nThese '
en5 English Supersidd 2024-07-11 14:34:01 4
en4 English Supersidd 2024-07-11 14:33:43 686
en3 English Supersidd 2024-07-11 12:57:50 1027
en2 English Supersidd 2024-07-11 12:47:17 331
en1 English Supersidd 2024-07-11 12:45:27 2257 Initial revision (saved to drafts)