Binary Search Variations Application

Правка en3, от Supersidd, 2024-07-11 12:57:50

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 for Array with distinct Elements (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, specialist

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en15 Английский Supersidd 2024-07-11 16:18:19 0 (published)
en14 Английский Supersidd 2024-07-11 15:58:06 2 Tiny change: ' key = 5\nEx- [1, ' -> ' key = 5\n\nEx- [1, '
en13 Английский Supersidd 2024-07-11 15:56:57 14
en12 Английский Supersidd 2024-07-11 15:56:03 178
en11 Английский Supersidd 2024-07-11 15:49:47 636
en10 Английский Supersidd 2024-07-11 15:36:46 1125
en9 Английский Supersidd 2024-07-11 15:17:29 16
en8 Английский 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 Английский Supersidd 2024-07-11 14:36:13 0 Tiny change: '^ ^ ^ ^\n\nThe' -> '^ ^ ^\n\nThe'
en6 Английский Supersidd 2024-07-11 14:34:45 37 Tiny change: ' ^ ^\n\n | | | |\n\nThese ' -> ' ^ ^\n\nThese '
en5 Английский Supersidd 2024-07-11 14:34:01 4
en4 Английский Supersidd 2024-07-11 14:33:43 686
en3 Английский Supersidd 2024-07-11 12:57:50 1027
en2 Английский Supersidd 2024-07-11 12:47:17 331
en1 Английский Supersidd 2024-07-11 12:45:27 2257 Initial revision (saved to drafts)