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.
Be the first one to answer
Add answer anonymously...
ThoughtWorks Software Developer 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