Find the smallest and second smallest elements in an array


Find the smallest and second smallest elements in an array
// C program to find smallest and second smallest elements
#include <stdio.h>
#include <limits.h> /* For INT_MAX */
void print2Smallest(int arr[], int arr_size)
{
    int i, first, second;
    /* There should be atleast two elements */
    if (arr_size < 2)
    {
        printf(" Invalid Input ");
        return;
    }
    first = second = INT_MAX;
    for (i = 0; i < arr_size ; i ++)
    {
        /* If current element is smaller than first
           then update both first and second */
        if (arr[i] < first)
        {
            second = first;
            first = arr[i];
        }
        /* If arr[i] is in between first and second
           then update second  */
        else if (arr[i] < second && arr[i] != first)
            second = arr[i];
    }
    if (second == INT_MAX)
        printf("There is no second smallest element\n");
    else
        printf("The smallest element is %d and second "
               "Smallest element is %d\n", first, second);
}
/* Driver program to test above function */
int main()
{
    int arr[] = {12, 13, 1, 10, 34, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    print2Smallest(arr, n);
    return 0;
}
Output :
The smallest element is 1 and second Smallest element is 10
Example:
Input:  arr[] = {12, 13, 1, 10, 34, 1}
Output: The smallest element is 1 and
        second Smallest element is 10

Find the smallest and second smallest elements in an array Find the smallest and second smallest elements in an array Reviewed by Unknown on August 25, 2018 Rating: 5

No comments:

If you have any doubt or query ,comment below:

Programming copyright © 2018-19. Powered by Blogger.