class
Main {
public
static
int
lower_bound(
int
arr[],
int
low,
int
high,
int
X)
{
if
(low > high) {
return
low;
}
int
mid = low + (high - low) /
2
;
if
(arr[mid] >= X) {
return
lower_bound(arr, low, mid -
1
, X);
}
return
lower_bound(arr, mid +
1
, high, X);
}
public
static
int
upper_bound(
int
arr[],
int
low,
int
high,
int
X)
{
if
(low > high)
return
low;
int
mid = low + (high - low) /
2
;
if
(arr[mid] <= X) {
return
upper_bound(arr, mid +
1
, high, X);
}
return
upper_bound(arr, low, mid -
1
, X);
}
public
static
int
printBound(
int
arr[],
int
N,
int
X)
{
int
lower, upper;
if
(arr[
0
] == X) {
lower =
0
;
}
else
{
lower = lower_bound(arr,
0
, N, X);
}
if
(arr[N -
1
] == X) {
upper = N -
1
;
}
else
{
upper = upper_bound(arr,
0
, N, X);
}
return
upper - lower;
}
public
static
void
main(String[] args)
{
int
X =
3
;
int
arr[] = {
1
,
1
,
2
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
3
,
4
,
5
,
6
,
6
,
7
};
int
occurrence = printBound(arr, arr.length, X);
if
(occurrence >= arr.length /
2
) {
System.out.println(
X +
" occurs "
+ occurrence
+
" times which is more than "
+ arr.length /
2
+
" times"
);
}
else
{
System.out.println(X
+
" doesn't occur more than "
+ arr.length /
2
+
" times"
);
}
}
}