Binary Search Variations Application

Revision en10, by Supersidd, 2024-07-11 15:36:46

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).

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

Their corresponding decreasing array counterparts:

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

Important Takeaways

The main point we can gleam from these algorithms is that when you get an element that satisfies the condition, you immediately set result = mid. Although quite obvious some algorithms I read don't set a different variable for result and only take the value of mid after the loop ends as the answer. This can be quite problematic sometimes. Another important point to note is that the pointer we move (l or r) depends on whether we want the first element or the last element, not whether the array is ascending or descending. Also, I

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)