Code Logo

Mountain Cabin Signal Range

Published at05 Jan 2026
Easy 4 views
Like30

In Mountain Cabin Signal Range, you are given an array of numbers and need to return the overall range of the data. Here, range means the largest value minus the smallest value.

This is not about counting how many values there are between them, and it is not about the difference between neighboring entries. You should look across the whole array, find the maximum number, find the minimum number, and subtract.

For example, if the input is altitudes = [1000,2000,500], the answer is 1500 because the highest value is 2000 and the lowest value is 500. If the input is altitudes = [10,20,30], the answer is 20. If all values are the same, like [5,5,5], the range is 0 because the maximum and minimum are equal.

An empty array should return 0 in this export. So the task is to find the largest and smallest values in the array and return their difference, or 0 when the list is empty.

Example Input & Output

Example 1
Input
altitudes = [1000,2000,500]
Output
1500
Explanation

The maximum is 2000 and the minimum is 500, so the range is 1500.

Example 2
Input
altitudes = [10,20,30]
Output
20
Explanation

The highest value is 30 and the lowest is 10, so the difference is 20.

Example 3
Input
altitudes = [5,5,5]
Output
0
Explanation

When every value is the same, the minimum and maximum match, so the range is 0.

Algorithm Flow

Recommendation Algorithm Flow for Mountain Cabin Signal Range
Recommendation Algorithm Flow for Mountain Cabin Signal Range

Solution Approach

A clean way to solve this problem is to scan the array once while keeping track of two values: the smallest number seen so far and the largest number seen so far. Once the scan is finished, subtract the minimum from the maximum. That directly gives the range.

The main reason this works well is that the problem only cares about the extreme ends of the array. You do not need to sort the full list, compare every pair, or build extra structures. Sorting would work, but it would do more work than necessary because the middle values do not affect the final answer at all.

In JavaScript, the logic can look like this:

if (altitudes.length === 0) return 0;

let minVal = altitudes[0];
let maxVal = altitudes[0];

for (let i = 1; i < altitudes.length; i++) {
  if (altitudes[i] < minVal) minVal = altitudes[i];
  if (altitudes[i] > maxVal) maxVal = altitudes[i];
}

return maxVal - minVal;

This starts by handling the empty-array case, because there is no minimum or maximum when the list has no values. The tests for this problem expect 0 there, so returning early is the simplest choice.

For a non-empty array, the first element is used to initialize both minVal and maxVal. Then the loop checks each remaining value. If the current number is smaller than the current minimum, update minVal. If it is larger than the current maximum, update maxVal. At the end, the difference maxVal - minVal is the range.

This handles all the key cases naturally. If all values are equal, the minimum and maximum stay the same, so the answer becomes 0. If the array contains negative values, the same logic still works because numeric comparison already handles them correctly. In an array like [100, -100], the minimum becomes -100, the maximum becomes 100, and the range is 200.

The time complexity is O(n) because you inspect each element once. The space complexity is O(1) because only a few variables are used. So the full strategy is: return 0 for an empty array, otherwise track the minimum and maximum in one pass and subtract them at the end.

Best Answers

java
class Solution {
    public double calculate_min_range(int[][] cabins) {
        int n = cabins.length;
        if (n <= 1) return 0.0;
        double[] minD = new double[n];
        Arrays.fill(minD, Double.MAX_VALUE);
        minD[0] = 0.0;
        boolean[] visited = new boolean[n];
        double res = 0.0;
        for (int i = 0; i < n; i++) {
            int u = -1;
            for (int j = 0; j < n; j++) {
                if (!visited[j] && (u == -1 || minD[j] < minD[u])) u = j;
            }
            visited[u] = true;
            res = Math.max(res, minD[u]);
            for (int v = 0; v < n; v++) {
                if (!visited[v]) {
                    double d = Math.sqrt(Math.pow(cabins[u][0] - cabins[v][0], 2) + Math.pow(cabins[u][1] - cabins[v][1], 2));
                    minD[v] = Math.min(minD[v], d);
                }
            }
        }
        return res;
    }
}