-
Notifications
You must be signed in to change notification settings - Fork 124
/
.cpp
36 lines (33 loc) · 982 Bytes
/
.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Cpp program to find second largest element in an array
#include <bits/stdc++.h>
using namespace std;
/* Function to print the second largest elements */
void print2largest(int arr[], int arr_size)
{
int i, first, second;
/* There should be atleast two elements */
if (arr_size < 2) {
printf(" Invalid Input ");
return;
}
// sort the array
sort(arr, arr + arr_size);
// start from second last element as the largest element
// is at last
for (i = arr_size - 2; i >= 0; i--) {
// if the element is not equal to largest element
if (arr[i] != arr[arr_size - 1]) {
printf("The second largest element is %d\n",arr[i]);
return;
}
}
printf("There is no second largest element\n");
}
/* Driver code*/
int main()
{
int arr[] = { 12, 35, 1, 10, 34, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
print2largest(arr, n);
return 0;
}