-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathOccurrenceCountOfElement.java
56 lines (55 loc) · 1.94 KB
/
OccurrenceCountOfElement.java
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
package SummerTrainingGFG.Searching;
/**
* @author Vishal Singh */
public class OccurrenceCountOfElement {
static int leftMostIndex(int[] arr,int low,int high,int elementToSearch){
if (low>high){
return -1;
}
int mid = (low+high)/2;
if (arr[mid] == elementToSearch &&
(mid == 0 || arr[mid-1] != elementToSearch)) {
return mid;
}
if (arr[mid]>=elementToSearch){
return leftMostIndex(arr,low,mid-1,elementToSearch);
}
else {
return leftMostIndex(arr,mid+1,high,elementToSearch);
}
}
static int rightMostIndex(int[] arr,int low,int high,int n,int elementToSearch){
if (low>high){
return -1;
}
int mid = (low+high)/2;
if (arr[mid] == elementToSearch &&
(mid == (n-1) || arr[mid+1] != elementToSearch)) {
return mid;
}
if (arr[mid]>elementToSearch){
return rightMostIndex(arr,low,mid-1,n,elementToSearch);
}
else {
return rightMostIndex(arr,mid+1,high,n,elementToSearch);
}
}
static int noOfOccurrences(int[] arr,int low,int high,int elementToSearch){
int l = leftMostIndex(arr,low,high,elementToSearch);
int r = rightMostIndex(arr,low,high,arr.length,elementToSearch);
if (l == -1 || r == -1){
return 0;
}
return (r-l)+1;
}
public static void main(String[] args) {
int[] arr = {0,1,2,2,2,4,5,5,5,5,6,6,6,99};
/*
*Find the number of occurrence of the searched element*/
System.out.println(noOfOccurrences(arr,0,arr.length-1,0));
System.out.println(noOfOccurrences(arr,0,arr.length-1,2));
System.out.println(noOfOccurrences(arr,0,arr.length-1,6));
System.out.println(noOfOccurrences(arr,0,arr.length-1,99));
System.out.println(noOfOccurrences(arr,0,arr.length-1,100));
}
}