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

Master LeetCode problem 34 (Find First and Last Position of Element in Sorted Array), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

34. Find First and Last Position of Element in Sorted Array

Problem Explanation

Explanation

To solve this problem with O(log n) runtime complexity, we can use a modified binary search approach. We will search for the leftmost and rightmost occurrences of the target element separately by modifying the binary search logic. By finding the leftmost occurrence, we can determine the starting position, and by finding the rightmost occurrence, we can determine the ending position.

  1. Find Leftmost Occurrence: Use binary search to find the leftmost occurrence of the target element.
  2. Find Rightmost Occurrence: Use binary search to find the rightmost occurrence of the target element.

Time Complexity

The time complexity of this approach is O(log n) as we are using binary search to find both the leftmost and rightmost occurrences.

Space Complexity

The space complexity is O(1) as we are not using any extra space apart from a few variables.

Solution Code

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int left = findOccurrence(nums, target, true);
        if (left == -1) {
            return new int[]{-1, -1};
        }
        int right = findOccurrence(nums, target, false);
        return new int[]{left, right};
    }
    
    private int findOccurrence(int[] nums, int target, boolean findLeft) {
        int left = 0, right = nums.length - 1;
        int result = -1;
        
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                result = mid;
                if (findLeft) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 34 (Find First and Last Position of Element in Sorted Array)?

This page provides optimized solutions for LeetCode problem 34 (Find First and Last Position of Element in Sorted Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 34 (Find First and Last Position of Element in Sorted Array)?

The time complexity for LeetCode 34 (Find First and Last Position of Element in Sorted Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 34 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 34 in Java, C++, or Python.

Back to LeetCode Solutions