File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .io .*;
2
+ public class PeakElement {
3
+ // TC : O(logn)
4
+ // SC : O(1)
5
+ public static void main (String args [])throws IOException
6
+ {
7
+ InputStreamReader read =new InputStreamReader (System .in );
8
+ BufferedReader in =new BufferedReader (read );
9
+ int n ,i ;
10
+ System .out .println ("Enter the size of the array" );
11
+ n =Integer .parseInt (in .readLine ());
12
+ int nums []=new int [n ];
13
+ System .out .println ("Enter the elements of the array" );
14
+ for (i =0 ;i <n ;i ++)
15
+ {
16
+ nums [i ]=Integer .parseInt (in .readLine ());
17
+ }
18
+ int low = 0 ;
19
+ int high = n -1 ;
20
+
21
+ while (low < high ){
22
+ int mid = low + (high -low )/2 ;
23
+ if (nums [mid ]<nums [mid +1 ]){
24
+ low = mid + 1 ;
25
+ } else {
26
+ high = mid ;
27
+ }
28
+ }
29
+ System .out .println ("The Peak Element is = " +nums [low ]);
30
+ }
31
+ }
You can’t perform that action at this time.
0 commit comments