Finding first and last occurrence of given number in sorted list is a very easy problem and can easily solved by leaner search with worse case Time complexity O(n) .
But We can Achieve faster search of first and last occurrence of a number by exploiting sorted feature of list.
We can search occurrence of any number in sorted list in O(log(n)) Time Complexity by using Binary search Algorithm.
We can Modify binary search to find first and last occurrence of number and its complexity will be O(lon(n)) .
But We can Achieve faster search of first and last occurrence of a number by exploiting sorted feature of list.
We can search occurrence of any number in sorted list in O(log(n)) Time Complexity by using Binary search Algorithm.
We can Modify binary search to find first and last occurrence of number and its complexity will be O(lon(n)) .
Finding First occurrence :
- apply binary search to search number if found save that index as ind
- then search again in left sub list i.e 0 to Ind-1
- repeat until no occurrence of n found in left sub-list
- return ind
C++ Source Code :
- //Find first occurence in sorted array in O(log(n))
- #include<iostream>
- #include<vector>
- using namespace std;
- int searchFirstInd(vector<int> &arr,int key,int low,int high){
- //using iterative modified binary search to find fist occurrence
- int mid,ind=-1;
- while(low<=high){
- mid=(low+high)/2;
- if(arr[mid]==key){
- ind=mid;
- high=mid-1; //finding occurrence of first key
- }
- else if(arr[mid]<key)
- low=mid+1;
- else
- high=mid-1;
- }
- return ind;
- //return -1 if not found
- }
- int main(){
- vector<int> arr;
- int n;
- cout<<"Enter No of element in sorted array :";
- cin>>n;
- cout<<"Enter n Element: "<<endl;
- int key;
- for(int i=0;i<n;i++){
- cin>>key;
- arr.push_back(key);
- }
- cout<<"Enter key to Search its first occurrence :";
- cin>>key;
- cout<<"Index of first occurrence of "<<key<<" is "<<searchFirstInd(arr,key,0,n-1);
- }
First Occurence |
Finding Last occurrence :
- apply binary search to search number if found save that index as ind
- then search again in left sub list i.e Ind-1 to end of list
- repeat until no occurrence of n found in left sub-list
- return ind
C++ Source Code:
- //Find Last occurrence in sorted array in O(log(n))
- #include<iostream>
- #include<vector>
- using namespace std;
- int searchFirstInd(vector<int> &arr,int key,int low,int high){
- //using iterative modified binary search to find Last occurrence
- int mid,ind=-1;
- while(low<=high){
- mid=(low+high)/2;
- if(arr[mid]==key){
- ind=mid;
- low=mid+1 //finding index of last occurrence of key
- }
- else if(arr[mid]<key)
- low=mid+1;
- else
- high=mid-1;
- }
- return ind;
- //return -1 if not found
- }
- int main(){
- vector<int> arr;
- int n;
- cout<<"Enter No of element in sorted array :";
- cin>>n;
- cout<<"Enter n Element: "<<endl;
- int key;
- for(int i=0;i<n;i++){
- cin>>key;
- arr.push_back(key);
- }
- cout<<"Enter key to Search its Last occurrence :";
- cin>>key;
- cout<<"Index of Last occurrence of "<<key<<" is "<<searchFirstInd(arr,key,0,n-1);
- }
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT