Skip to content

Commit fdec1ab

Browse files
add solution file
1 parent 92a97f1 commit fdec1ab

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

.idea/workspace.xml

+16-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ch6Arrays
2+
3+
fun main(args: Array<String>) {
4+
//[3,2,1,0,0,0,1] -> false
5+
//[3,3,2,0,2,1] -> true
6+
println(canReachTheEnd(arrayOf(3, 2, 1, 0, 0, 0, 1)))
7+
println(canReachTheEnd(arrayOf(3, 3, 2, 0, 2, 1)))
8+
}
9+
10+
fun canReachTheEnd(array: Array<Int>): Boolean {
11+
var maximumIndex = 0
12+
array.forEachIndexed { index, number ->
13+
if (index <= maximumIndex) {
14+
maximumIndex = Math.max(maximumIndex, index + number)
15+
}
16+
// optimisation, if we know we can reach the at some point no need
17+
// to continue the loop
18+
if (maximumIndex >= array.size - 1) {
19+
return true
20+
}
21+
}
22+
23+
return maximumIndex >= array.size - 1
24+
}
25+

0 commit comments

Comments
 (0)