Skip to content

#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.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Hematirukala opened this issue Feb 3, 2025 · 0 comments

Comments

@Hematirukala
Copy link

No description provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant