Code Logo

Gallery Stock Order Check

Published at05 Jan 2026
Easy 4 views
Like20

This problem asks whether a list of gallery stock widths is already in order. You are not supposed to sort the list yourself. You only check if the numbers are already arranged from left to right without ever going down.

That rule is called nondecreasing order. It means a number may stay the same as the one before it or become larger, but it should never become smaller. So a list like [12,18,18,21] is good, because it never drops. Repeated values are allowed.

But a list like [24,19,20] is not good, because the second value is smaller than the first one. As soon as that happens, the answer becomes false. A single-item list is always true, and an empty list is also true because there is no pair of values that breaks the rule.

The final answer is just yes or no. You read the list as it is, check each neighboring pair, and decide whether the order stays safe all the way to the end.

Example Input & Output

Example 1
Input
widths = [12,18,18,21]
Output
true
Explanation

The widths never decrease, so the rack can move directly to the gallery floor.

Example 2
Input
widths = [24,19,20]
Output
false
Explanation

The second entry breaks the nondecreasing order, signalling a rearrangement is needed.

Example 3
Input
widths = []
Output
true
Explanation

An empty manifest is trivially ordered, so the crew can proceed when the shipment arrives.

Algorithm Flow

Recommendation Algorithm Flow for Gallery Stock Order Check
Recommendation Algorithm Flow for Gallery Stock Order Check

Solution Approach

The best approach here is a single left-to-right scan that compares each value with the one immediately before it. The array counts as sorted when it never decreases, which means every value must be greater than or equal to the previous value. Because that rule depends only on neighboring pairs, you do not need to sort anything or build a copy of the list.

The important point is that this problem asks whether the list is already in nondecreasing order. It does not ask you to repair the order. That is why an O(n) scan is a better fit than making a sorted copy and comparing the two arrays. Sorting would do extra work and would also hide the simple rule the problem is actually testing.

In JavaScript, the core logic looks like this:

for (let i = 1; i < widths.length; i++) {
  if (widths[i] < widths[i - 1]) {
    return false;
  }
}
return true;

This works because the first backward step is enough to prove the full list is not ordered correctly. For example, in [2, 1, 3], the check fails immediately at index 1 because 1 is smaller than 2. There is no need to keep scanning after that.

This approach also handles edge cases naturally. An empty array returns true because there is no pair that violates the rule. A one-element array also returns true for the same reason. Repeated values are allowed, so arrays like [10, 20, 20, 30] still pass because the order never goes down.

The time complexity is O(n) since each value is checked at most once. The space complexity is O(1) because the solution uses only a loop index and direct comparisons. That makes it both simple and efficient.

So the full strategy is: start from the second element, compare each value with the previous one, return false on the first decrease, and return true if the whole array stays nondecreasing.

Best Answers

java
class Solution {
    public boolean is_gallery_ordered(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i+1]) return false;
        }
        return true;
    }
}