← Practice problem bank

Jan 9, 2026

The Enchanted Array Modulo Dance

Daily Challenge Archive · Open

mediumarrays

150 pts · 10000 ms · 128 MB · Practice only

Problem

In the mystical realm of Computia, the Modulo Wizards have left behind a magical array encrypted by a 'modulo dance'. This enchanted sequence holds the key to unlocking secrets of immense power, and it's your quest to restore it to its original order. Each element in the array corresponds to a unique index, determined by a mysterious modulo value. Your task is to decipher this array using your knowledge of modular arithmetic. To begin, you'll receive an array of integers from standard input. Your mission is to rearrange these integers to their original order by leveraging the given modulo values. The challenge lies in understanding how each element's position is determined by the modulo operation. Steps to solve: 1. Identify the modulo value associated with each element to ascertain its original index. 2. Rearrange the elements based on these indices to restore the array to its intended order. 3. Consider edge cases, such as repeated numbers or negative values, which may affect the indexing.

Examples
Given an array [3, 1, 4] with modulo values [0, 1, 2], rearrange it to [1, 4, 3].
Hints
  • Consider using a data structure like a hash map to store the original indices for quick access.
  • Think about how the modulo operation can map indices back to their original positions.
  • Pay attention to edge cases, such as when all numbers are the same or when negative indexing might occur.
  • For optimization, ensure that your solution can handle large arrays efficiently.

Input format

The first line contains an integer n (1 <= n <= 1000), the size of the array. The second line contains n integers a_i (0 <= a_i <= 1000), representing the elements of the array.

Output format

Output the array in its original order after undoing the modulo shuffle.

Constraints

1 <= n <= 1000, 0 <= a_i <= 1000

Sample input

5
5 9 2 7 4

Sample output

2 4 5 7 9

Sign in to submit solutions

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

Sign in