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.

/*
 * 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);    
    }        
}
Enter size of array
8
Enter 8 elements
87 63 51 25 40 24 6 1
 
Peak Elements : 40

Related posts:

Java Program to Implement Solovay Strassen Primality Test Algorithm
Apache Camel with Spring Boot
Spring Boot - Hystrix
Hướng dẫn sử dụng Lớp FilePermission trong java
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Dijkstra’s Algorithm using Queue
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Spring Security Logout
Java Program to Check if a Matrix is Invertible
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Java Program to Evaluate an Expression using Stacks
Serialize Only Fields that meet a Custom Criteria with Jackson
Merging Streams in Java
Guide to Spring 5 WebFlux
Extract links from an HTML page
Java Program for Douglas-Peucker Algorithm Implementation
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Annotation trong Java 8
Hướng dẫn Java Design Pattern – Proxy
Java Program to Implement the RSA Algorithm
Java String Conversions
Login For a Spring Web App – Error Handling and Localization
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Handle EML file with JavaMail
A Guide to the finalize Method in Java
Inheritance and Composition (Is-a vs Has-a relationship) in Java
HttpClient 4 Cookbook
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
A Quick Guide to Using Keycloak with Spring Boot
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Implement Stack API
Use Liquibase to Safely Evolve Your Database Schema