-
Notifications
You must be signed in to change notification settings - Fork 1
/
3June(IV).java
36 lines (32 loc) · 946 Bytes
/
3June(IV).java
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
public class PeakElement {
static void findPeakElement(int[] nums){
int n = nums.length;
int peak = 0;
if(n==1){
return 0;
}
for(int i = 0 ; i<n;i++){
if(i==0){
if(nums[i+1]>nums[i]){ /// checking for the 1st element basically it's not a checking
// here we don't have 1st element's neighbhour
peak = i+1;
}
}
else if(i==n-1){
if(nums[i-1]<nums[i]){
peak = i;
}
}
else{
if(nums[i-1]<nums[i] && nums[i+1]<nums[i]){ // main logic
peak = i ;
}
}
}
return peak;
}
public static void main(String[] args) {
int[] arr = {11,2,1,3,5,6,4};
findPeakElement(arr);
}
}