← Practice problem bank

Jan 5, 2026

The Mystic Array Shuffle

Daily Challenge Archive · Open

hardsorting

200 pts · 10000 ms · 128 MB · Practice only

Problem

In the mythical land of Wisdomia, a legendary array of numbers holds secrets that only the skilled can decipher. Your task is to take this array of integers, sort it in non-decreasing order, and then uncover the longest subsequence consisting of unique elements. Here's how you can achieve this: First, select and implement an efficient sorting algorithm to arrange the array in ascending order. Then, traverse the sorted array to identify the longest subsequence where each number appears only once. For instance, given the array [4, 3, 1, 2, 4, 3], after sorting, you get [1, 2, 3, 3, 4, 4], and the longest subsequence of unique elements is [1, 2, 3, 4]. Consider edge cases like arrays where all elements are the same or arrays that are already sorted.

Hints
Start by considering sorting algorithms like quicksort or mergesort for efficiency. Think about using a data structure like a hash set to help track unique elements. Make sure to handle arrays with various lengths, including empty arrays. Optimization tip: After sorting, you can traverse the array once to find the longest unique subsequence, achieving an O(n log n) complexity for the overall solution.

Input format

The first line contains an integer n (1 <= n <= 1000), the number of elements in the array. The second line contains n integers, each an element of the array.

Output format

Two lines of output: the first line contains the sorted array of integers, and the second line contains the longest subsequence of unique integers.

Constraints

1 <= n <= 1000; -10^9 <= each element <= 10^9

Sample input

6
4 6 2 6 3 1

Sample output

1 2 3 4 6 6
4 6 2 3 1

Sign in to submit solutions

Run your code against all test cases and get instant feedback.

Sign in