- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Environmental Science
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- articles and Answers
- Effective Resume Writing
- HR Interview articles
- Computer Glossary
- Who is Who
Find Second largest element in an array using C Programming
Given an array of integers, our task is to write a program that efficiently finds the second largest element present in the array.
Example:
Input: arr[] = {12, 35, 1, 10, 34, 1}
Output: The second largest element is 34.
Explanation: The largest element of the
array is 35 and the second
largest element is 34
Input: arr[] = {10, 5, 10}
Output: The second largest element is 5.
Explanation: The largest element of
the array is 10 and the second
largest element is 5
Input: arr[] = {10, 10, 10}
Output: The second largest does not exist.
Explanation: Largest element of the array
is 10 there is no second largest element
Approach: The idea is to sort the array in descending order and then return the second element which is not equal to the largest element from the sorted array
// C program to find second largest element in an array
#include <stdio.h>
#include <stdlib.h>
// Compare function for qsort
int cmpfunc(const void* a, const void* b)
{
return (*(int*)a - *(int*)b);
}
/* 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
qsort(arr, arr_size, sizeof(int), cmpfunc);
// 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 program to test above function */
int main()
{
int arr[] = { 12, 35, 1, 10, 34, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
print2largest(arr, n);
return 0;
}
Output :
The second largest element is 34
Complexity Analysis:
Time Complexity: O(n log n). Time required to sort the array is O(n log n).
Auxiliary space: O(1). As no extra space is required.
- Related Questions & Answers
- How to print hello world using C Language
- How to Solve Second Order Quadratic Equation Using C Program
- Recursive program to linearly search an element in a given array using C
- How to reverse a given number using C
- How to Implement Stack Operations Using Array using C
- How to Calculate Area of Rectangle using C Program
- How to find sum of two numbers using C Language
- C Program To Print Your Own Name
- Write C Program to Find Largest Element in Array in C Programming
- How to Calculate Area of Equilatral Triangle using C Language
- Write C Program to Merge Two arrays in C Programming
- How to check the type of triangle using C
- How to Find Area of Scalene Triangle using C
- How to Calculate Area of Circle using C Programming
- Find Second largest element in an array using C Programming
- Write C Program to Delete duplicate elements from an array
Advertisements
ads