3,028 questions
-1
votes
0
answers
26
views
Java error: missing return statement in BinSearch [duplicate]
I am figuring out searching algorithms and when I copied the following code, I received the error "missing return statement".
I would also like to know why int k (supposedly the number I'm ...
0
votes
1
answer
79
views
Clojure mutant – can't kill a surviving mutant in binary search
I'm writing a classic binary search function in Clojure and testing it with the mutant mutation testing library.
My tests work fine in terms of correctness, but mutation testing reports one surviving ...
0
votes
2
answers
79
views
Find start index of a rotated non-decreasing array
This is part of a interview question I recently came across and Im not able to find an elegant solution for this. Can someone please help.
Q - We have a non-decreasing sorted array, which is rotated ...
-3
votes
3
answers
109
views
How to do binary search correctly [closed]
I am trying to solve this question on Leetcode:
74. Search a 2D Matrix
You are given an m x n integer matrix matrix with the following two properties:
Each row is sorted in non-decreasing order.
The ...
-3
votes
2
answers
74
views
Binarysearch with Python with recursion
def binarysearch(a,n):
mid=int(len(a)/2) #taking the middle point
if(n==a[mid]):
return mid
elif(n>a[mid]): #if greater then do binarysearch in smaller part
binarysearch(a[(mid+1):],n)
...
0
votes
0
answers
37
views
Finding an element in the matrix with a least amount of steps
I have a W x H matrix, random starting position x0,y0, and the target with unknown coordinates. On every step I get information of direction to the target (above-below, left-right) and then have to ...
1
vote
2
answers
59
views
What is the idiomatic way to handle code duplication in match bindings for Result? [duplicate]
I'll give some context here. The closure foo takes in a character and inserts it into a sorted vector. foo uses binary_search_by_key to insert the character into the vector at the correct index.
What ...
0
votes
0
answers
60
views
Generic binary search Java with Comparator and Comparable [duplicate]
I'm being asked to create a binary search that can use either Comparable or Comparator depending on which constructor is being used. I made a private boolean to note whether or not to use the object's ...
1
vote
1
answer
56
views
When are bisect_left(arr, x) and bisect_right(arr, x-1) equal?
If you are doing this on a sorted array arr then would bisect_left(arr, x) == bisect_right(arr, x-1) under any circumstances?
0
votes
2
answers
115
views
How does this code manage to correctly find the peak element's array index using binary search on an unsorted array?
The below code is using binary search. Since the array is not sorted, how does it manage to correctly figure out the peak element's index? Generally it works on sorted arrays only, right?
Input: arr = ...
-3
votes
2
answers
102
views
What are the arguments of comp for lower_bound() in C++?
I'm trying to create a custom comparison function for the binary search method lower_bound(). I've tried reading the docs and searching, but I can't figure out how the arguments of the comp function ...
0
votes
2
answers
31
views
Binary search 704 leetcode calling a function in python 3
class Solution:
def search(self, nums: List[int], target: int) -> int:
#establish a left and right limit
l = 0
#this will take from the right and push more ...
0
votes
0
answers
66
views
How to accurately measure memory usage for iterative and recursive binary search algorithms in Java?
I am benchmarking the performance of iterative and recursive binary search algorithms in Java, specifically measuring both execution time and memory usage for different dataset sizes. However, I am ...
1
vote
0
answers
73
views
Are There More Efficient Awaiters for C++ Coroutines Than std::suspend_always?
I’m attempting to optimize a binary search workload using C++ coroutines on a 1GB sorted array of unique random integers to induce multiple memory misses. The approach involves using co_await when ...
1
vote
1
answer
79
views
Using the compare() function with strings
I have to use a binary search to find a movie that the user inputs, however, when doing certain movies, or movies that aren't in the list, the search loops forever. I was told it was cause I can't use ...