#C10084. Reverse Subarray Between Maximum Elements

    ID: 39250 Type: Default 1000ms 256MiB

Reverse Subarray Between Maximum Elements

Reverse Subarray Between Maximum Elements

Given a list of integers, your task is to reverse the order of the elements that lie strictly between the first and last occurrence of the maximum element in the list. The maximum element itself and any elements outside this range remain unchanged.

If the maximum element occurs only once or if there are no elements between its first and last occurrences, return the original list.

Note: Let \(i\) be the index of the first occurrence and \(j\) be the index of the last occurrence of the maximum element. You need to reverse the subarray from index \(i+1\) to \(j-1\) (if \(j-i > 1\)).

Examples:

  • For the input list [1, 3, 2, 7, 5, 7, 4], the maximum element is 7 which first appears at index 3 and last appears at index 5. Reversing the subarray [5] gives the same list: [1, 3, 2, 7, 5, 7, 4].
  • For the input list [1, 3, 7, 2, 7, 5, 4, 7], the maximum element is 7 with first occurrence at index 2 and last occurrence at index 7. Reversing the subarray [2, 7, 5, 4] results in [1, 3, 7, 4, 5, 7, 2, 7].

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains an integer \(N\) representing the number of elements in the list.
  2. The second line contains \(N\) space-separated integers.

Note: If \(N = 0\), the list is empty.

outputFormat

The output should be printed to standard output (stdout) as a single line of space-separated integers representing the list after the required reversal operation.

## sample
7
1 3 2 7 5 7 4
1 3 2 7 5 7 4