Leetcode bug? (Function isn't stopping even after it returns in binary search)

Advertisements class Solution { public: int search(vector<int>& nums, int target) { int a = 0; int b = nums.size()-1,mid; while (b>a){ mid = (a+b)/2; cout<<a<<" "<<b<<" "<<mid<<endl; if (nums[mid]==target) { cout<<"ret"; return mid; } else if (nums[mid]>target){ b = mid; }else{ a = mid; } } return -1; } }; The output is : Time… Read More Leetcode bug? (Function isn't stopping even after it returns in binary search)

runtime error: addition of unsigned offset to 0x6030000000d0 overflowed to 0x6030000000cc (stl_vector.h)

Advertisements Problem Name: Peak Index in Mountain Array The problem requires finding the index of the peak element in a mountain array. The peak element is defined as an element that is greater than its neighbors. The array is guaranteed to have a mountain pattern. Such that: arr[0] < arr[1] < … < arr[i -… Read More runtime error: addition of unsigned offset to 0x6030000000d0 overflowed to 0x6030000000cc (stl_vector.h)

Why does my linear search isn't linearly taking more time with regards to the array size

Advertisements I was trying to benchmark binary search vs linear search. I’m using C++. Here is how I implemented my benchmark: #include <chrono> #include <iostream> #include <vector> #include <algorithm> #include <numeric> using namespace std; class Solution { public: int search(vector<int>& nums, int target) { int low = 0; int high = nums.size() – 1; while… Read More Why does my linear search isn't linearly taking more time with regards to the array size

binary search in a sorted 2d array using row / column pointers

Advertisements I’m trying to build a function that uses binary search to find a number in a sorted 2d array and I’m given this function prototype that I can’t change: int findNum(int array[N][M], int num,unsigned int* row, unsigned int* col); I tried this: int findNum(int array[N][M], int num,unsigned int* row, unsigned int* col){ int low… Read More binary search in a sorted 2d array using row / column pointers

Move all negative numbers to beginning and positive to end (java),order is not important

Advertisements public class posbeginnegend { private static void Swap(int a, int b) { int temp=a; a=b; b=temp; } public static void main(String[] args) { int []arr={45,23,-89,-21,67,-34,7,-44}; int start=0; int end=arr.length-1; while(start<=end) { int mid=start+(end-start)/2; if(arr[mid]>0) { if(arr[end]>0) { end–; } else { Swap(arr[mid],arr[end]); end–; } } else { if(arr[start]<0) { start++; } else { Swap(arr[mid],arr[start]);… Read More Move all negative numbers to beginning and positive to end (java),order is not important

Arrays.binarySearch not finding the element

Advertisements 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

Advertisements 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;… Read More Binary Search enters a infinite loop when I use a 'for' Loop but works fine 'while' while loop

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

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

Advertisements #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… Read More Binary search array showing -1 as result