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[2]; out[0] =… Read More Arrays.binarySearch not finding the element

Binary Search enters a infinite loop when I use a 'for' Loop but works fine 'while' while loop

I am learning Binary Search and these are my attempts. So here are two pieces of code, both of them being same with the only difference being one is in while loop while the other is in for loop. int binarySearch(int *input, int n , int val) { int high = n-1, low =0; while… Read More Binary Search enters a infinite loop when I use a 'for' Loop but works fine 'while' while loop

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[20]={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

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[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 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“`