River Crossing: Help Ninja Cross Over

Ninja stands at the start of a wooden bridge over a river. Some segments of the bridge are damaged. The undamaged segments, marked as 'safe', are listed in an array SAFE, sorted in increasing order. Ninja must navigate from the first to the last safe segment by jumping from one safe segment to another.

Can Ninja make it across?

Input:

The first line contains an integer ‘T’ - the number of test cases.
For each test case, the first line contains an integer ‘N’ - the number of safe units.
The second line contains ‘N’ distinct integers representing the ‘SAFE’ units.

Output:

For each test case, return 'true' if Ninja can cross the river. Otherwise, return 'false'.

Example:

Input:
1
4
1 2 4 7
Output:
true
Explanation:

Ninja can cross by jumping 1 unit to 2, then 2 units to 4, and finally 3 units to 7.

Constraints:

  • 1 <= T <= 10
  • 2 <= N <= 10^3
  • 0 <= SAFE[i] <= 10^5
  • Time limit: 1 second

Note:

The first jump is always 1 unit. If the previous jump was 'X' units, the next can be 'X' - 1, 'X', or 'X' + 1 units. Implement the function without printing directly.
AnswerBot
4mo

Check if Ninja can cross the river by jumping from one safe segment to another based on given constraints.

  • Iterate through the 'SAFE' array and check if Ninja can make the required jumps to reach the l...read more

Help your peers!
Select
Add answer anonymously...

ThoughtWorks Software Developer interview questions & answers

A Software Developer was asked Q. How would you add an API to the existing repository?
A Software Developer was asked Q. In a code pairing round, you will be given a story where you need to add an API ...read more
A Software Developer was asked Q. What is the difference between CHAR and VARCHAR2 data types in SQL?

Popular interview questions of Software Developer

A Software Developer was asked Q1. How would you add an API to the existing repository?
A Software Developer was asked Q2. In a code pairing round, you will be given a story where you need to add an API ...read more
A Software Developer was asked Q3. What is the difference between CHAR and VARCHAR2 data types in SQL?
ThoughtWorks Software Developer 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