Target with Repeated 1, 2 and 3 Jumps

Last Updated : 25 Jul, 2026

Geek starts at point 0 on a number line. He jumps in a repeating pattern of lengths 1, 2, 3, 1, 2, 3.... and so on. A jump of length 1 moves him from P to P + 1. A jump of length 2 moves him from P to P + 2. A jump of length 3 moves him from P to P + 3.

Given an integer n, find if Geek can land exactly on point n. Return true if he can land otherwise, return false.

Examples:

Input: n = 1
Output: true
Explanation: Geek will land at Position 1 after the 1st jump.

Input: n = 8
Output: false
Explanation: Geek can't land at Position 8.

Try It Yourself
redirect icon

[Naive Approach] Using Direct Simulation - O(n) time and O(1) Space

Since Geek always follows the fixed jump pattern 1, 2, 3 repeatedly, we can simply simulate each jump starting from position 0. During the simulation, if Geek lands exactly on n, we return true; if he crosses n, it is impossible to reach the target, so we return false.

  • If n is 0, return true since Geek is already at the starting position.
  • Initialize position = 0 and store the jump pattern as {1, 2, 3}.
  • Repeatedly add the current jump length to position, cycling through the jump pattern.
  • If position becomes equal to n, return true.
  • If position exceeds n, stop the simulation.
  • Return false since Geek cannot land exactly on n.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to determine whether Geek can land exactly on point n.
bool jumpingGeek(int n)
{
    if (n == 0)
    {
        return true;
    }

    // Current position of Geek.
    int position = 0;

    // Jump pattern: 1, 2, 3 (repeats continuously).
    int jump[] = {1, 2, 3};

    // Index to track the current jump length.
    int idx = 0;

    // Keep making jumps until Geek reaches or crosses n.
    while (position < n)
    {
        position += jump[idx];

        // If Geek lands exactly on n, return true.
        if (position == n)
            return true;

        // Move to the next jump in cyclic order.
        idx = (idx + 1) % 3;
    }

    // Geek has crossed n without landing on it.
    return false;
}

int main()
{
    int n = 8;

    if (jumpingGeek(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.io.*;

public class GFG {

    // Function to determine whether Geek can land exactly
    // on point n.
    static boolean jumpingGeek(int n)
    {
        if (n == 0) {
            return true;
        }

        // Current position of Geek.
        int position = 0;

        // Jump pattern: 1, 2, 3 (repeats continuously).
        int[] jump = { 1, 2, 3 };

        // Index to track the current jump length.
        int idx = 0;

        // Keep making jumps until Geek reaches or crosses
        // n.
        while (position < n) {
            position += jump[idx];

            // If Geek lands exactly on n, return true.
            if (position == n)
                return true;

            // Move to the next jump in cyclic order.
            idx = (idx + 1) % 3;
        }

        // Geek has crossed n without landing on it.
        return false;
    }

    public static void main(String[] args)
    {
        int n = 8;

        if (jumpingGeek(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
# Function to determine whether Geek can land exactly on point n.
def jumpingGeek(n):
    if n == 0:
        return True

    # Current position of Geek.
    position = 0

    # Jump pattern: 1, 2, 3 (repeats continuously).
    jump = [1, 2, 3]

    # Index to track the current jump length.
    idx = 0

    # Keep making jumps until Geek reaches or crosses n.
    while position < n:
        position += jump[idx]

        # If Geek lands exactly on n, return True.
        if position == n:
            return True

        # Move to the next jump in cyclic order.
        idx = (idx + 1) % 3

    # Geek has crossed n without landing on it.
    return False


# Driver Code
if __name__ == "__main__":
    n = 8

    if jumpingGeek(n):
        print("true")
    else:
        print("false")
C#
using System;

class GFG {
    
    // Function to determine whether Geek can land exactly
    // on point n.
    static bool jumpingGeek(int n)
    {
        if (n == 0) {
            return true;
        }

        // Current position of Geek.
        int position = 0;

        // Jump pattern: 1, 2, 3 (repeats continuously).
        int[] jump = { 1, 2, 3 };

        // Index to track the current jump length.
        int idx = 0;

        // Keep making jumps until Geek reaches or crosses
        // n.
        while (position < n) {
            position += jump[idx];

            // If Geek lands exactly on n, return true.
            if (position == n)
                return true;

            // Move to the next jump in cyclic order.
            idx = (idx + 1) % 3;
        }

        // Geek has crossed n without landing on it.
        return false;
    }

    static void Main()
    {
        int n = 8;

        if (jumpingGeek(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// Function to determine whether Geek can land exactly on
// point n.
function jumpingGeek(n)
{
    if (n === 0) {
        return true;
    }

    // Current position of Geek.
    let position = 0;

    // Jump pattern: 1, 2, 3 (repeats continuously).
    const jump = [ 1, 2, 3 ];

    // Index to track the current jump length.
    let idx = 0;

    // Keep making jumps until Geek reaches or crosses n.
    while (position < n) {
        position += jump[idx];

        // If Geek lands exactly on n, return true.
        if (position === n)
            return true;

        // Move to the next jump in cyclic order.
        idx = (idx + 1) % 3;
    }

    // Geek has crossed n without landing on it.
    return false;
}

// Driver Code
let n = 8;

if (jumpingGeek(n))
    console.log("true");
else
    console.log("false");

Output
false

[Expected Approach] Using Mathematical Observation - O(1) Time and O(1) Space

Instead of simulating every jump, observe the positions reached by Geek:

0, 1, 3, 6, 7, 9, 12, 13, 15, 18, ...

Every three jumps (1 + 2 + 3) increase the position by 6. Thus, in every block of 6, Geek can only reach numbers of the form 6k, 6k + 1, and 6k + 3. Therefore, we only need to check the remainder when n is divided by 6.

  • If n is 0, return true.
  • Compute remainder = n % 6.
  • If the remainder is 0, 1, or 3, return true.
  • Otherwise, return false.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to determine whether Geek can land exactly on point n.
bool jumpingGeek(int n)
{
    // Compute the remainder when n is divided by 6.
    int rem = n % 6;

    // Geek can reach only numbers whose remainder is 0, 1, or 3.
    return (rem == 0 || rem == 1 || rem == 3);
}

int main()
{
    int n = 8;

    if (jumpingGeek(n))
        cout << "true";
    else
        cout << "false";

    return 0;
}
Java
import java.io.*;

public class GFG {

    // Function to determine whether Geek can land exactly
    // on point n.
    static boolean jumpingGeek(int n)
    {
        // Compute the remainder when n is divided by 6.
        int rem = n % 6;

        // Geek can reach only numbers whose remainder is 0,
        // 1, or 3.
        return (rem == 0 || rem == 1 || rem == 3);
    }

    public static void main(String[] args)
    {
        int n = 8;

        if (jumpingGeek(n))
            System.out.println("true");
        else
            System.out.println("false");
    }
}
Python
# Function to determine whether Geek can land exactly on point n.
def jumpingGeek(n):

    # Compute the remainder when n is divided by 6.
    rem = n % 6

    # Geek can reach only numbers whose remainder is 0, 1, or 3.
    return rem == 0 or rem == 1 or rem == 3


# Driver Code
if __name__ == "__main__":
    n = 8

    if jumpingGeek(n):
        print("true")
    else:
        print("false")
C#
using System;

class GFG {
    
    // Function to determine whether Geek can land exactly
    // on point n.
    static bool jumpingGeek(int n)
    {
        // Compute the remainder when n is divided by 6.
        int rem = n % 6;

        // Geek can reach only numbers whose remainder is 0,
        // 1, or 3.
        return (rem == 0 || rem == 1 || rem == 3);
    }

    static void Main()
    {
        int n = 8;

        if (jumpingGeek(n))
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// Function to determine whether Geek can land exactly on
// point n.
function jumpingGeek(n)
{
    // Compute the remainder when n is divided by 6.
    const rem = n % 6;

    // Geek can reach only numbers whose remainder is 0, 1,
    // or 3.
    return rem === 0 || rem === 1 || rem === 3;
}

// Driver Code
let n = 8;

if (jumpingGeek(n))
    console.log("true");
else
    console.log("false");

Output
false
Comment