#C4871. Replace Elements with Greatest Element on Right

    ID: 48457 Type: Default 1000ms 256MiB

Replace Elements with Greatest Element on Right

Replace Elements with Greatest Element on Right

Given an array of integers \(a = [a_0, a_1, \dots, a_{n-1}]\), transform the array such that every element \(a_i\) is replaced by the greatest element among the elements to its right (i.e. from \(a_{i+1}\) to \(a_{n-1}\)). The last element of the array should be replaced with \(-1\).

For example, given the array [16, 17, 4, 3, 5, 2], the output is [17, 5, 5, 5, 2, -1] because:

  • For index 0, the elements on the right are [17, 4, 3, 5, 2] and the maximum is 17.
  • For index 1, the elements on the right are [4, 3, 5, 2] and the maximum is 5.
  • And so on, with the last element replaced by -1.
Your task is to implement this transformation. The solution must read input from stdin and write the output to stdout as described in the input/output sections below.

inputFormat

The input is given in the following format:

  • The first line contains a single integer \(n\) (the number of elements in the array).
  • The second line contains \(n\) space-separated integers representing the elements of the array.

outputFormat

Output a single line with \(n\) space-separated integers representing the modified array where each element is replaced by the largest element to its right and the last element is \(-1\).

## sample
6
16 17 4 3 5 2
17 5 5 5 2 -1