We can find the largest and second largest number of an integers array using the c language. This question is also important and asks by the interviewer in an interview.
Let’s take an example, suppose there are an integer array iaData of 5 integers.
int iaData[5] = {3, 5, 123, 6, 1};
Here the largest number is 123 and the second largest number is 6.
A simple way to find the largest and second-largest number is that sort the array in descending order and pick its first and second elements. Its first element will be the largest number and the second number will be the second-largest number. The time complexity of this solution is O(n log n).
#include <stdio.h> // Size of array #define SIZE_ARRAY(x) sizeof(x)/sizeof(x[0]); int main() { int iaData[5] = {3,5,123,6,1}; const int ArraySize = SIZE_ARRAY(iaData); int iLoop1 =0, iLoop2 =0; for(iLoop1 = 0; iLoop1 < ArraySize; iLoop1++) { for(iLoop2 = iLoop1+1; iLoop2 < ArraySize ; iLoop2++) { if(iaData[iLoop1] < iaData[iLoop2]) { iaData[iLoop1] ^= iaData[iLoop2]; iaData[iLoop2] ^= iaData[iLoop1]; iaData[iLoop1] ^= iaData[iLoop2]; } } } //Sorted array printf("\n\nSorted Array: "); for(iLoop1 = 0; iLoop1 < ArraySize; iLoop1++) { printf("%d ",iaData[iLoop1]); } //First element of sorted array printf("\n\nBigest element = %d\n",iaData[0]); //Second element of sorted array printf("\n\nSecond Biggest element = %d\n\n\n",iaData[1]); return 0; }
OutPut:
The above method is not better because it scans the array twice. There is also another way to find the largest and second-largest number in a single scan of the array. The time complexity of this solution is O(n).
Here is an algorithm to find the two largest and second-largest numbers in one traversal.
The algorithm to find largest and second largest number:
- Create two-variable iFirstLargest and iSecondLargest.
- Initialize both variables as INT_MIN
iFirstLargest = INT_MIN
iSecondLargest = INT_MIN - Loop through all the elements and perform the below task
1). If the current element is bigger than iFirstLargest, then update iFirstLargest and iSecondLargest.
2). Else if the current element is bigger than iSecondLargest then update iSecondLargest.
#include <stdio.h> #include <limits.h> //Size of array #define SIZE_ARRAY(x) sizeof(x)/sizeof(x[0]) void FindTwoLargestNumber(int *piListOfData, int iSizeOfArray) { int iLoop1= 0,iLoop2 =0; int iFirstLargest = INT_MIN; int iSecondLargest = INT_MIN; while(iLoop1 < iSizeOfArray) { if(piListOfData[iLoop1] > iFirstLargest) { iSecondLargest = iFirstLargest; iFirstLargest = piListOfData[iLoop1]; } else if((piListOfData[iLoop1] > iSecondLargest) && (piListOfData[iLoop1] !=iFirstLargest )) { iSecondLargest = piListOfData[iLoop1]; } iLoop1++; } printf("First largest Numbers = %d\nSecond largest Number = %d\n ",iFirstLargest,iSecondLargest); } int main() { int iaData[5] = {3,5,123,6,1}; int ArraySize = SIZE_ARRAY(iaData); //calling function to find FindTwoLargestNumber(iaData,ArraySize); return 0; }
OutPut:
Recommended Articles for you:
- How to find smallest and second smallest element in an array
- How to create a dynamic array in C?
- How to access 2d array in C?
- Function pointer in c, a detailed guide
- How to use the structure of function pointer in c language?
- Function pointer in structure.
- How to pass an array as a parameter?
- How to use C if-else condition?
- How to use for loop in C?
- Dangling, Void, Null and Wild Pointers
- When and how to use array in C?
- File handling in C, In a few hours.