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.

Be the first one to answer
Add answer anonymously...
BugsMirror Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter