Advertisement

Find First and Last Position of Element in Sorted Array - LeetCode 34 Solution

Find First and Last Position of Element in Sorted Array - Complete Solution Guide

Find First and Last Position of Element in Sorted Array is LeetCode problem 34, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 algorithm with O(log n) runtime complexity. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Example 3: Input: nums = [], target = 0 Output: [-1,-1] Constraints: 0 <= nums.length <= 10 5 -10 9 <= nums[i] <= 10 9 num

Detailed Explanation

The problem requires us to find the first and last positions of a given target value in a sorted array of integers. If the target is not found, we should return [-1, -1]. The crucial constraint is that the algorithm must have O(log n) runtime complexity, implying the use of binary search. The array is sorted in non-decreasing order, which is also key to using binary search.

Solution Approach

The provided solution uses a modified binary search to find the leftmost occurrence of the target and the leftmost occurrence of the target + 1. The rightmost occurrence is then simply the leftmost occurrence of target + 1 minus 1. This approach leverages the sorted property of the array to efficiently locate the target's boundaries.

Step-by-Step Algorithm

  1. Step 1: Define a helper function 'find_left_boundary' that uses binary search to find the leftmost index where a value is greater than or equal to the target value.
  2. Step 2: Call 'find_left_boundary' with the original array and target value to find the potential leftmost index of the target.
  3. Step 3: Check if the found index is within the array bounds and if the value at that index is indeed equal to the target. If not, the target is not present in the array, so return [-1, -1].
  4. Step 4: Call 'find_left_boundary' again, this time with the original array and target + 1. This returns the index where target + 1 would be inserted to maintain sorted order. Subtracting 1 from this index gives the rightmost index of the target.
  5. Step 5: Return the leftmost index and rightmost index as a list/array containing these two values.

Key Insights

  • Insight 1: The sorted nature of the array allows for the efficient use of binary search to achieve O(log n) time complexity.
  • Insight 2: Finding the first and last positions requires two separate binary search operations, one to find the leftmost occurrence and the other to find the rightmost occurrence.
  • Insight 3: The 'find_left_boundary' function can be used to find both the leftmost and rightmost indices by slightly modifying the target value for the rightmost index search (target + 1).

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(1)

Topics

This problem involves: Array, Binary Search.

Companies

Asked at: Accenture, Adobe, Airtel, Amazon, Apple, Applied Intuition, Atlassian, Attentive, Bloomberg, Capgemini, DE Shaw, Instacart, LinkedIn, MakeMyTrip, Meta, Microsoft, NetApp, Oracle, PayPal, Pinterest, ServiceNow, Splunk, Tekion, TikTok, Tinkoff, Turing, Uber, Yahoo, Zillow, tcs.