Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Last Updated: 13 Nov, 2020

Easy

```
a) Duplicate elements may be present.
b) If no such element is present return -1.
```

```
Input: Given a sequence of five numbers 2, 4, 5, 6, 8.
Output: 6
Explanation:
In the given sequence of numbers, number 8 is the largest element, followed by number 6 which is the second-largest element. Hence we return number 6 which is the second-largest element in the sequence.
```

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains an integer ‘N’ denoting the number of elements in the array.
The second line of each test case contains ‘N’ space-separated integers denoting the elements in the array.
```

```
For each test case, print a single line containing a single integer denoting the second largest element in the array.
The output of each test case will be printed in a separate line.
```

```
You are not required to print the expected output; it has already been taken care of, Just implement the function.
```

```
1 <= T <= 100
1 <= N <= 5000
-10 ^ 9 <= 'SIZE' <= 10 ^ 9
Where ‘T’ is the total number of test cases, ‘N’ denotes the number of elements in the array and ‘SIZE’ denotes the range of the elements in the array.
Time limit: 1 sec.
```

The idea is to sort the array in decreasing order and return the second largest element in the array.

- Sort the array in decreasing order.
- We can create a function to sort the elements using a sorting algorithm such as quicksort or use inbuilt sorting functions.
- Traverse from index 1(0-based indexing) because the element at index 0 will clearly be the first largest and check whether duplicates of the larger elements exist or not. Finally, return the second element which is not equal to the largest element from the sorted array.
- If no such element is found, return -1.

The approach is to traverse the array twice. In the first traversal find the maximum element of the array. In the second traversal find the greatest element less than the element obtained in the first traversal.

- Traverse the array to find the largest element and store it in a variable ‘MAXIMUM’.
- Initialize variable ‘SECONDLARGEST’ to ‘INT_MIN’.
- Traverse the array again.
- If the element is not equal to the first largest element ‘maximum’, compare it with ‘SECONDLARGEST’ variable and store the maximum of both the elements in the variable ‘SECONDLARGEST’.
- Continue step 4 till the whole array is traversed and all elements are checked.
- Return ‘SECONDLARGEST’, which is the required answer.
- If ‘SECONDLARGEST’ is still equal to ‘INT_MIN’, that is, no second largest element exists, return -1.

The idea is to traverse the array once, check every element for the larger element, and store the previous largest element in a variable which will be our answer.

- Initialize two variables ‘FIRSTLARGEST’ and 'SECONDLARGEST' to ‘INT_MIN’.
- Start traversing the array.
- If the current element in the array say ‘ARR['I']’ is greater than first.
- Then update ‘FIRSTLARGEST’ and 'SECONDLARGEST' as, 'SECONDLARGEST'='FIRSTLARGEST' and 'FIRSTLARGEST' = ARR['I'].

- If the current element is in between first and second, then update 'SECONDLARGEST' to store the value of the current variable as 'SECONDLARGEST' = ARR['I'].

- If the current element in the array say ‘ARR['I']’ is greater than first.
- Return the value stored in variable 'SECONDLARGEST'.
- If 'SECONDLARGEST' is still equal to ‘INT_MIN’, that is, no second largest element exists, return -1.

Similar problems

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Merge Two Sorted Arrays Without Extra Space

Moderate

Posted: 19 Nov, 2022

Ninja And The Strictly Increasing Array

Moderate

Posted: 27 Nov, 2022

Negative To The End

Easy

Posted: 16 Dec, 2022

Sort 0s, 1s, 2s

Easy

Posted: 24 Dec, 2022

Find Duplicate in Array

Easy

Posted: 5 Jun, 2023