15,666,122 members
See more:
Nidhi has created an alternate version of Subway Surfer game. Her new version is not on a train track unlimited length, rather limited to a track of length N + 1. Just like the original game, this alternate version also has three lanes.
The track has points on it ranging from 0 to N.
In the three lanes, at some points, certain manholes are present. Now, to avoid these manholes, our player needs to jump sideways and switch lanes, as he cannot go from i to (i + 1)th point on the same lane if there is no manhole on the lane at point i + 1.
You are given an array manholes of length N + 1 where manholes[i] tells us on which lane is the manhole present for point i. For example, if manholes[2] = 1, this means that on point 2 of the train track, a manhole is present on lane 1.
Find the minimum number of lane changes our player has to perform in order to reach from point 0 to point N on the train track.
Note:
If manholes[i] = 0, this means that for point i, there is no manhole present on any lane.
manholes[0] and manholes[N] are always 0.
Input Format:
First line of input contains integer T, representing the number of test cases.
First line of each test case contains the integer N.
Second line of each test case contains N + 1 space separated integers, representing manholes[i].
Our player always begins from the middle lane.
`Constraints:
manholes.length == n + 1
1 <= t <= 100
1 <= N <= 5 * 10^4
0 <= manholes[i] <= 3
manholes[0] == manholes[n] == 0
Output Format:
Minimum number of lane changes to reach from point 0 to N.
Sample Input:
4
0 1 2 3 0
Sample Output:
2
Explanation:
The optimal path followed by our player is shown in the image below:

[image for reference ](https://i.stack.imgur.com/Os2nS.png)

It is clear that to reach from point 0 till point N optimally, out player will have to change lanes twice at minimum.

providing sample test cases below :

Custom Test Case
1
4
0 1 2 3 0

EXECUTE

Result
2

Expected Output
2

Custom Test Case
1
9
0 3 0 2 0 2 1 2 2 0

EXECUTE

Result
0

Expected Output
1

Custom Test Case
3
1
0 0
2
0 2 0
3
0 3 3 0

EXECUTE

Result
0
0
0

Expected Output
0
1
0

Custom Test Case
2
2
0 0 0
9
0 1 1 1 1 3 3 3 2 0

EXECUTE

Result

1
2

Expected Output
0
1

What I have tried:

tried for testcases but some were wrong , need help in this , thankyou all
Posted
Updated 7-Mar-23 2:14am
v2
Dave Kreskowiak 7-Mar-23 7:47am
Did you plan on sharing the code you've written that you're having a problem with and describing the problems?
OriginalGriff 7-Mar-23 8:16am
Google (if you have the "Student" language pack) translates "I tried ... but ..." into "I had a shufti at google but can't find the code, so how about you write it for me?"
speed blaze 7-Mar-23 8:40am
hi all , i would like thank all for your utmost support , i'm learning and i've tried it myself , here i'm new , i'll share my code here

public class Solution {

static public int minLaneChanges(int[] manholes) {

int laneChanges = 0;

// Loop through each point on the track except the last one
for (int i = 0; i < manholes.length - 1;) {
// If there is a manhole at current point, we need to jump to another lane
if (manholes[i] != 0) {
int currentLane = manholes[i];
boolean jumped = false;

// Jump to middle lane first (if not already in it) and check if we can continue on this lane
if (currentLane != 1 && manholes[i + 1] != currentLane) {
laneChanges++;
i++;
jumped = true;
}
// Check if we can continue on the side lanes without jumping again
if (!jumped) {
if (manholes[i + 1] == 0) {
i++;
} else if (manholes[i + 1] != currentLane && manholes[i + 2] == currentLane) {
i += 2;
laneChanges++;
} else {
i += 2;
laneChanges++;
}
}
} else {
i++;
}
}

return laneChanges;
}
}

// the thing is for only one test case it was giving correct outputs , rest of em it was'nt
i'm not sure which aspect do i need to correct

## Solution 1

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

speed blaze 7-Mar-23 8:42am
hi thanks for your suggestions , i'm not getting paid for this code , i'm learning and improving my skills , sorry if i'm wrong in any way
OriginalGriff 7-Mar-23 8:51am
"i'm learning and improving my skills"
So use the debugger to follow your code through, and watch what it is doing while it runs. That way, you can spot when it goes wrong, and start working out why it went the way it did.
This is one of the really important skills in development - debugging!
speed blaze 7-Mar-23 9:02am
thanks but i'm not good at debugging but i'll do get on it