Array Prefix Maxima and MinimaMedium

Array Prefix Maxima and Minima

MediumPractice library · +20 XParraysprefix sumsrunning max/min

Given an array of N integers, compute two new arrays: one storing the prefix maximums and another storing the prefix minimums. The prefix maximum at index i is the maximum value among elements from index 0 to i (inclusive). Similarly, the prefix minimum at index i is the minimum value among elements from index 0 to i (inclusive). Print both arrays.

Input

The first line contains an integer N. The second line contains N space-separated integers representing the array elements.

Output

The first line should contain N space-separated integers representing the prefix maximums. The second line should contain N space-separated integers representing the prefix minimums.

Constraints

1 <= N <= 200000; -10^9 <= arr[i] <= 10^9

Time limit 1 s (native code; slower languages get more)Memory 256 MB15 hidden tests

Example 1

Input
5
3 1 4 2 5
Output
3 3 4 4 5
3 1 1 1 1

Prefix maximums: [3, 3, 4, 4, 5]. Prefix minimums: [3, 1, 1, 1, 1].

Example 2

Input
3
-1 -5 -2
Output
-1 -1 -1
-1 -5 -5

Prefix maximums: [-1, -1, -1]. Prefix minimums: [-1, -5, -5].

Hints