Noob Approach but works :)

programming
Tags: #<Tag:0x00007f2426de7500>

#1
**int firstIndex(vector<int> A,int B){**

** int start = 0;**
** int end = A.size() - 1;**
** while(start<=end){**
** int mid = start + (end - start)/2;**
** if(A[mid] == B && (mid == 0 || A[mid-1]!=B)){**
** return mid; **
** }**
** else if(A[mid] >= B){**
** end = mid - 1;**
** }**
** else{**
** start = mid + 1;**
** }**
** }**
** return -1;**
** }**
** int lastIndex(vector A,int B){**
** int start = 0;**
** int end = A.size() - 1;**
** while(start<=end){**
** int mid = start + (end - start)/2;**
** if(A[mid] == B && (mid == A.size()-1 || A[mid+1]!=B)){**
** return mid; **
** }**
** else if(A[mid] > B){**
** end = mid - 1;**
** }**
** else{**
** start = mid + 1;**
** }**
** }**
** return -1;**
** }**
** int Solution::findCount(const vector &A, int B) {**
** int first = firstIndex(A,B);**
** int last = lastIndex(A,B);**
** if(first == -1 && last == -1){**
** return 0;**
** }**
** int count = last - first + 1;**
** return count;**
** }**