You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include <stdio.h>
#include <limits.h>
long long second_larg_ele(int size, long long arr[]) {
if (size < 2) {
return -1; // Return -1 if there is no second largest element
}
long long largest = LLONG_MIN;
long long second_largest = LLONG_MIN;
for (int i = 0; i < size; i++) {
if (arr[i] > largest) {
second_largest = largest;
largest = arr[i];
} else if (arr[i] > second_largest && arr[i] < largest) {
second_largest = arr[i];
}
}
return (second_largest == LLONG_MIN) ? -1 : second_largest;
}
// Example usage
int main() {
int size1 = 8;
long long arr1[] = {20, 30, 50, 60, 70, 10, 11, 25};
printf("%lld\n", second_larg_ele(size1, arr1)); // Output: 60
int size2 = 5;
long long arr2[] = {1, 2, 3, 4, 5};
printf("%lld\n", second_larg_ele(size2, arr2)); // Output: 4
return 0;
}
#464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
No description provided.
The text was updated successfully, but these errors were encountered: