-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoccurrence.java
More file actions
56 lines (51 loc) · 1.29 KB
/
Copy pathoccurrence.java
File metadata and controls
56 lines (51 loc) · 1.29 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
public class occurrence {
public static void main(String[] args) {
int[] arr = { 1, 1, 2, 2, 2, 2, 2, 3, 4 };
int x = 1;
System.out.println(countOcc(arr, x));
}
// Through linear Search
static int Occ(int[] arr, int x) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
count++;
}
}
return count;
}
// Through Binary Search
static int binarySearch(int[] arr, int x) {
int l = 0;
int r = arr.length - 1;
while (l < r) {
int mid = l + (r - l) / 2;
if (arr[mid] > x) {
r = mid - 1;
} else if (arr[mid] == x) {
return mid;
} else {
l = mid + 1;
}
}
return -1;
}
static int countOcc(int[] arr, int x) {
int ind = binarySearch(arr, x);
int left = ind - 1;
int right = ind + 1;
int count = 1;
if (ind == -1) {
return 0;
}
while (left >= 0 && arr[left] == x) {
count++;
left--;
}
while (right < arr.length && arr[right] == x) {
count++;
right++;
}
return count;
}
}