# Arrays.binarySearch not finding the element

I have this piece of code: class Solution { public int[] twoSum(int[] nums, int target) { for(int i = 0;i<nums.length;i++){ int n = target – nums[i]; int temp = nums[i]; nums[i] = Integer.MIN_VALUE; System.out.println("nums: "+ Arrays.toString(nums)); System.out.println("n: "+n); int result = Arrays.binarySearch(nums,n); System.out.println("result: "+result); if (result >= 0){ int[] out = new int; out =… Read More Arrays.binarySearch not finding the element

# Why doesn't this code for a binary search function in c++ work?

#include <iostream> int binary_search(int arr[], int size, int target) { int first = 0; int last = size – 1; int midpoint = (last + first) / 2; while(first <= last) { if(arr[midpoint] == target) { return midpoint; } else if(arr[midpoint] < target) { first = midpoint + 1; } else if(arr[midpoint] > target) {… Read More Why doesn't this code for a binary search function in c++ work?

# Binary search array showing -1 as result

#include<stdio.h> int binary_search(int arr[], int size ,int element){ int low, mid, high; low=0; high=size-1; //start of search while(low<=high) { mid = (low + high)/2; if(arr[mid] == element){ return mid; } if(arr[mid]<element){ low= mid+1; } else{ high = mid-1; } } //end of search return -1; } int main(){ int arr={1,20,31,44,54,68,70,85}; int size= sizeof(arr)/sizeof(int); int element=44;… Read More Binary search array showing -1 as result

# How do I call the binary search 10 times?

#I want to call the binary search 10 times (make the function run 10 times). Im supposed to that and then make a graph out of the result, which I know how to do. I just cant make the function run 10 times to get the information for the graph. import matplotlib.pyplot as plt import… Read More How do I call the binary search 10 times?

# Why is this code not printing the value returned by the binarySearch() function?

#include<bits/stdc++.h> using namespace std; int binarySearch(int [], int, int, int); int main() { int n, ar, givensum; cout << "Enter the size of the array: "; cin >> n; for(int i = 0; i<n; i++) { cout << "ar[" << i << "] = "; cin >> ar[i]; } cout << "Enter the given sum:… Read More Why is this code not printing the value returned by the binarySearch() function?

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

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

int arr5={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 to… 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“`

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

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