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:
Quick Guide on Loading Initial Data with Spring Boot
Assertions in JUnit 4 and JUnit 5
Giới thiệu luồng vào ra (I/O) trong Java
Spring Security – security none, filters none, access permitAll
Form Validation with AngularJS and Spring MVC
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
A Guide to TreeMap in Java
Performance Difference Between save() and saveAll() in Spring Data
Running Spring Boot Applications With Minikube
Spring Boot - CORS Support
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java 8 Stream API Analogies in Kotlin
Java Program to Perform Sorting Using B-Tree
Java Program to Implement Knight’s Tour Problem
Java Program to Perform Postorder Non-Recursive Traversal of a Given Binary Tree
Phương thức forEach() trong java 8
Spring Data JPA Delete and Relationships
So sánh ArrayList và Vector trong Java
Java Program to Compute Cross Product of Two Vectors
Java Program to Perform Search in a BST
Basic Authentication with the RestTemplate
Base64 encoding và decoding trong Java 8
Abstract class và Interface trong Java
Java Program to Implement Merge Sort Algorithm on Linked List
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Implement ArrayList API
A Guide to Queries in Spring Data MongoDB
Spring 5 Testing with @EnabledIf Annotation
Java Convenience Factory Methods for Collections
Getting Started with Stream Processing with Spring Cloud Data Flow
Java Program to Find kth Largest Element in a Sequence
Java Program to Generate All Possible Combinations of a Given List of Numbers