Questions tagged [binary-search]

Use this tag for code that implements a binary search.

Filter by
Sorted by
Tagged with
2 votes
3 answers
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 ...
SHOVAL's user avatar
  • 21
5 votes
1 answer
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. ...
Aycon's user avatar
  • 153
3 votes
1 answer
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 ...
Damir Tenishev's user avatar
3 votes
2 answers
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: ...
Bryan C's user avatar
  • 31
-1 votes
0 answers
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 ...
driver's user avatar
  • 210
4 votes
1 answer
293 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
140 views

Lower-Bound algorithm getting Time Limit Exceeded

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

Binary search of 2D matrix

I have written code as below: ...
Szyszka947's user avatar
1 vote
0 answers
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 ...
Luke's user avatar
  • 11
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
  • 739
3 votes
2 answers
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: ...
Данил Денк'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
4 votes
3 answers
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 ...
Some nerd who does not have a 's user avatar
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 ...
High On Math's user avatar
8 votes
3 answers
952 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
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 ...
frozenca's user avatar
  • 1,703
6 votes
1 answer
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 ...
joseville's user avatar
  • 456
2 votes
3 answers
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, ...
cocomac's user avatar
  • 123
1 vote
2 answers
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 ...
Stanley Yu's user avatar
4 votes
3 answers
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: ...
coderodde's user avatar
  • 28.2k
7 votes
1 answer
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 ...
Liam Bolton's user avatar
0 votes
0 answers
259 views

Python one-line binary search

...
jarOfCode's user avatar
3 votes
3 answers
423 views

BinarySearchTree toString method

I have my ToString method right here for my BinarySearchTree class ...
Rasmus Steen's user avatar
1 vote
2 answers
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. ...
Rasmus Steen's user avatar
3 votes
2 answers
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. <...
Aniruddha's user avatar
  • 133
1 vote
2 answers
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? ...
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
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 ...
Ryan Peschel's user avatar
2 votes
1 answer
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 ...
TKoL's user avatar
  • 229
0 votes
2 answers
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 ...
joseville's user avatar
  • 456
0 votes
1 answer
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 ...
MM360's user avatar
  • 43
0 votes
1 answer
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, ...
CyborgBeta's user avatar
4 votes
1 answer
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 ...
one-hand-octopus's user avatar
1 vote
2 answers
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: <...
Sam's user avatar
  • 105
0 votes
1 answer
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 ...
Abrar Ajaz Wani's user avatar
-2 votes
2 answers
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 ...
Soner from The Ottoman Empire's user avatar
1 vote
2 answers
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. ...
SomeoneLearning17's user avatar
1 vote
1 answer
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: ...
prehistoricpenguin's user avatar
1 vote
2 answers
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-...
seoul_007's user avatar
  • 434
6 votes
0 answers
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 (...
nuki's user avatar
  • 61
1 vote
1 answer
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 ...
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
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: ...
Lerner Zhang's user avatar
2 votes
1 answer
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 ...
sukesh's user avatar
  • 21
2 votes
1 answer
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,...
iRohitBhatia's user avatar
2 votes
2 answers
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 ...
Anonymous's user avatar
2 votes
3 answers
357 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
  • 28.2k
2 votes
1 answer
317 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

1
2 3 4 5
7