-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day16.java
36 lines (33 loc) · 879 Bytes
/
Day16.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 Day16 {
public static void main(String[] args){
int[] arr ={1,15,25,45,42,21,17,12,11};
int n = arr.length;
int ans = new Solution().findMaximum(arr, n);
System.out.println(ans);
}
}
class Solution {
int findMaximum(int[] arr, int n) {
// code here
//if there is only one element
if(n==1){
return arr[0];
}
//if the first element is greater than second
else if(arr[0]>=arr[1]){
return arr[0];
}
//if the last element is greater than second last element
else if(arr[n-1]>=arr[n-2]){
return arr[n-1];
}
//else search PEEK element one by one
for(int i=1; i<n-1; i++){
if((arr[i]>=arr[i+1])&&(arr[i]>=arr[i-1])){
return arr[i];
}
}
//if no found , then first element is peek element
return arr[0];
}
}