Java Program to find the peak element of an array using Binary Search approach

This is a Java Program to find peak element of an array. A peak element of an array is that element which is greater than its neighbors. The following program uses binary search approach to find a peak element. The time complexity of the following program is O (log n).

Here is the source code of the Java program to find peak element of an array using binary search. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

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
/*
 * Java Program to Find the peak element of an array using
 * Binary Search approach
 */
import java.util.Scanner;
  
public class PeakElement2   
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter size of array");
        int N = scan.nextInt();
        int[] arr = new int[ N ];
        /* Accept N elements */
        System.out.println("Enter "+ N +" elements");
        for (int i = 0; i < N; i++)
            arr[i] = scan.nextInt();
        /* Find Peak Elements */       
        System.out.print("\nPeak Elements : ");
        peak(arr);
        System.out.println();
    }
    public static void peak(int[] arr)
    {
        peak(arr, 0, arr.length - 1);
    }   
    public static void peak (int arr[], int low, int high)
    {
        int N = arr.length;
        if (high - low < 2)
            return;
        int mid = (low + high) / 2;
        if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1])
            System.out.print(arr[mid] +" ");
        /* Recursively find other peak elements */       
        peak (arr, low, mid);
        peak (arr, mid, high);   
    }       
}
1
2
3
4
5
6
Enter size of array
8
Enter 8 elements
87 63 51 25 40 24 6 1
  
Peak Elements : 40

Related posts:

Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Implement ConcurrentLinkedQueue API
Spring Security Registration – Resend Verification Email
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Bootstrap a Web Application with Spring 5
So sánh ArrayList và Vector trong Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Find the Vertex Connectivity of a Graph
Spring @Primary Annotation
Overview of Spring Boot Dev Tools
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Solve any Linear Equation in One Variable
Java Program to Find Basis and Dimension of a Matrix
Deque và ArrayDeque trong Java
Logging in Spring Boot
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Spring Data JPA Delete and Relationships
Comparing Long Values in Java
Java Program to Represent Graph Using Incidence List
Send email with JavaMail
Java Program to Describe the Representation of Graph using Adjacency List
A Guide to LinkedHashMap in Java
Java IO vs NIO
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
Handling URL Encoded Form Data in Spring REST
Java Program to Implement the Bin Packing Algorithm
Simplify the DAO with Spring and Java Generics
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
REST Web service: Upload và Download file với Jersey 2.x
Spring Boot - Database Handling
SOAP Web service: Authentication trong JAX-WS