New update is available. Click here to update.

Topics

You have been given an array * ‘a’* of

Find the second largest and second smallest element from the array.

Return the two elements (second largest and second smallest) as another array of size 2.

```
Input: ‘n’ = 5, ‘a’ = [1, 2, 3, 4, 5]
Output: [4, 2]
The second largest element after 5 is 4, and the second smallest element after 1 is 2.
```

Detailed explanation

```
4
3 4 5 2
```

```
4 3
```

```
The second largest element after 5 is 4 only, and the second smallest element after 2 is 3.
```

```
5
4 5 3 6 7
```

```
6 4
```

```
O(n), Where ‘n’ is the size of an input array ‘a’.
```

```
2 ≤ 'n' ≤ 10^5
0 ≤ 'a'[i] ≤ 10^9
Time limit: 1 sec
```