Skip to content

Commit 97441f3

Browse files
authored
#479 Peak Element in an Array using Java
#479 Peak Element in an Array using Java
1 parent 44c2f30 commit 97441f3

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

Arrays/PeakElement.java

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

0 commit comments

Comments
 (0)