From 710d7bfbf61d57ae84609f27a2964309b960f7e5 Mon Sep 17 00:00:00 2001 From: Prashant Bhapkar Date: Mon, 29 Aug 2022 20:39:31 +0530 Subject: [PATCH 1/2] Find Second largest element in the array --- algorithms/C/arrays/secondLargestElement.c | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 algorithms/C/arrays/secondLargestElement.c diff --git a/algorithms/C/arrays/secondLargestElement.c b/algorithms/C/arrays/secondLargestElement.c new file mode 100644 index 00000000..04263d26 --- /dev/null +++ b/algorithms/C/arrays/secondLargestElement.c @@ -0,0 +1,30 @@ +//Second largest element in the array + +#include +#include + +int second(int arr[],int size) +{ + int max1=arr[0],max2; + for(int i=0;imax1) //Find largest element in the array + { + max2=max1; + max1=arr[i]; + } + else if (arr[i]>max2 && arr[i] Date: Wed, 31 Aug 2022 16:30:53 +0530 Subject: [PATCH 2/2] Added running time of algorithm and update readme.md file --- algorithms/C/README.md | 1 + algorithms/C/arrays/secondLargestElement.c | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/algorithms/C/README.md b/algorithms/C/README.md index 9e241b51..50a7eb55 100644 --- a/algorithms/C/README.md +++ b/algorithms/C/README.md @@ -7,6 +7,7 @@ - [Reverse an array](arrays/reverse-array.c) - [Maximum difference](arrays/maximum-difference.c) - [Largest Element](arrays/largestElement.c) +- [Second Largest Element](arrays/secondLargestElement.c) - [Sieve of Eratosthenes](arrays/sieve-of-eratosthenes.c) ## Bit Manipulation diff --git a/algorithms/C/arrays/secondLargestElement.c b/algorithms/C/arrays/secondLargestElement.c index 04263d26..34b81d12 100644 --- a/algorithms/C/arrays/secondLargestElement.c +++ b/algorithms/C/arrays/secondLargestElement.c @@ -28,3 +28,11 @@ int main() second(arr,size); return 0; } + +/* +----------------Sample Output---------------- +> 88 45 + +Time Compexity: O(n) +Space compexity: O(1) +*/