# Binary search: Not getting upper & lower bound for very large values

I’m trying to solve this cp problem, UVA – The Playboy Chimp using Python but for some reason, the answer comes wrong for very large values for example this input: 5 3949 45969 294854 9848573 2147483647 5 10000 6 2147483647 4959 5949583 Accepted output: 3949 45969 X 3949 9848573 X 3949 45969 294854 9848573 My… Read More Binary search: Not getting upper & lower bound for very large values

# Logic Error: Binary search fails with more than two elements in a string array

The objective is to return the index of an element in a string array if present. The method uses a basic binary search using compareTo statements. With more than two elements in a tested array, the method will not detect the present element and return -1. The Java code this question is referring to is… Read More Logic Error: Binary search fails with more than two elements in a string array

# Why is my Binary search stuck in an endless loop?

I just had to pop in here to hopefully get a quick answer to my little problem. I’m trying to create a binary search method but ran into some problems. I created it iteratively, which my IDE(Intellij) apparently didn’t like. It had me stuck in a endless loop of… well, you know the rest. Any… Read More Why is my Binary search stuck in an endless loop?

# Search Insert Position-Wrong answe

I’m solving a problem (leetcode 35). My code was accepted in run code result but when I submit it returns wrong answer.I don’t really understand what is wrong in my answer . Given a sorted array and a target value, return the index if the target is found. If not, return the index where it… Read More Search Insert Position-Wrong answe

# Binary Search Output giving wrong answer

This is my code: def binary_search(keys, query): low, high = 0, 0 while low <= high: midpoint = low + (high – low) // 2 if query == keys[midpoint]: return midpoint elif keys[midpoint] > query: high = midpoint – 1 else: low = midpoint + 1 return -1 if __name__ == ‘__main__’: num_keys = int(input())… Read More Binary Search Output giving wrong answer

# Binary Search: Program doesn't terminate

I’ve been trying to learn algorithms and as part of this I have been trying to code binary search and the logic seems fine. The code doesn’t terminate and the IDE stays idle forever. I don’t understand what I’m doing wrong. Any help is appreciated. Thanks in advance! public class BinarySearch { public static void… Read More Binary Search: Program doesn't terminate

# How does this binary search works?

I saw this method in a book, to do binary search, but I can’t understand how it is working no matter how I try. Can someone explain to me exactly how it is working? the book’s explanation did not help : The idea is to make jumps and slow the speed when we get closer… Read More How does this binary search works?