Skip to content

Commit b98ad3a

Browse files
committed
Bitonic Array Maximum
1 parent 2ba8c8e commit b98ad3a

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"Bigtable",
2020
"Bitmask",
2121
"Bitmasking",
22+
"Bitonic",
2223
"Bloomberg",
2324
"Blum",
2425
"Buschmann",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package EducativeIo.Courses.GrokkingTheCodingInterview.Ch12_ModifiedBinarySearch.P7_BitonicArrayMaximum.Java;
2+
3+
public class Solution {
4+
public static void main(String[] args) {
5+
System.out.println(findMax(new int[] {1, 3, 8, 12, 4, 2}));
6+
System.out.println(findMax(new int[] {3, 8, 3, 1}));
7+
System.out.println(findMax(new int[] {1, 3, 8, 12}));
8+
System.out.println(findMax(new int[] {10, 9, 8}));
9+
}
10+
11+
public static int findMax(int[] arr) {
12+
int start = 0, end = arr.length - 1;
13+
while (start < end) {
14+
int mid = start + (end - start) / 2;
15+
if (arr[mid] > arr[mid + 1]) {
16+
end = mid;
17+
} else {
18+
start = mid + 1;
19+
}
20+
}
21+
22+
// at the end of the while loop, 'start == end'
23+
return arr[start];
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package EducativeIo.Courses.GrokkingTheCodingInterview.Ch12_ModifiedBinarySearch.P7_BitonicArrayMaximum.Java;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.time.Duration;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
10+
public class SolutionTest {
11+
12+
Solution solution;
13+
14+
@BeforeEach
15+
public void setUp() throws Exception {
16+
solution = new Solution();
17+
}
18+
19+
@AfterEach
20+
public void tearDown() throws Exception {
21+
solution = null;
22+
}
23+
24+
@Test
25+
public void MainFunction() {
26+
assertTimeout(Duration.ofMillis(500), () -> {
27+
String[] args = new String[0];
28+
assertAll(() -> Solution.main(args));
29+
});
30+
}
31+
32+
@Test
33+
public void TrivialCase1() {
34+
int[] arr = new int[] {1, 3, 8, 12, 4, 2};
35+
assertTimeout(Duration.ofMillis(500), () -> {
36+
int expected = 12;
37+
int actual = Solution.findMax(arr);
38+
assertEquals(expected, actual);
39+
});
40+
}
41+
42+
@Test
43+
public void TrivialCase2() {
44+
int[] arr = new int[] {3, 8, 3, 1};
45+
assertTimeout(Duration.ofMillis(500), () -> {
46+
int expected = 8;
47+
int actual = Solution.findMax(arr);
48+
assertEquals(expected, actual);
49+
});
50+
}
51+
52+
@Test
53+
public void TrivialCase3() {
54+
int[] arr = new int[] {1, 3, 8, 12};
55+
assertTimeout(Duration.ofMillis(500), () -> {
56+
int expected = 12;
57+
int actual = Solution.findMax(arr);
58+
assertEquals(expected, actual);
59+
});
60+
}
61+
62+
@Test
63+
public void TrivialCase4() {
64+
int[] arr = new int[] {10, 9, 8};
65+
assertTimeout(Duration.ofMillis(500), () -> {
66+
int expected = 10;
67+
int actual = Solution.findMax(arr);
68+
assertEquals(expected, actual);
69+
});
70+
}
71+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"type": "Coding",
3+
"name": "Bitonic Array Maximum",
4+
"origin": {
5+
"name": "Educative",
6+
"link": "https://www.educative.io/courses/grokking-the-coding-interview/RMyRR6wZoYK"
7+
},
8+
"companies": ["", ""],
9+
"categories": [{
10+
"name": "Courses",
11+
"children": [{
12+
"name": "Grokking the Coding Interview: Patterns for Coding Questions",
13+
"children": []
14+
}]
15+
},
16+
{
17+
"name": "Difficulty",
18+
"children": [{
19+
"name": "Easy",
20+
"children": []
21+
}]
22+
},
23+
{
24+
"name": "Pattern",
25+
"children": [{
26+
"name": "Modified Binary Search",
27+
"children": []
28+
}]
29+
}
30+
],
31+
"tags": ["Modified Binary Search"],
32+
"buckets": []
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Problem Definition
2+
3+
## Description
4+
5+
Find the maximum value in a given Bitonic array. An array is considered bitonic if it is monotonically increasing and then monotonically decreasing. Monotonically increasing or decreasing means that for any index `i` in the array `arr[i] != arr[i+1]`.
6+
7+
Example 1:
8+
9+
```plaintext
10+
Input: [1, 3, 8, 12, 4, 2]
11+
Output: 12
12+
Explanation: The maximum number in the input bitonic array is '12'.
13+
```
14+
15+
Example 2:
16+
17+
```plaintext
18+
Input: [3, 8, 3, 1]
19+
Output: 8
20+
```
21+
22+
Example 3:
23+
24+
```plaintext
25+
Input: [1, 3, 8, 12]
26+
Output: 12
27+
```
28+
29+
Example 4:
30+
31+
```plaintext
32+
Input: [10, 9, 8]
33+
Output: 10
34+
```
35+
36+
## Discussion
37+
38+
A bitonic array is a sorted array; the only difference is that its first part is sorted in ascending order and the second part is sorted in descending order. We can use a similar approach as discussed in **Order-agnostic Binary Search**. Since no two consecutive numbers are same (as the array is monotonically increasing or decreasing), whenever we calculate the `middle`, we can compare the numbers pointed out by the index `middle` and `middle+1` to find if we are in the ascending or the descending part. So:
39+
40+
1. If `arr[middle] > arr[middle + 1]`, we are in the second (descending) part of the bitonic array. Therefore, our required number could either be pointed out by `middle` or will be before `middle`. This means we will be doing: `end = middle`.
41+
2. If `arr[middle] <= arr[middle + 1]`, we are in the first (ascending) part of the bitonic array. Therefore, the required number will be after `middle`. This means we will be doing: `start = middle + 1`.
42+
43+
We can break when `start == end`. Due to the two points mentioned above, both `start` and `end` will be pointing at the maximum number of the bitonic array.
44+
45+
### Time Complexity
46+
47+
Since we are reducing the search range by half at every step, this means that the time complexity of our algorithm will be O(logN) where ‘N’ is the total elements in the given array.
48+
49+
### Space Complexity
50+
51+
The algorithm runs in constant space O(1).
52+
53+
## Notes
54+
55+
## References

0 commit comments

Comments
 (0)