This is a Java Program to find peak element of an array. A peak element of an array is that element which is not smaller than its neighbors. Consider only one neighbour for corner elements. The time complexity of the following program is O (n).
Brute Force Algorithm is as follows :
for i in range (n) :
if A[i - 1] <= A[i] >= A[i + 1] :
print A[i]
end if
end for
Here is the source code of the Java program to find peak element of an array. 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 O(n) time (Naive Method)
*/
import java.util.Scanner;
public class PeakElement1
{
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 + 2];
/* set corner values to -infinity */
arr[0] = Integer.MIN_VALUE;
arr[N + 1] = Integer.MIN_VALUE;
/* Accept N elements */
System.out.println("Enter "+ N +" elements");
for (int i = 1; i <= N; i++)
arr[i] = scan.nextInt();
/* Find All Peak Elements */
System.out.println("\nAll Peak Elements : ");
for (int i = 1; i <= N; i++)
if (arr[i - 1] <= arr[i] && arr[i] >= arr[i + 1])
System.out.println(arr[i] +" at position "+ i);
System.out.println();
}
}
Enter size of array 6 Enter 6 elements 1 2 5 5 4 1 All Peak Elements : 5 at position 3 5 at position 4 Enter size of array 7 Enter 7 elements 6 24 15 2 23 99 67 All Peak Elements : 24 at position 2 99 at position 6 Enter size of array 10 Enter 10 elements 10 9 8 24 8 7 97 28 17 63 All Peak Elements : 10 at position 1 24 at position 4 97 at position 7 63 at position 10
Related posts:
Java Program to Perform Polygon Containment Test
Custom Thread Pools In Java 8 Parallel Streams
Command-Line Arguments in Java
Sử dụng CyclicBarrier trong Java
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Object Type Casting in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Comparing Strings in Java
Java Program to Implement AA Tree
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Hướng dẫn sử dụng String Format trong Java
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java 8 and Infinite Streams
Java Program to Implement CountMinSketch
Java Program to Implement Fibonacci Heap
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Spring Security Logout
Java Program to Implement Segment Tree
wait() and notify() Methods in Java
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Hướng dẫn Java Design Pattern – Mediator
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
Java Program to Implement Sieve Of Sundaram
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Implement Bellman-Ford Algorithm
Java – File to Reader
Using the Not Operator in If Conditions in Java
Hướng dẫn Java Design Pattern – Service Locator