Skip to content
Image
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Java 1D Array (Part 2) problem solution

Image YASH PAL, 31 July 202416 January 2026

HackerRank Java 1D Array (Part 2) problem solution – In this HackerRank java Array (Part 2) problem in the java programming language. 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.

HackerRank Java 1D Array (Part 2) problem solution

HackerRank Java 1D Array (Part 2) problem solution.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            //System.out.println(n + " " + m);
            int[] arr = new int[n];
            for (int j = 0; j < n; j++) {
                arr[j] = scanner.nextInt();
                //System.out.print(arr[j]);
            }
            //System.out.println();
            solve(n,m,arr);
        }
    }
    
    public static void solve(int n, int m, int[] arr) {
        if (solve(n,m,arr,new boolean[n],0)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
    
    public static boolean solve(int n, int m, int[] arr, boolean[] visited, int curr) {
        if (curr + m >= n || curr + 1 == n) {
            return true;
        }
        
        boolean[] newVisited = new boolean[n];
        for (int i = 0; i < n; i++) {
            newVisited[i] = visited[i];
        }
        
        boolean s = false;
        if (!visited[curr+1] && arr[curr+1] == 0) {
            newVisited[curr+1] = true;
            s = solve(n,m,arr,newVisited,curr+1);
        }
        if (s) {
            return true;
        }
        if (m > 1 && arr[curr+m] == 0 && !visited[curr+m]) {
            newVisited[curr+m] = true;
            s = solve(n,m,arr,newVisited,curr+m);
        }
        if (s) {
            return true;
        }
        if (curr > 0 && arr[curr-1] == 0 && !visited[curr-1]) {
            newVisited[curr-1] = true;
            s = solve(n,m,arr,newVisited,curr-1); 
        }
        return s;
    }
}

Second solution in java8 programming.

import java.util.*;

public class Solution {

    public static boolean canWin(int leap, int[] game) {
    VirtualPlayer v = new VirtualPlayer(leap, game);
    v.tick();
    return v.winnable;

}
static class VirtualPlayer {
    private int pos = 0; //pos-ition
    private int lp; //lp means leap
    private int[] map;
    boolean winnable = false;
    public VirtualPlayer(int lp, int[] map) {
        this.lp = lp; //gets the values from the canWin function
        this.map = map;

    }
    private void moveup() {
        if (map[pos + 1] == 0) {
            pos++;
            tick();
        }

    }

    private void movedown() {
        if ((pos - 1) >= 0 && map[pos - 1] == 0) {
            map[pos] = 1;
            pos--;
            tick();
        }

    }

    private void jump() {
        if ((pos + lp) < map.length) {
            if (map[pos + lp] == 0) {
                int posold = pos;
                pos = pos + lp;
                tick();
                pos = posold;
            }
        }

    }
    void tick() {
        //System.out.println("im at:"+pos);
        if (pos == (map.length - 1) || ((pos + lp) >= map.length)) {
            winnable = true;
        }
        else {
            this.moveup();
            if (lp != 0) {
                this.jump();
            } //cant jump 0 steps!
            this.movedown();
        }

    }

}

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int q = scan.nextInt();
        while (q-- > 0) {
            int n = scan.nextInt();
            int leap = scan.nextInt();
            
            int[] game = new int[n];
            for (int i = 0; i < n; i++) {
                game[i] = scan.nextInt();
            }

            System.out.println( (canWin(leap, game)) ? "YES" : "NO" );
        }
        scan.close();
    }
}

Third solution

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution 
{
    public static void main(String[] args) 
    {
        Scanner scanner = new Scanner(System.in);
        int caseCount = scanner.nextInt();
        for(int ii=0; ii<caseCount; ++ii)
        {
            int arraySize = scanner.nextInt();
            int jumpSize = scanner.nextInt();
            int[] array = new int[arraySize];
            for(int jj=0; jj<arraySize; ++jj) array[jj] = scanner.nextInt();
            // --
            boolean solvableGame = solveGame(array, jumpSize, 0, new boolean[array.length]);
            System.out.println(solvableGame?"YES":"NO");
        }
    }
    public static boolean solveGame(int[] array, int jumpSize, int position, boolean[] testedPositions)
    {
        // System.out.println("solveGame:"+position);
        if(position < 0) return false;
        if(position >= array.length) return true;
        if(testedPositions[position]) return false;
        if(array[position]==1) return false;
        // --
        // -- test jump back case is pointless
        // -- which just repeat the last position
        // --
        testedPositions[position]=true;
        return solveGame(array, jumpSize, position+jumpSize, testedPositions)
            || solveGame(array, jumpSize, position+1, testedPositions) 
            || solveGame(array, jumpSize, position-1, testedPositions);
        
    }
}
coding problems solutions Hackerrank Problems Solutions Java solutions HackerRankjava

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank Java Solutions
Welcome to Java! problem solution
Java Stdin and Stdout I problem solution
Java If-Else problem solution
Java Stdin and Stdout II problem solution
Java Output Formatting problem solution
Java Loops I problem solution
Java Loops II problem solution
Java Datatypes problem solution
Java End-of-file problem solution
Java Static Initializer Block solution
Java Int to String problem solution
Java Date and Time problem solution
Java Currency Formatter problem solution
Java Strings Introduction problem solution
Java Substring problem solution
Java Substring Comparisons problem solution
Java String Reverse problem solution
Java Anagrams problem solution
Java String Tokens problem solution
Pattern Syntax Checker solution
Java Regex problem solution
Java Regex 2 — Duplicate Words solution
Valid Username Regular Expression solution
Java ArrayList problem solution
Java BigDecimal problem solution
Java Primality Test problem solution
Java BigInteger problem solution
Java 1D Array problem solution
Java 2D Array problem solution
java Subarray problem solution
Java Arraylist problem solution
Java 1D Array (Part 2) problem solution
Java List problem solution
Java Map problem solution
Java Stack problem solution
Java Hashset problem solution
Java Generics problem solution
Java Comparator problem solution
Java Sort problem solution
Java Dequeue problem solution
Java BitSet problem solution
Java Priority Queue problem solution
Java Inheritance I problem solution
Java Inheritance II problem solution
Java Abstract Class problem solution
Java Interface problem solution
Java Method Overriding problem solution
Java Method Overriding 2 (Super Keyword) solution
Java Instanceof keyword problem solution
Java Iterator problem solution
Java Exception Handling (Try-catch) solution
Java Exception Handling problem solution
Java Varargs — Simple Addition solution
Java Reflection — Attributes problem solution
Can You Access? problem solution
Prime Checker problem solution
Java Factory Pattern problem solution
Java Singleton Pattern problem solution
Java Visitor Pattern problem solution
Java Annotations problem solution
Covariant Return Types solution
Java Lambda Expressions problem solution
Java MD5 problem solution
Java SHA-256 problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes
Advertisement