Code Logo

Festival Booth Priority Sort

Published at05 Jan 2026
Medium 3 views
Like21

In Festival Booth Priority Sort, you are given an array of integers and need to return those same integers sorted from smallest to largest.

The tested behavior here is a normal numeric sort. You are not inserting new values by index, merging a second list, or simulating arrivals. The task is simply to reorder the numbers that are already in the input array. Every value should still appear in the output, including duplicates.

For example, nums = [14,5,12,5] should return [5,5,12,14]. Both copies of 5 are kept. If nums = [0,-3,7], the correct output is [-3,0,7] because -3 is the smallest number. If the array is empty, the result is an empty array.

So the real task is to sort the given numbers in ascending numeric order and return the sorted array.

Example Input & Output

Example 1
Input
nums = [10,-5,10]
Output
[-5,10,10]
Explanation

The smallest value moves to the front and duplicate 10 values are both preserved.

Example 2
Input
nums = [14,5,12,5]
Output
[5,5,12,14]
Explanation

The values are reordered from smallest to largest while keeping both copies of 5.

Example 3
Input
nums = [0,-3,7]
Output
[-3,0,7]
Explanation

Negative numbers come first when they are smaller than the rest.

Algorithm Flow

Recommendation Algorithm Flow for Festival Booth Priority Sort
Recommendation Algorithm Flow for Festival Booth Priority Sort

Solution Approach

The right solution is a normal ascending numeric sort. Even though some older statement text for this export talked about booths and arrivals, the actual judge expects a single list of integers to be returned in sorted order. So the best approach is to follow the tested behavior directly and keep the implementation focused on numeric sorting.

The key implementation detail is the same one that appears in many easy sorting problems: make sure the comparison is numeric, not textual. In JavaScript, for example, using sort() without a comparator can lead to string-style ordering, which would break inputs with multi-digit numbers. The fix is simple: use a comparator like (a, b) => a - b.

A clear version looks like this:

const result = [...nums].sort((a, b) => a - b);
return result;

The copied array is optional, but it helps if you want to avoid mutating the original input. The comparator ensures that smaller values move to the front and larger values move to the back. If two values are equal, both remain in the array as separate items, which is important for cases like [14, 5, 12, 5].

This method handles all of the judge cases naturally. Empty arrays stay empty. A one-element array returns that same value. Negative numbers move before zero and positive numbers when they are smaller. Repeated values are preserved because sorting only changes position, not membership.

Using the built-in sort also keeps the code much more readable than writing a manual nested-loop sort for a simple challenge like this. Since the problem does not ask you to implement merge sort or quicksort by hand, a well-configured built-in sort is the most practical solution.

The time complexity is O(n log n), which is standard for comparison sorting. Extra space depends on whether you sort in place or work on a copied array first. Either version expresses the same algorithmic idea clearly.

So the full strategy is: treat the input as one plain integer array, apply an ascending numeric sort with an explicit comparator, and return the sorted result with all original values still present.

Best Answers

java
import java.util.*;
class Solution {
    public int[] sort_priorities(int[] nums) {
        int[] res = nums.clone();
        Arrays.sort(res);
        return res;
    }
}