City Bus Loop Access
Given an array of integers, determine whether the array contains at least one non-zero value. Return true if any element is not zero, and false if all elements are zero or the array is empty.
For example, [1, 2, 3] returns true because it contains non-zero values. [0, 0, 0] returns false because every element is zero. An empty array also returns false because there is nothing to satisfy the condition. Negative numbers like -5 count as non-zero, so [-5, 0, 5] returns true.
This is a simple existence check: you only need to find one element that meets the condition. As soon as you encounter a non-zero value, you can immediately return true. If you scan the entire array without finding any non-zero element, return false. This early-exit pattern is common in search problems and can significantly improve performance when the matching element appears early.
The problem is intentionally straightforward to test your understanding of basic array iteration and conditional logic. Despite its simplicity, the early-return pattern it demonstrates is a building block for more complex search algorithms like linear search, find-first, and any-match operations used throughout software development.
Edge cases include an empty array (return false), an array of all zeros (return false), and an array with a single non-zero element (return true immediately on the first element). The solution must handle all these cases correctly without throwing exceptions.
Example Input & Output
The list contains non-zero values, so the check passes.
Every value is zero, so there is no qualifying element.
Negative values still count because they are different from zero.
Algorithm Flow
Solution Approach
The simplest solution scans the array and returns true as soon as it finds a non-zero value. If the loop completes without finding any non-zero, return false.
Initialize a loop from index 0 to the last element. For each element, check if it is not equal to zero. If a non-zero value is found, return true immediately — there is no need to check the remaining elements. If the loop finishes without returning, all elements were zero (or the array was empty), so return false.
The early-return optimization means the best case is O(1) when a non-zero value is at the first position. The worst case is O(n) when all values are zero or the non-zero value is at the very end. Space complexity is O(1) since only a loop variable is used.
Some languages provide built-in helpers like JavaScript's Array.prototype.some() or Python's any(), which express the same logic more concisely: return nums.some(function(n){return n !== 0;}) or return any(n != 0 for n in nums). However, the explicit loop better illustrates the underlying algorithm.
Best Answers
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();
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
