Questions tagged [binary-search]
Use this tag for code that implements a binary search.
302
questions
2
votes
2
answers
117
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:
...
0
votes
0
answers
36
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 ...
4
votes
1
answer
191
views
K&R exercise 3-1
...
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 ...
4
votes
3
answers
1k
views
C++ Binary Search
Could you advise on this binary search implementation?
...
-2
votes
2
answers
107
views
1
vote
1
answer
83
views
Binary search of 2D matrix
I have written code as below:
...
1
vote
0
answers
75
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 ...
3
votes
1
answer
990
views
Find closest value in a big list
There are solutions e.g.
list.OrderBy(item => Math.Abs(number - item)).First()
or
...
3
votes
2
answers
297
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:
...
3
votes
2
answers
560
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:
...
4
votes
3
answers
256
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 ...
0
votes
1
answer
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 ...
8
votes
3
answers
855
views
A generic binary search function in C++
I have written the following generic function template:
...
4
votes
1
answer
527
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 ...
6
votes
1
answer
127
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 ...
2
votes
3
answers
137
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, ...
1
vote
2
answers
468
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 ...
1
vote
1
answer
76
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:
...
7
votes
1
answer
228
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 ...
0
votes
0
answers
214
views
3
votes
3
answers
231
views
BinarySearchTree toString method
I have my ToString method right here for my BinarySearchTree class
...
1
vote
2
answers
484
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.
...
3
votes
2
answers
487
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.
<...
1
vote
2
answers
86
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
1
answer
875
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 ...
1
vote
1
answer
117
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 ...
2
votes
1
answer
113
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 ...
0
votes
2
answers
133
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 ...
0
votes
1
answer
210
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 ...
0
votes
1
answer
132
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, ...
4
votes
1
answer
115
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
2
answers
465
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:
<...
0
votes
1
answer
119
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 ...
-2
votes
2
answers
72
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
2
answers
116
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
1
answer
104
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
2
answers
248
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-...
6
votes
0
answers
84
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 (...
1
vote
1
answer
150
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
...
7
votes
1
answer
941
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
1
answer
325
views
Faster binary search to find the index in Python?
I am implementing a binary search algorithm and my code is as shown bellow:
...
2
votes
1
answer
819
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 ...
2
votes
1
answer
150
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,...
2
votes
2
answers
91
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 ...
2
votes
3
answers
332
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, ...
2
votes
1
answer
297
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 ...
3
votes
2
answers
427
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.
...
3
votes
1
answer
41
views
I need to know that if everything okay in my BTS put and get method
I implement BinaryTree (K extends Comparable, V) concept to binarySearchTree and I have written get and put method. As far as I check, there is no problem in my code. So now I wonder that are there ...
2
votes
1
answer
2k
views
Simple BST implementation using C++ Struct
I am trying to implement DSs in C++ and this is BST insert and search functions. I tried in two different ways, please review and see if one is advantageous over other.
With pointer to pointer
...