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]
Solution : 1
int firstPosition(vector<int>& nums, int target, int low, int high)
{
if(high >= low)
{
int mid = low + (high-low) / 2;
if((mid == 0 || target > nums[mid-1]) && nums[mid] == target)
return mid;
else if(target > nums[mid])
return firstPosition(nums, target, mid+1, high);
else
return firstPosition(nums, target, low, mid-1);
}
return -1;
}
int lastPosition(vector<int>& nums, int target, int low, int high, int size)
{
if(high >= low)
{
int mid = low + (high-low) / 2;
if((mid == size - 1 || target < nums[mid+1]) && nums[mid] == target)
{return mid;}
else if(target < nums[mid])
return lastPosition(nums, target, low, (mid-1), size);
else
return lastPosition(nums, target, (mid+1), high, size);
}
return -1;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
int first = firstPosition(nums, target, 0, nums.size()-1);
int last = lastPosition(nums, target, 0, nums.size()-1, nums.size());
result.push_back(first);
result.push_back(last);
return result;
}
Solution : 2
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
int first=-1, last=-1;
if(nums.size() > 0)
{
for(int i=0; i<nums.size(); i++)
{
if(nums[i] == target)
{
if(first == -1)
first = i;
last = i;
}
}
}
result.push_back(first);
result.push_back(last);
return result;
}
Solution : 3
Iterator lower_bound (Iterator first, Iterator last, const val)
Iterator upper_bound (Iterator first, Iterator last, const val)
lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’.
upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’.
vector<int> searchRange3(vector<int>& nums, int target) {
vector<int> result;
int first=-1, last=-1;
first = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
last = upper_bound(nums.begin(), nums.end(), target) - nums.begin() -1;
result.push_back(first);
result.push_back(last);
return result;
}
Complete Code :
/*
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]
*/
#include <bits/stdc++.h>
using namespace std;
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
int first=-1, last=-1;
if(nums.size() > 0)
{
for(int i=0; i<nums.size(); i++)
{
if(nums[i] == target)
{
if(first == -1)
first = i;
last = i;
}
}
}
result.push_back(first);
result.push_back(last);
return result;
}
vector<int> searchRange3(vector<int>& nums, int target) {
vector<int> result;
int first=-1, last=-1;
first = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
last = upper_bound(nums.begin(), nums.end(), target) - nums.begin() -1;
result.push_back(first);
result.push_back(last);
return result;
}
int firstPosition(vector<int>& nums, int target, int low, int high)
{
//cout << "low: " << low << "high: " << high <<"mid: " << endl;
if(high >= low)
{
int mid = low + (high-low) / 2;
//cout << "low: " << low << " high: " << high <<" mid: " << mid << endl;
if((mid == 0 || target > nums[mid-1]) && nums[mid] == target)
return mid;
else if(target > nums[mid])
return firstPosition(nums, target, mid+1, high);
else
return firstPosition(nums, target, low, mid-1);
}
return -1;
}
int lastPosition(vector<int>& nums, int target, int low, int high, int size)
{
//cout << "low: " << low << " high: " << high << endl;
if(high >= low)
{
int mid = low + (high-low) / 2;
//cout << "low: " << low << " high: " << high <<" mid: " << mid << endl;
if((mid == size - 1 || target < nums[mid+1]) && nums[mid] == target)
{return mid;}
else if(target < nums[mid])
return lastPosition(nums, target, low, (mid-1), size);
else
return lastPosition(nums, target, (mid+1), high, size);
//cout << "mid: " << mid << endl;
}
return -1;
}
vector<int> searchRangeUsingBinarySearch(vector<int>& nums, int target) {
vector<int> result;
int first = firstPosition(nums, target, 0, nums.size()-1);
int last = lastPosition(nums, target, 0, nums.size()-1, nums.size());
result.push_back(first);
result.push_back(last);
return result;
}
int main()
{
vector<int> v1 = {5,7,7,8,8,10};
int target = 8;
//vector<int> ret = searchRangeUsingBinarySearch(v1, target);
//vector<int> ret = searchRange(v1, target);
vector<int> ret = searchRange3(v1, target);
for(int i=0; i<ret.size(); i++)
cout << ret[i] << " ";
cout << endl;
return 0;
}