Merge pull request #824 from Prashant-Bhapkar/second-largest-element
chore(C): add find second largest element in arraypull/833/head
commit
170a706b1b
|
@ -7,6 +7,7 @@
|
||||||
- [Reverse an array](arrays/reverse-array.c)
|
- [Reverse an array](arrays/reverse-array.c)
|
||||||
- [Maximum difference](arrays/maximum-difference.c)
|
- [Maximum difference](arrays/maximum-difference.c)
|
||||||
- [Largest Element](arrays/largestElement.c)
|
- [Largest Element](arrays/largestElement.c)
|
||||||
|
- [Second Largest Element](arrays/secondLargestElement.c)
|
||||||
- [Sieve of Eratosthenes](arrays/sieve-of-eratosthenes.c)
|
- [Sieve of Eratosthenes](arrays/sieve-of-eratosthenes.c)
|
||||||
|
|
||||||
## Bit Manipulation
|
## Bit Manipulation
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
//Second largest element in the array
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
int second(int arr[],int size)
|
||||||
|
{
|
||||||
|
int max1=arr[0],max2;
|
||||||
|
for(int i=0;i<size;i++)
|
||||||
|
{
|
||||||
|
if(arr[i]>max1) //Find largest element in the array
|
||||||
|
{
|
||||||
|
max2=max1;
|
||||||
|
max1=arr[i];
|
||||||
|
}
|
||||||
|
else if (arr[i]>max2 && arr[i]<max1) // Find second largest element in the array
|
||||||
|
{
|
||||||
|
max2=arr[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("%d %d",max1,max2); // Print both first and second largest element in the array
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int arr[]={2,5,1,12,18,88,23,45,4}; // Initialize array
|
||||||
|
int size=sizeof(arr)/sizeof(int); // Calculate array size
|
||||||
|
second(arr,size);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
----------------Sample Output----------------
|
||||||
|
> 88 45
|
||||||
|
|
||||||
|
Time Compexity: O(n)
|
||||||
|
Space compexity: O(1)
|
||||||
|
*/
|
Loading…
Reference in New Issue