Festival Cabins Sorted Flag
In Festival Cabins Sorted Flag, you are given an array of numbers and need to return true if the array is already in ascending order. If at any point a number is smaller than the one before it, the answer should be false.
This problem does not ask you to sort the array yourself. The real question is whether the list is already sorted. That means you only need to inspect the current order and decide if it ever goes backward.
For example, nums = [1,2,3,4] should return true because every number is greater than or equal to the previous one. But nums = [2,1,3] should return false because the sequence drops from 2 to 1 right away. An empty array also returns true because there is no pair of values that breaks the order.
So the task is to scan the array and determine whether it stays in nondecreasing order from left to right.
Example Input & Output
Each number is greater than or equal to the one before it, so the array is already sorted.
An empty array has no inversion, so it is treated as sorted.
The order breaks immediately because 1 is smaller than the previous value 2.
Algorithm Flow

Solution Approach
The best approach here is a simple one-pass scan through the array. Because you only need to know whether the list is already sorted, there is no reason to actually sort it. Sorting would do extra work and change the data when the problem only asks for a yes-or-no answer.
The key observation is that an array is in ascending order if every element is greater than or equal to the element before it. So instead of thinking about the whole list at once, you can compare each adjacent pair. The first time you find a pair where the current value is smaller than the previous one, you already know the answer must be false.
In JavaScript, the core idea looks like this:
This is enough because the rule for being sorted is local. If every neighboring pair respects the order, then the whole array is sorted. If even one neighboring pair breaks the order, then the whole array fails.
This approach naturally handles edge cases too. If the array is empty, the loop never finds a problem, so returning true is correct. If the array has one element, it is also trivially sorted. Repeated values are fine because the array only needs to be nondecreasing, not strictly increasing. So [5,5,5] should still return true.
Another nice part of this method is efficiency. You only walk through the array once, so the time complexity is O(n). The space complexity is O(1) because you are not building any extra arrays or maps. That makes this better than copying the array and comparing it to a sorted version, which would use more time and more memory.
So the full strategy is: start from index 1, compare each number with the one before it, and stop as soon as the sequence drops. If you reach the end without finding any drop, return true.
Best Answers
class Solution {
public boolean is_festival_sorted(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i+1]) return false;
}
return true;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
