binary-search Questions

13

Solved

is there an algorithm that is faster than binary search, for searching in sorted values of array? in my case, I have a sorted values (could be any type values) in an A array, I need to return n if...
Basion asked 30/10, 2010 at 4:33

7

The following is the pseudocode I got from a TopCoder tutorial about binary search binary_search(A, target): lo = 1, hi = size(A) while lo <= hi: mid = lo + (hi-lo)/2 if A[mid] == target: ...
Equiangular asked 26/12, 2010 at 15:29

17

Solved

I was asked if a Binary Search is a divide and conquer algorithm at an exam. My answer was yes, because you divided the problem into smaller subproblems, until you reached your result. But the exa...

3

Solved

I've difficulty in understanding when to use: while (left < right ) { } vs when to use: while (left <= right ) { } Also while setting left and right boundaries sometimes we use: left ...
Counterpoint asked 25/7, 2019 at 4:0

2

Solved

I was looking into MIT's open courseware first lecture on an introduction to algorithms and there is something that is not terribly obvious to me. You cant start watching the lecture at 24:30 here ...
Jocosity asked 8/7, 2020 at 11:8

3

Solved

I was working through this problem on leetcode https://leetcode.com/problems/leftmost-column-with-at-least-a-one/ and I cant think of an intuitive answer. Why is the right (or high) pointer sometim...
Porthole asked 10/4, 2021 at 4:47

13

Solved

I have a sorted array and want to do binary search on it. So I'm asking if something is already available in Swift library like sort etc.? Or is there a type independend version available? Of cou...
Karee asked 9/8, 2015 at 12:48

2

Solved

I want to find if a number exists in a sorted array. To be straight an array contains fibonaccci number from 1 to 63. Below is the fibonacci number generator and some of it's output. stacksize = 10...
Geopolitics asked 11/6, 2019 at 6:0

3

Solved

Assuming a sorted list of integers as below: data = [1] * 3 + [4] * 5 + [5] * 2 + [9] * 3 # [1, 1, 1, 4, 4, 4, 4, 4, 5, 5, 9, 9, 9] I want to find the indices where the values changes, i.e. [3, 8,...
Cowskin asked 23/11, 2021 at 9:16

3

I came across this document Binary Search Revisited where the authors have proved/explained that binary search can be used for unsorted arrays (lists) as well. I haven't grokked much of the documen...
Caruthers asked 12/12, 2009 at 10:56

4

Solved

Tried to understand the solution for Codility NailingPlanks. Link for the Problem: https://app.codility.com/programmers/lessons/14-binary_search_algorithm/nailing_planks/ You are given two no...
Town asked 1/6, 2020 at 13:49

2

I know how binary search works but I always make small mistakes when I need to implement one. The following code is the solution for leetcode 287 find the duplicate number class Solution { public...
Everard asked 9/7, 2018 at 20:42

9

Solved

I have a List of object sorted and I want to find the first occurrence and the last occurrence of an object. In C++, I can easily use std::equal_range (or just one lower_bound and one upper_bound)....
Blanketing asked 24/3, 2013 at 20:50

7

Solved

Binary search is harder to implement than it looks. "Although the basic idea of binary search is comparatively straightforward, the details can be surprisingly tricky…" — Donald Knuth. Which...
Subclavius asked 2/2, 2009 at 18:35

9

Solved

I need a binary search function. I couldn't find any function in the standard library that will return the index of the found item, and if it wasn't found, will return the bitwise complement of th...
Volkslied asked 11/12, 2014 at 19:38

6

For an array with single peak element, it could be done in o(logn) using binary search, however in case of multiple peak elements in an array what approach should we use ? ---Putting some more inf...
Poche asked 16/8, 2017 at 12:36

1

Solved

Many common operations aren't built in to Raku because they can be concisely expressed with a combination of (meta) operators and/or functions. It feels like binary search of a sorted array ought t...
Reveille asked 7/7, 2021 at 21:49

1

Solved

I'm watching university lectures on algorithms and it seems so many of them rely almost entirely binary search trees of some particular sort for querying/database/search tasks. I don't understand t...

3

I am learning binary search and the sample code uses" low = mid +1 and high = mid -1" but I don't understand why we do not use "low = mid and high = mid " instead? def binarysearch(sequence, valu...
Rici asked 13/11, 2017 at 3:23

4

I am trying to write a binary search that takes a sorted list and finds the largest number less than a target value: def binary_max(list, target) hi=len(list)-1 lo=0 while lo<=hi: mid=(hi+lo...
Repp asked 16/11, 2013 at 19:47

3

Solved

This was the question, I was asked in interview. What is the best time complexity you can get to find a min and max of array? I replied: O(n). Iterate through the array, keeping track of the max ...

4

This has been bothering me for a while. I know that given N keys to arrange in the form of a binary search tree, the possible number of trees that can be created correspond to the Nth number from t...
Johnjohna asked 30/8, 2009 at 1:7

3

For most such operations, we're using the lodash library. I'm open to other suggestions, but would likely just write the function myself before importing a new lib. lodash has sortedIndexOf, which...
Augustus asked 8/6, 2016 at 19:51

4

Solved

I am using lambda expressions to sort and search an array in C#. I don't want to implement the IComparer interface in my class, because I need to sort and search on multiple member fields. class W...
Dylan asked 2/2, 2011 at 2:25

3

Solved

How I can get the number of iterations of binary search? This is my code: int main() { int target = 11; int N = 10; std::vector<int> index; int num; for (int i = 0; i < N; i++) {...
Rill asked 4/5, 2019 at 21:39

© 2022 - 2024 — McMap. All rights reserved.