C program to find two largest elements in a one dimensional array
Write a C program to find two largest elements in a one dimensional array.
Example: Type1: (all the elements are not same & no of element is more than two)
Input:
Array size: 4
Elements: 32 54 -6 43
Output:
54 43
Example: Type2: (second maximum doesn’t exist as size of array < 2)
Input:
Array size : 1
Elements: 4
Output:
4
Example: Type3: (all elements are same, thus only one largest element)
Input:
Array size : 4
Elements: 12 12 12 12
Output:
12
Algorithm:
a. For(int i=0;i<n;i++) if array[i]>max then update max to array[i]&sec_max to previous max value else if array[i] is greater than sec_max but less than max then update sec_max to array value do nothing to max End for loopOnly one largest value exists, print it
Else
Print max & sec_max
C implementation to find two largest elements in a one dimensional array
Output
need an explanation for this answer? contact us directly to get an explanation for this answer