# Questions tagged [binary-search]

Use this tag for code that implements a binary search.

305 questions
Filter by
Sorted by
Tagged with
139 views

### Creating a search function

Write an efficient function in the Python language called SmartSearch. This function will receive a sorted arr array of integers of size $n$ containing at the ...
• 21
183 views

### Testing binary search module, based on bisect

The bisect module can only find the index to insert an element. I created a simple class based on the bisect module for precise searching and covered all the edge cases that came to mind with tests. ...
• 153
123 views

### Incremental upper bound in sorted range

Since initial question Increasing binary search for mismatch in sorted range contained defects in code and can confuse the readers, here is the updated version of the question. Is the code below ...
194 views

### Counting duplicate elements in two sorted arrays

I've been working on an assignment that involves optimizing a duplicate finding algorithm for sorted arrays, and I'd like to get your thoughts on the implementation. Here's the code I've come up with: ...
• 31
47 views

### Finding the pivot point in a rotated array which has been shifted from kth element

I'm trying to solve the following problem: Joker has a deck of n cards, each with a value written on it. The cards are arranged in ascending order from top to bottom. Joker's master performs a hidden ...
• 210
293 views

...
• 143
2k views

### Optimizing the Egg Drop Problem implemented with Python

I've implemented the Egg Drop Problem using Python. As described in the code, I'm trying to find the highest floor of a 102-story building from which I could drop an egg without it breaking. The ...
1k views

### C++ Binary Search

Could you advise on this binary search implementation? ...
• 107
140 views

...
1 vote
100 views

### Binary search of 2D matrix

I have written code as below: ...
• 127
1 vote
109 views

### USACO Silver "Wormhole Sort" solver

I'm struggling a bit with a USACO silver question using python, http://usaco.org/index.php?page=viewproblem2&cpid=992. The question provides an unsorted list of numbers (cows) and a number of ...
• 11
2k views

### Find closest value in a big list

There are solutions e.g. list.OrderBy(item => Math.Abs(number - item)).First() or ...
• 739
378 views

### Implementation of a market that matches bids to offers

This is my task: Each line in the file can be one of the following: Updates to the limit order book in the following format: ...
1k views

### Non recursive binary search in C

I wrote a non recursive binary search implementation in C, for this time I think there are no bugs. Here's the code: ...
• 97
269 views

### Increase efficiency of stick cutting algorithm

I have this typical code interview type problem. Suppose you have a stick of length n. You make X cuts in the stick at places x1, x2, x3 and so on. For every cut in X cuts you need to print the length ...
37 views

### Improving time complexity for codechef Que [closed]

Question Link. Binary Search and Two Pointers related question. For the above-mentioned question, In other people's solutions, people get AC with a binary search solution. However, I get TLE with ...
952 views

### A generic binary search function in C++

I have written the following generic function template: ...
• 747
868 views

### C++ Binary search using SIMD

Recently I found that the binary search (std::ranges::lower_bound and std::ranges::upper_bound) is the main bottleneck in my ...
• 1,703
142 views

### Implementation and Testing of Exponential Search for scenario where we search the same array/list many times

Exponential Search is an optimization over binary search. Conceptually, when searching for a number target in a list of numbers ...
• 456
164 views

### Calculating square roots using binary search

I'm trying to find the square root of a number (num) using binary search in Rust. I'm new to Rust, but I've done quite a bit of programming in other languages, ...
• 123
1 vote
627 views

### Duplicate Binary Search Improvements

I'm working on an implementation of binary search (in Python) that can operate on a sorted array that may have duplicate elements. I've written a submission that has managed to hold up against my test ...
222 views

### Counting the array spots visited by binary searches (Java)

So the idea was to find out how many times each array component is considered by binary search when we search all possible needles for the haystack: ...
• 28.2k
253 views

### Binary search algorithm (Python 3.9)

New to using this platform and would like to familiarise myself with it so would just like to ask if I am exhibiting good practice with my programming. Started the online CS50 course today and thought ...
259 views

...
423 views

### BinarySearchTree toString method

I have my ToString method right here for my BinarySearchTree class ...
1 vote
704 views

### size() method for a binary search tree

This is my size method for my binary search tree, that is meant to implement recursion to calculate the tree's size. ...
682 views

### Binary Search of Array in C#

I am an algo-newbie and this is my first attempt at writing binary search and it worked on the first try. But something about it tells me that its far from perfect. Please tell me how I can improve. <...
• 133
1 vote
95 views

### Binary Search using recursive approach

Is the following implementation of recursive function correct? How about the time complexity of the code; is it good in terms of performance? ...
1 vote
1k views

### HackerRank - Climbing the Leaderboard: I don't understand why my code is exceeding the time limit

Climbing the Leaderboard An arcade game player wants to climb to the top of the leaderboard and track their ranking. The game uses Dense Ranking, so its leaderboard works like this: The player with ...
• 147
1 vote
141 views

### Finding the closest values in a sorted list that don't go under/over

For my project I have a sorted array of objects of timestamps (in milliseconds) and I need two functions. One function should find the closest time that doesn't go over a specific amount, and the ...
• 193
129 views

### Recursive Binary Search in Typescript

I've ran a few tests, not extensive though, and it apparently works. Like many standard binary search algorithms, if the number returned is < 0, you can take the complement (~) of the returned ...
• 229
152 views

### Python list subclass with O(1) prepend / How to get bisect to work with reverse sorted list

For a particular problem, I need to have a list that: supports O(1) prepend, and that I can call bisect.bisect_left on, which ...
• 456
251 views

### Climbing the leaderboard (Hacker Rank) via binary search

I am trying to solve the Climbing the leaderboard problem on Hacker Rank. My code passes all the test cases except Test Cases 6 to 9, which I assume are using large data sets. I'm using binary search ...
• 43
149 views

### Binary search to find the closest element

I wrote a binary search to find the closest element to x in an array. How can show the correctness of the algorithm for any cases? Because it handles with floating points and variable length arrays, ...
130 views

### JavaScript Binary Search -- first try

EDIT: code snippet added, check the output in JS console I wrote the following binary search algorithm in JS, for the first time in my experience. The variable ...
1 vote
470 views

### Search Method In Python / Python 3.9

I have implemented Binary_Search in python (I think), and my own method for range objects. I want to know if there is any way to improve this code, here it is: <...
• 105
125 views

### Finding inorder successor of a BST in C [closed]

Please review this code I have written a function to find the successor of BST in c. It takes three arguments, i.e root pointer to the binary search tree, node value of which we have to find a ...
87 views

### Binary Search Algorithm Difference

As far as I test, there is no any bug point tested. However, I wonder that if there is a problem with the usage of arr.Length or ...
1 vote
140 views

### How can I optimize this Binary Search Tree?

I've added some methods to a basic BST implementation for integers - if anyone can point out some ways that this could be written in a more efficient or clean manner, please let me know. ...
1 vote
114 views

### How to make this special binary-search algorithm more rusty?

I am solving one binary search problem in LeetCode. I have accomplished such code: ...
1 vote
253 views

### Sorting and Searching Algorithm

The searching algorithm that I created is dependent upon the sorting algorithm (which many of you have seen in my previous question). I believe that the sorting algorithm can't be better (for beginner-...
• 434
91 views

### Subtracting elements of datasets of an HDF5 file

I am trying to solve the following problem: Input: Input is two arrays (Nx4, sorted in column-2) stored in datasets-1 and 2 in HDF5 file (input.h5). N is huge (...
• 61
1 vote
170 views

### Binary Search recursive & iterative solution

I have implemented binary search solution by using recursion and iterative approach. I there any way to improve it? Tests ...
1k views

### x64 NASM Assembler: Binary Search

While quarantine I decided to look into the assembly language, as it's quite interesting to see how code works on the lower levels. To challenge myself I made a binary search algorithm. The OS is ...
1 vote
354 views

### Faster binary search to find the index in Python?

I am implementing a binary search algorithm and my code is as shown bellow: ...
• 325
936 views

### Count number of previous elements greater than its element at present index

Suppose I have a vector containing n elements. I want to find out the number of previous elements greater than its element at present index i. I want to find ...
• 21
156 views

### Leetcode Search in Rotated Sorted Array

I was doing Search in sorted array question from leetcode Question Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,...
• 495
93 views

### Basic binary search in python

I just started learning to code during quarantine and whilst learning python, I created this program for a binary search. This is really basic so I assume that there is a way easier method. If anyone ...
Straight insertion sort When inserting an element into its proper location to the left, one can achieve that by $n$ adjacent swaps which totals to $3n$ assignments. Straight insertion sort, ...