Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [binary-search]

Use this tag for code that implements a binary search.

Filter by
Sorted by
Tagged with
8 votes
5 answers
1k views

LeetCode 977: Squares of a sorted array, maintaining non-decreasing order

I tried solving the LeetCode question like many others, trying to incorporate the O(n) time complexity requirement. Squares of a Sorted Array Given an integer array ...
Littlejacob2603's user avatar
3 votes
3 answers
206 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 ...
SHOVAL's user avatar
  • 61
5 votes
1 answer
242 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. ...
Aycon's user avatar
  • 225
3 votes
1 answer
203 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 ...
Damir Tenishev's user avatar
3 votes
2 answers
252 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: ...
Bryan C's user avatar
  • 31
4 votes
1 answer
357 views

K&R exercise 3-1

...
Hansoko's user avatar
  • 143
9 votes
3 answers
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 ...
Muhammed Abiola's user avatar
4 votes
3 answers
1k views

C++ Binary Search

Could you advise on this binary search implementation? ...
acd's user avatar
  • 107
-2 votes
2 answers
169 views

Lower-Bound algorithm getting Time Limit Exceeded

...
Shakhawat Hossain SHIHAB's user avatar
1 vote
1 answer
144 views

Binary search of 2D matrix

I have written code as below: ...
Szyszka947's user avatar
2 votes
1 answer
150 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 (...
Luke's user avatar
  • 21
3 votes
1 answer
2k views

Find closest value in a big list

There are solutions e.g. list.OrderBy(item => Math.Abs(number - item)).First() or ...
nop's user avatar
  • 819
3 votes
2 answers
447 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: ...
Данил Денк's user avatar
3 votes
2 answers
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: ...
manungsa's user avatar
  • 107
4 votes
3 answers
302 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 ...
Some nerd who does not have a 's user avatar
0 votes
1 answer
54 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 ...
High On Math's user avatar
8 votes
3 answers
1k views

A generic binary search function in C++

I have written the following generic function template: ...
digito_evo's user avatar
4 votes
1 answer
1k 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 ...
frozenca's user avatar
  • 1,751
6 votes
1 answer
189 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 ...
joseville's user avatar
  • 516
2 votes
3 answers
226 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, ...
cocomac's user avatar
  • 123
1 vote
2 answers
725 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 ...
Stanley Yu's user avatar
4 votes
3 answers
291 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: ...
coderodde's user avatar
  • 32.2k
7 votes
1 answer
310 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 ...
Liam Bolton's user avatar
3 votes
3 answers
515 views

BinarySearchTree toString method

I have my ToString method right here for my BinarySearchTree class ...
Rasmus Steen's user avatar
1 vote
2 answers
766 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. ...
Rasmus Steen's user avatar
3 votes
2 answers
810 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. <...
Aniruddha's user avatar
  • 133
1 vote
2 answers
149 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? ...
Siawash Kasra's user avatar
1 vote
1 answer
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 ...
No Name's user avatar
  • 147
1 vote
1 answer
182 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 ...
Ryan Peschel's user avatar
2 votes
1 answer
155 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 ...
TKoL's user avatar
  • 229
3 votes
2 answers
240 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 ...
joseville's user avatar
  • 516
1 vote
1 answer
289 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 ...
MM360's user avatar
  • 63
0 votes
1 answer
181 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, ...
CyborgBeta's user avatar
4 votes
1 answer
157 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 ...
one-hand-octopus's user avatar
1 vote
2 answers
520 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: <...
Sam's user avatar
  • 147
0 votes
1 answer
167 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 ...
Abrar Ajaz Wani's user avatar
-2 votes
2 answers
109 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 ...
Soner from The Ottoman Empire's user avatar
1 vote
2 answers
160 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. ...
SomeoneLearning17's user avatar
1 vote
1 answer
133 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: ...
prehistoricpenguin's user avatar
2 votes
2 answers
294 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-...
seoul_007's user avatar
  • 454
6 votes
0 answers
120 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 (...
nuki's user avatar
  • 61
1 vote
1 answer
186 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 ...
Neslihan Bozer's user avatar
7 votes
1 answer
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 ...
christian_schmidt's user avatar
1 vote
1 answer
396 views

Faster binary search to find the index in Python?

I am implementing a binary search algorithm and my code is as shown bellow: ...
Lerner Zhang's user avatar
2 votes
1 answer
996 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 ...
sukesh's user avatar
  • 21
2 votes
1 answer
175 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,...
iRohitBhatia's user avatar
2 votes
2 answers
112 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 ...
Anonymous's user avatar
3 votes
3 answers
434 views

Comparing binary insertion sort with straight insertion sort in Java

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, ...
coderodde's user avatar
  • 32.2k
2 votes
1 answer
351 views

Binary Search implementation: HackerRank - Climbing the Leaderboard

My code passes 11 out of 12 test cases. I was wondering where I can improve my code. NOTE: This code needs performance improvement, as it is working for most of the cases. To mu knowledge it would ...
user10859's user avatar
3 votes
2 answers
530 views

Sorting an array using a Binary Search Tree with C++

Given an array of integer (all different from each other), this program creates a BST with the array elements in order to sort them and put them back into the array, using the BST properties. ...
Vittorio A.'s user avatar

1
2 3 4 5
7