# Hackerrank Solution: 1D Array – Part 2 using Kotlin

This tutorial will show you Hackerrank 1D Array Part 2 using Kotlin.

Let’s play a game on an array! You’re standing at index 0 of an n-element array named game. From some index i (where 0 <= i < n), you can perform one of the following moves:

Move Backward: If cell i-1 exists and contains a 0, you can walk back to cell i-1.

Move Forward:

If cell i+1 contains a zero, you can walk to cell i+1.

If cell i+leap contains a zero, you can jump to cell i+leap.

If you’re standing in cell n-1 or the value of i+leap >= n, you can walk or jump off the end of the array and win the game.

In other words, you can move from index i to index i+1, i-1, or i+leap as long as the destination index is a cell containing a 0. If the destination index is greater than n-1, you win the game.

Given leap and game, complete the function in the editor below so that it returns true if you can win the game (or false if you cannot).

Input Format
The lines describe each query over two lines:

The first line contains two space-separated integers describing the respective values of n and leap.

The second line contains n space-separated binary integers (i.e., zeroes and ones) describing the respective values of game0, game1, game2,…, game(n-1).

Output Format

Return true or YES if you can win the game; otherwise, return false or NO.
Sample Input

```5 3
0 0 0 0 0
6 5
0 0 0 1 1 1
6 3
0 0 1 1 1 0
3 1
0 1 0```

Sample Output

```YES
YES
NO
NO```

The complete source code
Please go through first how to create Kotlin project in Eclipse

Here we will create Hackerrank 1d array part 2 using Kotlin programming language.

Notice in the below Kotlin source code we have Companion object{}, that means, it is equivalent to static method in Java. We want to access isGameWon() method without creating any instance of the class Game1DArrayPart2.

```package com.roytuts.kt
class Game1DArrayPart2 {
companion object {
fun isGameWon(arr: IntArray, leap: Int): String {
val stepSize = arr.size;
var steps = 0;
var won = false;
val strategy = ArrayList<Int>()
while (strategy.size > 0) {
var move = strategy.last();
var sz = strategy.count();
strategy.removeAt(sz - 1);
if (move >= stepSize - 1) {
won = true;
break;
}
if (arr[move] == 0 && move + leap < stepSize && arr[move + leap] == 0 || move + leap >= stepSize) {
++steps;
var minm = move + leap;
if (minm > 0 && minm < stepSize) {
while (arr[minm] == 0 && minm > 0 && arr[minm - 1] == 0) {
--minm;
}
++steps;
}
}
if (arr[move] == 0 && move + 1 < stepSize && arr[move + 1] == 0 || move + 1 >= stepSize) {
++steps;
}
if (steps > 10 * stepSize) {
break;
}
}
return (if (won) "YES" else "NO")
}
}
}
fun main(args: Array<String>) {
println(Game1DArrayPart2.isGameWon(intArrayOf(0, 0, 0, 0, 0), 3));
println(Game1DArrayPart2.isGameWon(intArrayOf(0, 0, 0, 1, 1, 1), 5));
println(Game1DArrayPart2.isGameWon(intArrayOf(0, 0, 1, 1, 1, 0), 3));
println(Game1DArrayPart2.isGameWon(intArrayOf(0, 1, 0), 1));
}```

Output

```YES
YES
NO
NO```