MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / firstocc

Function firstocc

CPP/searching/binary_search1.cpp:8–25  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6#define mod 1000000007
7#define endl '\n'
8int firstocc(vector<int>& arr,int n,int key){
9 int s=0;
10 int e=n-1;
11 int ans=-1;
12 for (int i = 0; i < n; i++) {
13 int mid=(s+e)/2;
14 if(arr[mid]==key){
15 ans=mid;
16 e=mid-1;
17 }
18 else if(key>arr[mid])
19 s=mid+1;
20 else
21 e=mid-1;
22 }
23 return ans;
24
25}
26int lastocc(vector<int>& arr,int n,int key){
27 int s=0;
28 int e=n-1;

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected