Time complexity of finding range of target in sorted array – Is this solution O(N) in the worst case?

Advertisements I was going through LeetCode problem 34. Find First and Last Position of Element in Sorted Array, which says: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. If target is not found in the array, return [-1, -1]. You must write… Read More Time complexity of finding range of target in sorted array – Is this solution O(N) in the worst case?

Can't typecast to int in C; Error: Expected expression before int

Advertisements int arr5[8]={10,20,30,40,50,60,70,80}; len=8; int beg=0,mid,end=len-1,loc; mid=int((beg+end)/2); printf("%d",mid); I was trying to write a code for binary search but can’t typecast the value of (beg+end)/2 to mid. error: expected expression before ‘int’ mid=int((beg+end)/2); ^~~ >Solution : For starters using the type specifier int in this statement mid=int((beg+end)/2); does not make a sense. It is enough… Read More Can't typecast to int in C; Error: Expected expression before int

What is the difference between “`lo == 0, hi == len(cards) – 1“` and “`lo, hi = 0, len(cards) – 1“`

Advertisements I am currently practicing binary search and I’m having trouble understanding the difference in syntax between lo == 0, hi == len(cards) – 1 and lo, hi = 0, len(cards) – 1. The code works with the second method but not with the first one. Any help would be appreciated. Thank you. The code… Read More What is the difference between “`lo == 0, hi == len(cards) – 1“` and “`lo, hi = 0, len(cards) – 1“`

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

Advertisements 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… 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

Advertisements 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… Read More Logic Error: Binary search fails with more than two elements in a string array