Daffodil International University 2024
My Stats
EXP gained
3623
Level
5 (Champion)
Community stats
Discussions
0
0
Know more
174
Total problems solved
164
Easy
10
Moderate
0
Hard
0
Ninja

Current streak:

0 days

Longest streak:

4 days

Less

More

Achievements
1
Sensei
Guided path
Fundamentals of Html
Discussions
99.97% better performance in O(log(n)) time complexity in C++
Interview problems
``````int firstOcc(vector<int>&arr,int n, int x){
int low=0, high=n-1;
int mid;
while (low <= high) {
mid=(low+high)/2;
if((mid==0 && arr[mid]==x) || (arr[mid]==x && arr[mid-1]<x)){
return mid;
}
else if(x>arr[mid]){
low=mid+1;
}
else{
high=mid-1;
}
}
return -1;
}
int lastOcc(vector<int>&arr, int n, int x){
int low=0, high=n-1;
int mid;
while(low<=high){
mid=(low+high)/2;
if((mid==n-1 && arr[mid]==x) || (arr[mid]==x && arr[mid+1]>x)){
return mid;
}
else if(x<arr[mid]){
high=mid-1;
}
else{
low=mid+1;
}
}
return -1;
}
pair<int, int> findFirstLastPosition(vector<int> &arr, int n, int x)
{
pair<int,int>pr;
pr.first=firstOcc(arr,n,x);
pr.second=lastOcc(arr,n,x);
return pr;
}``````
tazbid
Published On 03-Apr-2023
220 views
0 replies