-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_element_occurance_in_logn.cpp
More file actions
96 lines (81 loc) · 1.4 KB
/
count_element_occurance_in_logn.cpp
File metadata and controls
96 lines (81 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <bits/stdc++.h>
using namespace std;
int binary_search(int arr[], int left, int right, int k){
int mid;
while(left<=right){
mid = (left+right)/2;
if(arr[mid] == k && arr[mid-1]!=k){
return mid;
}
else if(k > arr[mid]){
left = mid+1;
}
else{
right = mid-1;
}
}
}
void count_occurance(int arr[], int n, int k){
int idx = binary_search(arr, 0, n-1, k);
int count=0;
for(int i=idx; i<n; i++){
if(arr[i]==k){
count++;
}
else{
break;
}
}
cout<<count<<endl;
}
int main()
{
int test;
cin>>test;
while(test--){
int k, n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
cin>>k;
count_occurance(arr, n, k);
}
return 0;
}
/*
int binary_search(vector<int> arr, int left, int right, int k){
int mid;
while(left<=right){
mid = (left+right)/2;
if(arr[mid] == k && arr[mid-1]!=k){
return mid;
}
else if(k > arr[mid]){
left = mid+1;
}
else{
right = mid-1;
}
}
}
int Solution::findCount(const vector<int> &A, int B) {
// }
// void count_occurance(int arr[], int n, int k){
int n = A.size();
int k = B;
int idx = binary_search(A, 0, n-1, k);
int count=0;
for(int i=idx; i<n; i++){
if(A[i]==k){
count++;
}
else{
break;
}
}
// cout<<count<<endl;
return count;
}
*/