Skip to content

Commit e1b1863

Browse files
authored
Merge pull request #535 from ushmita4/Ushmita
#479 Peak Element in Java
2 parents d470e21 + 3823f7e commit e1b1863

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Arrays/PeakElement.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)