Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Find First and Last Position of Element in Sorted Array problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Find First and the Last Position of Element in Sorted Array problem solution we have Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

If the target is not found in the array, return [-1, -1]. You must write an algorithm with O(log n) runtime complexity.

Leetcode Find First and the Last Position of Element in Sorted Array problem solution

Leetcode Find First and Last Position of Element in Sorted Array problem solution in Python.

class Solution:
    def searchRange(self, nums, target):
        left = bisect.bisect_left(nums, target)
        if left == len(nums) or nums[left] != target:
            return [-1, -1]
        right = bisect.bisect_right(nums, target)
        return [left, right - 1]

Find First and Last Position of Element in Sorted Array problem solution in Java.

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int start = 0;
        int end = nums.length-1;
        int first =-1;
        while(start<=end){
            int mid = (start+end)/2;
            if(nums[mid]==target)first = mid;
            if(nums[mid]>=target)end = mid-1;
            else start =mid+1;
        }
        int last  = -1;
        start=0;end =nums.length-1;
        while(start<=end){
              int mid = (start+end)/2;
            if(nums[mid]==target)last = mid;
            if(nums[mid]>target)end = mid-1;
            else start =mid+1;
        }
        return new int[]{first,last};
    }
}

Problem solution in C++.

int bs(vector<int> &nums, int target, int left, int right) { // binary search
    while (left < right) {
        int mid = (right + left) / 2;
        if (nums[mid] == target && (mid == 0 || nums[mid - 1] != target))
            return mid;
        else if (nums[mid] >= target)
            right = mid;
        else
            left = mid + 1;
    }
    return left;
}
vector<int> searchRange(vector<int>& nums, int target) {
    int l = bs(nums, target, 0, nums.size());
    if (l == nums.size() || nums[l] != target)
        return {-1, -1};
    int r = bs(nums, target + 1, l, nums.size()) - 1;
    return {l, r};
}

Problem solution in C.

int* searchRange(int* nums, int numsSize, int target, int* returnSize){
    *returnSize=2;
    int * result=(int*)malloc(sizeof(int)*2);
    result[0]=-1;
    result[1]=-1;
    int start=-1;
    int end=-1;
    if(numsSize==0)
    {
        return result;
    }
    for(int i=0;i<numsSize;i++)
    {
        if(nums[i]==target)
        {
            start=i;
            break;
        }
    }
    for(int i=numsSize-1;i>=0;i--)
    {
        if(nums[i]==target)
        {
            end=i;
            break;
        }
    }
    result[0]=start;
    result[1]=end;
    return result;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Comment

  1. YASH PAL says:
    20 January 2026 at 1:13 PM

    good problem and solution

Leave a Reply

Your email address will not be published. Required fields are marked *

CLOSE ADS
CLOSE ADS

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes