Dec 19,2024 (LBQ-Search Insert Position, Missing Numbers)
Dec 19,2024 (LBQ-Search Insert Position, Missing Numbers)
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Constraints:
1 <= nums.length <= 10^4
-10^4 <= nums[i] <= 10^4
nums contains distinct values sorted in ascending order.
-10^4 <= target <= 10^4
Solution The problem can be solved using a Binary Search Algorithm to achieve
(Example 1) the required O (log n) time complexity. Here's how the algorithm works:
1. Use two pointers, left and right, to represent the range of indices in
the array to search.
2. Calculate the middle index mid of the current range.
3. Compare nums[mid] with the target:
o If nums[mid] equals the target, return mid.
o If nums[mid] is less than the target, move the left pointer to
mid + 1.
o If nums[mid] is greater than the target, move the right
pointer to mid - 1.
4. Repeat steps 2–3 until left exceeds right.
5. If the loop ends without finding the target, left will indicate the
index where the target should be inserted.
// Example 1
(Example 2) int[] nums1 = {1, 3, 5, 6};
int target1 = 2;
System.out.println(solution.searchInsert(nums1,
target1)); // Output: 1
// Example 2
int[] nums2 = {1, 3, 5, 6};
int target2 = 7;
System.out.println(solution.searchInsert(nums2,
target2)); // Output: 4
// Example 3
int[] nums3 = {1, 3, 5, 6};
int target3 = 0;
System.out.println(solution.searchInsert(nums3,
target3)); // Output: 0
// Example 4
int[] nums4 = {1, 3, 5, 6};
int target4 = 5;
System.out.println(solution.searchInsert(nums4,
target4)); // Output: 2
}
}
Explanation of Example 2:
Output: 1.
Complexity:
(Example 3) Input:
nums = [1, 3, 5, 6]
target = 7
Steps:
// Example 3
int[] nums = {1, 3, 5, 6};
int target = 7;
System.out.println(solution.searchInsert(nums, target)); // Output: 4
}
}
1. Initial pointers: left = 0, right = 3.
2. Compute mid = (0 + 3) / 2 = 1. nums[mid] = 3.
o Since nums[mid] < target, move left = mid + 1 = 2.
3. Compute mid = (2 + 3) / 2 = 2. nums[mid] = 5.
Galgotias College of Engineering & Technology, Greater Noida
Output: 4.
2 Missing Number
Given an array nums containing n distinct numbers in the range [0, n],
return the only number in the range that is missing from the array.
Example 1:
Input: nums = [3,0,1] L
Output: 2 2
Explanation: n = 3 since there are 3 numbers, so all numbers are in the
range [0,3]. 2 is the missing number in the range since it does not appear in
nums.
Example 2:
Input: nums = [0,1]
Output: 2
Explanation: n = 2 since there are 2 numbers, so all numbers are in the
range [0,2]. 2 is the missing number in the range since it does not appear in
nums.
Example 3:
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
Explanation: n = 9 since there are 9 numbers, so all numbers are in the
range [0,9]. 8 is the missing number in the range since it does not appear in
nums.
Constraints:
n == nums.length
1 <= n <= 104
0 <= nums[i] <= n
All the numbers of nums are unique.
Solution Approach
(Example 1)
We can solve this problem using the Sum Formula for the first nnn natural
numbers:
Galgotias College of Engineering & Technology, Greater Noida
Complexity
Example Usage
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.missingNumber(nums1)); //
Output: 2
System.out.println(solution.missingNumber(nums2)); //
Galgotias College of Engineering & Technology, Greater Noida
Output: 2
System.out.println(solution.missingNumber(nums3)); //
Output: 8
System.out.println(solution.missingNumber(nums4)); //
Output: 1
}
}
1. Expected Sum:
o Compute the sum of numbers from 000 to nnn using the
formula n×(n+1)/2
2. Actual Sum:
o Traverse the array and compute the sum of its elements.
3. Missing Number:
o The missing number is simply expectedSum − actualSum
Calculate the sum of numbers in the range [0,n][0, n][0,n] using the
formula.
Calculate the actual sum of the array elements.
The difference between the two sums gives the missing number.
}
public class Main {
System.out.println(solution.missingNumber(nums1)); //
Output: 2
System.out.println(solution.missingNumber(nums2)); //
Output: 2
System.out.println(solution.missingNumber(nums3)); //
Output: 8
}
}
Input: nums = [0, 1], n=2
1. Sum Formula:
o Expected sum: (2x(2+1))/2 = 3
o Actual sum: 0+1=1
o Missing number: 3−1=2
2. XOR Method:
o XOR from 0 to 2: 0⊕1⊕2=3
o XOR with array elements: 3⊕0⊕1=2
o Missing number: 2.
Complexity: