#K15221. Find Duplicates in an Array

    ID: 24309 Type: Default 1000ms 256MiB

Find Duplicates in an Array

Find Duplicates in an Array

Given an array of integers, your task is to find all the elements that appear more than once. You must implement an algorithm that runs in (O(n)) time and uses (O(1)) extra space by leveraging an in-place marking strategy. Specifically, for each element (x) in the array (with (1 \leq x \leq n)), mark the element at index (x-1). If the element at that index is already negative, then (x) is a duplicate. Finally, output all duplicate numbers in ascending order.

Constraints:

  • \(1 \leq n \leq 10^5\)
  • Each element in the array satisfies \(1 \leq nums[i] \leq n\).
If there are no duplicates, output nothing.

inputFormat

The input is given via standard input (stdin).\nThe first line contains an integer (n), the number of elements in the array.\nThe second line contains (n) space-separated integers representing the array.

outputFormat

Print the duplicate elements in ascending order separated by a single space on standard output (stdout). If there are no duplicates, print nothing.## sample

1
1