


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.
The first line will contain the integer ‘n’, the number of elements in the array ‘a’, and the next line will contain the ‘n’ spaced integers in the array elements.
Print two elements, the second largest and the second smallest element, from the array.
You are not required to print anything; it has already been taken care of. Just implement the function.
Approach:
In this approach, we will sort the given array, and the answer will be the second and second last element as the second smallest and second largest elements respectively.
Algorithm:
Approach:
First, we will traverse the array and will find the smallest and largest elements. Then we will travel again and will find the element just greater than the smallest element and the element just smaller than the largest element. The elements found after words are the required answer.
Algorithm: