Ninja's Jump Task

The Ninja has been given a challenge by his master to reach the last stone. These stones are represented as an array of numbers. The Ninja can jump using either odd-numbered or even-numbered jumps. Your task is to determine how many starting indices allow the Ninja to jump from the beginning to the last stone following these rules.

Instructions for Jumping:

  • During odd-numbered jumps (e.g., jumps 1, 3, 5,...), jump from index i to index j (where i < j) such that arr[i] <= arr[j] and arr[j] is the smallest possible value. If there are multiple valid indices, choose the smallest j.
  • During even-numbered jumps (e.g., jumps 2, 4, 6,...), jump from index i to index j (where i < j) such that arr[i] >= arr[j] and arr[j] is the largest possible value. If there are multiple valid indices, choose the smallest j.
  • Some indices may have no possible jumps.

Input:

The first line contains an integer 'T' representing the number of test cases.
For each test case, the first line contains an integer ‘N’ which indicates the size of the array ‘arr’.
The following line contains 'N' space-separated integers, which are the elements of ‘arr’.

Output:

For each test case, print the number of starting indices from which Ninja can reach the last stone by following the jump rules.

Example:

Input:
2
5
10 13 12 14 15
6
2 3 1 1 4 5
Output:
2
3

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 5000
  • 0 <= arr[i] < 10^6
  • Time limit: 1 second

Note:

You do not need to print anything as it is handled automatically. Simply implement the function to calculate the answer.

AnswerBot
4mo

Find the number of starting indices from which a Ninja can reach the last stone by following specific jump rules.

  • Iterate through the array and keep track of the possible jumps for each index.

  • Use dynam...read more

Help your peers!
Select
Add answer anonymously...

BugsMirror Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. What is the difference between deadlock and livelock?
A Software Developer Intern was asked Q. Find the Lone Set Bit Your task is to identify the position of the only '1' bit ...read more
A Software Developer Intern was asked Q. Ninja's Jump Task The Ninja has been given a challenge by his master to reach th...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. What is the difference between deadlock and livelock?
A Software Developer Intern was asked Q2. Find the Lone Set Bit Your task is to identify the position of the only '1' bit ...read more
A Software Developer Intern was asked Q3. Ninja's Jump Task The Ninja has been given a challenge by his master to reach th...read more
BugsMirror Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits