Please help to understand the logic of
Please help me to understand the logic
@rssrivastavarohan This is the simplest code to understand.
void thirdLargest(int arr[], int arr_size)
{
/* There should be atleast three elements */
if (arr_size < 3)
{
printf(" Invalid Input ");
return;
}
// Find first largest element
int first = arr[0];
for (int i = 1; i < arr_size ; i++)
if (arr[i] > first)
first = arr[i];
// Find second largest element
int second = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > second && arr[i] < first)
second = arr[i];
// Find third largest element
int third = INT_MIN;
for (int i = 0; i < arr_size ; i++)
if (arr[i] > third && arr[i] < second)
third = arr[i];
printf("The third Largest element is %d\n", third);
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.