-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGFG.java
More file actions
34 lines (30 loc) · 798 Bytes
/
GFG.java
File metadata and controls
34 lines (30 loc) · 798 Bytes
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
// A Java program to find a peak element
import java.util.*;
class GFG {
// Find the peak element in the array
static int findPeak(int arr[], int n)
{
// First or last element is peak element
if (n == 1)
return 0;
if (arr[0] >= arr[1])
return 0;
if (arr[n - 1] >= arr[n - 2])
return n - 1;
// Check for every other element
for (int i = 1; i < n - 1; i++) {
// Check if the neighbors are smaller
if (arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])
return i;
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 3, 20, 4, 1, 0 };
int n = arr.length;
System.out.print("Index of a peak point is " + findPeak(arr, n));
}
}
// This code is contributed by Aditya Kumar (adityakumar699)