This is a Java Program to find number of occurences of a given number using binary search approach. The time complexity of the following program is O (log n).
Here is the source code of the Java program to find number of occurences of a given number using binary search approach. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/*
* Java Program to Find the Number of occurrences of a given Number using Binary Search approach
*/
import java.util.Scanner;
public class NumberOfOccurences
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter number of elements in sorted array");
int N = scan.nextInt();
int[] arr = new int[ N ];
/* Accept N elements */
System.out.println("Enter "+ N +" sorted elements");
for (int i = 0; i < N; i++)
arr[i] = scan.nextInt();
System.out.println("Enter number to find occurences");
int num = scan.nextInt();
int f = occur(arr, num);
if (f == -1)
System.out.println("No occurence");
else
System.out.println("Occurences = "+ f);
}
public static int occur(int[] arr, int num)
{
/* find first index */
int l1 = first(arr, num);
/* find last index */
int l2 = last(arr, num);
if (l1 == -1 || l2 == -1)
return -1;
return l2 - l1 + 1;
}
public static int first(int[] arr, int num)
{
if (arr[0] == num)
return 0;
int start = 0, end = arr.length - 1;
int mid = (start + end) / 2;
int flag = 0;
while (!(arr[mid] == num && arr[mid - 1] < arr[mid]))
{
if (start == end)
{
flag = 1;
break;
}
if (arr[mid] >= num)
end = mid - 1;
if (arr[mid] < num)
start = mid + 1;
mid = (start + end) / 2;
}
if (flag == 0)
return mid;
return -1;
}
public static int last(int[] arr, int num)
{
if (arr[arr.length - 1] == num)
return arr.length - 1;
int start = 0, end = arr.length - 1;
int mid = (start + end) / 2;
int flag = 0;
while (!(arr[mid] == num && arr[mid + 1] > arr[mid]))
{
if (start == end)
{
flag = 1;
break;
}
if (arr[mid] > num)
end = mid - 1;
if (arr[mid] <= num)
start = mid + 1;
mid = (start + end) / 2;
}
if (flag == 0)
return mid;
return -1;
}
}
Enter number of elements in sorted array 10 Enter 10 sorted elements 1 1 3 3 3 3 4 4 4 5 Enter number to find occurences 3 Occurences = 4 Enter number of elements in sorted array 10 Enter 10 sorted elements 1 1 3 3 3 3 4 4 4 5 Enter number to find occurences 5 Occurences = 1
Related posts:
Spring Data JPA @Modifying Annotation
Guide to PriorityBlockingQueue in Java
Java Program to Solve any Linear Equations
Binary Numbers in Java
Java Program to Implement Direct Addressing Tables
Guide to System.gc()
Custom Cascading in Spring Data MongoDB
Biểu thức Lambda trong Java 8 – Lambda Expressions
Spring Boot Integration Testing with Embedded MongoDB
Guide to the Synchronized Keyword in Java
Java Program to Implement Radix Sort
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Java Program to Perform Cryptography Using Transposition Technique
Server-Sent Events in Spring
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Implement Quick sort
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Get and Post Lists of Objects with RestTemplate
Comparing Two HashMaps in Java
Guide to the Fork/Join Framework in Java
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Webflux and CORS
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Implement Sparse Array
Guava – Join and Split Collections
Java Program to Implement Miller Rabin Primality Test Algorithm
Remove HTML tags from a file to extract only the TEXT
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Hướng dẫn Java Design Pattern – MVC
Encode/Decode to/from Base64
Receive email using POP3