Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int binarySearch(int arr[], int l, int r, int x){
- if (r < l)
- return -1;
- int mid = l + (r - l) / 2;
- if (arr[mid] == x)
- return mid;
- if (arr[mid] > x)
- return binarySearch(arr, l, mid - 1, x);
- return binarySearch(arr, mid + 1, r, x);
- }
- int count(int arr[], int n, int x){
- int ind = binarySearch(arr, 0, n - 1, x);
- if (ind == -1)
- return 0;
- int count = 1;
- int left = ind - 1;
- while (left >= 0 && arr[left] == x)
- count++, left--;
- int right = ind + 1;
- while (right < n && arr[right] == x)
- count++, right++;
- return count;
- }
- int main(){
- int size, queries, temp;
- scanf("%d %d", &size, &queries);
- int data[size];
- for(int i=0;i<size;i++){
- scanf("%d", &data[i]);
- }
- while(queries--){
- scanf("%d", &temp);
- printf("%d\n", count(data, size, temp));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement