Skip to content

Commit 5a11efe

Browse files
solves get maximum in generted array
1 parent 7f18229 commit 5a11efe

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
| 1629 | [Slowest Key](https://leetcode.com/problems/slowest-key) | [![Java](assets/java.png)](src/SlowestKey.java) | |
408408
| 1636 | [Sort Array by Increasing Frequency](https://leetcode.com/problems/sort-array-by-increasing-frequency) | [![Java](assets/java.png)](src/SortArrayByIncreasingFrequency.java) | |
409409
| 1640 | [Check Array Formation Through Concatenation](https://leetcode.com/problems/check-array-formation-through-concatenation) | [![Java](assets/java.png)](src/CheckArrayFormationThroughConcatenation.java) | |
410-
| 1646 | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array) | | |
410+
| 1646 | [Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array) | [![Java](assets/java.png)](src/GetMaximumInGeneratedArray.java) | |
411411
| 1652 | [Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb) | | |
412412
| 1656 | [Design an Ordered Stream](https://leetcode.com/problems/design-an-ordered-stream) | | |
413413
| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent) | | |

src/GetMaximumInGeneratedArray.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Arrays;
2+
3+
public class GetMaximumInGeneratedArray {
4+
public int getMaximumGenerated(int n) {
5+
if (n == 0) return 0;
6+
final int[] array = new int[n + 1];
7+
array[0] = 0;
8+
array[1] = 1;
9+
for (int index = 2 ; index < array.length ; index++) {
10+
if ((index & 1) == 0) array[index] = array[index / 2];
11+
else array[index] = array[index / 2] + array[index - index / 2];
12+
}
13+
return Arrays.stream(array).max().getAsInt();
14+
}
15+
}

0 commit comments

Comments
 (0)