Repository navigation
Expand file tree
/
Copy pathMountainArray.java
More file actions
83 lines (70 loc) · 2.72 KB
/
Copy pathMountainArray.java
File metadata and controls
83 lines (70 loc) · 2.72 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package BinaySearch;
public class MountainArray {
public static void main(String[] args) {
//[1,2,3,1]
//[1,2,1,3,5,6,4]
int[] array = {1,2,3,6,5,4,3,2,1};
// int[] array = {1,2,1,3,5,6,4};
// int[] array = {1,2,3,1};//[2,1]
// [6,5,4,3,2,3,2]
// int[] array = {6,5,4,3,2,3,2};
System.out.println("Peak Index Value is " + findPeakIndexFromArray(array));
System.out.println("Peak Number is " + array[findPeakIndexFromArray(array)]);
}
static int findPeakIndexFromArray(int[] nums){
int peakIndex = -1;
int startIndex = 0;
int endIndex = nums.length -1;
while (startIndex <= endIndex){
//Find Middle Element
int middle = startIndex + (endIndex - startIndex)/2;
System.out.println("Middle Value is " + middle);
if (nums.length == 1){
return nums[0];
}else if (nums.length == 2){
if (nums[0] < nums[1]){
return 1;
}else {
return 0;
}
}
if (nums[middle] > nums[middle-1] && nums[middle] > nums[middle +1]){
System.out.println("Returning Middle Index in if condition is " + middle);
return middle;
}
else if (nums[middle] > nums[middle+1]){
endIndex = middle;
System.out.println("End Index Changed to " + middle);
// System.out.println("Start Index Incremented");
}else if(nums[middle] < nums[middle+1]){
startIndex = middle;
System.out.println("Start Index Changed to " + middle);
// System.out.println("End Index Decremented");
}else {//if (nums[middle] > nums[middle+1] && nums[middle] < nums[middle-1])
System.out.println("Returning Middle Index in else condition is " + middle);
return middle;
}
}
return peakIndex;
}
/*
Solution by Kunal Kushwaha
*/
static int peakIndexInMountainArray(int[] nums) {
int startIndex = 0;
int endIndex = nums.length -1;
while (startIndex < endIndex){
int middle = startIndex + (endIndex - startIndex)/2;
if (nums[middle] > nums[middle+1]){
//You are in the Descending Part of the Array
//This may be the answer but look at Left of the Array
//This is why end!= mid -1;
endIndex = middle;
}else {
//You are in the Ascending part of the Array
startIndex = middle +1;
}
}
return startIndex;
}
}