Merge Two Sorted Arrays

Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Similar Problem You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. ...

Remove duplicates from Sorted Array 1

Problem Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Note that even though we want you to return the new length, make sure to change the original array as well in place. ...

Guess Number Higher or Lower

Problem We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess. You call a pre-defined API int guess(int num), which returns three possible results: -1: Your guess is higher than the number I picked (i.e. num > pick). 1: Your guess is lower than the number I picked (i.e. num < pick). 0: your guess is equal to the number I picked (i.e. num == pick). Return the number that I picked. ...

Search Insert Position in sorted array

Problem 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. You must write an algorithm with O(log n) runtime complexity. Examples Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 ...

Intersection of Two Sorted Array

Problem Given two sorted arrays, find the intersection (elements present in both arrays). Examples Example 1: Input: nums1 = [1,1,2,2], nums2 = [2,2] Output: [2,2] Example 2: Input: nums1 = [4,5,9], nums2 = [4,4,8,9,9] Output: [4,9] Explanation: [9,4] is also accepted. ...

Find last position of target element in sorted array

Problem Given a sorted array and an element, find last occurrence of element in array. Examples Example 1: Input: a = [1, 4, 4, 10, 10, 15, 20], target = 10 Output: 4 This problem is similar to Find first position of target element in sorted array. Solution A brute force solution involves scanning the entire array and comparing each A[index] with the key. If A[index] is equal to the key and the next element is different, return the index. The worst-case time complexity of this method is ( O(N) ). Can we improve this? And the answer is yes, using Binary Search on Sorted Array. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy