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

Sun Apr 02 2023 04:04:56 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

```class Solution {
public:

int loweridx(vector<int> nums ,int target)
{
int st=0, end=nums.size()-1;
int ans=-1;
int mid=st+(end-st)/2;
while(st<=end)
{
if(nums[mid]==target)
{
ans=mid;
end=mid-1;
}
else if(target>nums[mid])
{
st=mid+1;
}
else if(target<nums[mid])
{
end=mid-1;
}
mid=st+(end-st)/2;
}
return ans;
}

int largidx(vector<int> nums ,int target)
{
int st=0, end=nums.size()-1;
int ans=-1;
int mid=st+(end-st)/2;
while(st<=end)
{
if(nums[mid]==target)
{
ans=mid;
st=mid+1;
}
else if(target>nums[mid])
{
st=mid+1;
}
else if(target<nums[mid])
{
end=mid-1;
}
mid=st+(end-st)/2;
}
return ans;
}

vector<int> searchRange(vector<int>& nums, int target) {

vector<int> v;
int lowidx=loweridx(nums, target);
int laridx=largidx(nums, target);
v.push_back(lowidx);
v.push_back(laridx);
return v;
}
};```
content_copyCOPY

https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/