Binary Search Variations Application

Revision en2, by Supersidd, 2024-07-11 12:47:17

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.

The Solution 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.

So the main difference between these algorithms is whether they return the first element or last element satisfying required condition. To illustrate their outputs, consider an array, Ex- [1, 1, 1, 2, 2, 3, 4, 4, 4, 7, 7, 9] key = 4 In this array basic binary search algorithm wont work as there are repeating values. If you use lower bound or upper bound algorithm, it will output the first element satisfying the condition (lets say >= or > key respectively) which is index 6 and 9 respectively. But we could require the last element that is equal to key i.e at index 8. This leads us to five different algorithms.

The Algorithms These are the five algorithms for ascending order and they all have their descending order counterparts as well.

Binary Search Lower Bound (First Element >= Key)
Binary Search Upper Bound (First Element > Key)
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)