Code Logo

City Bus Loop Access

Published at05 Jan 2026
BFS Medium 16 views
Like27

In

City Bus Loop Access

, the tested task is a yes-or-no check on a plain list of numbers. You are given an array and need to return

true

if at least one value in the array is not zero. If every value is zero, or the array is empty, return

false

.

This means the problem is not about adding the numbers, sorting them, or comparing them to a target. The only question is whether the list contains any non-zero entry at all. A positive number counts, and a negative number also counts, because both are different from zero.

For example,

nums = [1,2,3]

returns

true

because the list clearly contains non-zero values. The array

[0,0,0]

returns

false

because every entry is zero. The array

[5,-5,5]

returns

true

because all of its values are non-zero. An empty array returns

false

because there is nothing inside it that could satisfy the rule.

So the task is simply to scan the list and decide whether any element is different from zero.

Example Input & Output

Example 1
Input
nums = [1,2,3]
Output
true
Explanation

The list contains non-zero values, so the check passes.

Example 2
Input
nums = [0,0,0]
Output
false
Explanation

Every value is zero, so there is no qualifying element.

Example 3
Input
nums = [5,-5,5]
Output
true
Explanation

Negative values still count because they are different from zero.

Algorithm Flow

Recommendation Algorithm Flow for City Bus Loop Access
Recommendation Algorithm Flow for City Bus Loop Access

Solution Approach

A simple one-pass scan is enough to solve this problem. Since the only thing you need to know is whether the array contains at least one non-zero value, you can stop as soon as you find one. There is no need to inspect the rest of the list after that point.

The key condition is

num !== 0

. That is all the problem is asking about. A value like

5

should make the answer

true

, and a value like

-5

should also make the answer

true

because it is still not zero. Only exact zero values fail the condition.

In JavaScript, the core logic can look like this:

<p>for (const num of nums) {
  if (num !== 0) {
    return true;
  }
}
return false;</p>

This works because the first non-zero value is enough to prove the answer. In

[1, 2, 3]

, the very first element already gives you

true

. In

[0, 0, 0]

, the loop finishes without finding any non-zero number, so the answer stays

false

. In

[5, -5, 5]

, even the negative value would still count as success because the comparison only cares whether the number equals zero.

You could also solve this with a built-in helper such as

some()

, but the explicit loop is often clearer when explaining the reasoning. It shows directly that the algorithm is making one pass, checking one condition, and returning early when possible.

This approach handles the edge cases naturally. An empty array returns

false

because the loop never runs and no qualifying element is found. A one-element array like

[10]

returns

true

if that single value is non-zero. If the single value were

0

, the answer would be

false

.

The time complexity is

O(n)

in the worst case because you may need to inspect every element. The space complexity is

O(1)

because the solution only uses a loop variable and a direct comparison.

So the full strategy is: scan the array from left to right, return

true

on the first value that is not zero, and return

false

only if no such value exists.

Best Answers

java
import java.util.*;

class Solution {
    public int city_bus_loop_access(int n, int[][] roads, int start, int[] closed_stations) {
        Set<Integer> closed = new HashSet<>();
        for (int s : closed_stations) closed.add(s);
        if (closed.contains(start)) return 0;
        List<Integer>[] adj = new ArrayList[n];
        for (int i = 0; i < n; i++) adj[i] = new ArrayList<>();
        for (int[] road : roads) {
            adj[road[0]].add(road[1]);
            adj[road[1]].add(road[0]);
        }
        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        visited.add(start);
        queue.add(start);
        while (!queue.isEmpty()) {
            int u = queue.poll();
            for (int v : adj[u]) {
                if (!visited.contains(v) && !closed.contains(v)) {
                    visited.add(v);
                    queue.add(v);
                }
            }
        }
        return visited.size();
    }
}