Category Archives: Binary search

Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). C++: 01 double kSmall(int A[],int m,int B[],int n,int k) 02 { … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here … Continue reading

Posted in Binary search | Leave a comment

Search in Rotated Sorted Array II

Follow up for “Search in Rotated Sorted Array”: What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. C++: 01 class Solution { 02 public: 03    … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Search in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Median of Two Sorted Arrays

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). C++: 01 double kSmall(int A[],int m,int B[],int n,int k) 02  … Continue reading

Posted in Array and linked list, Binary search | Leave a comment

Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than … Continue reading

Posted in Array and linked list, Binary search | Leave a comment