Skip to main content

Featured

Merge Sort in C++

  Merge Sort in C++ Introduction Merge Sort is a popular sorting algorithm that follows the Divide and Conquer approach. It divides an array into smaller subarrays, recursively sorts those subarrays, and finally merges the sorted subarrays to produce a completely sorted array. In this tutorial, we will learn how to implement Merge Sort in C++ . The program divides the array into two halves using the mid index, recursively sorts both halves, and then combines them using the merge() function. Merge Sort has a time complexity of O(n log n) in the best, average, and worst cases. Table of Contents Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Keywords Conclusion Algorithm Start the program. Read the size of the array. Read the array elements from the user. Call the mer...

C Program to Find the Third Smallest Element Without Sorting

๐Ÿ” C Program to Find the Third Smallest Element Without Sorting

#include <stdio.h>

int main()
{
    int num, a[100];
    printf("Enter the size of the array:\n");
    scanf("%d", &num);

    if (num < 3)
    {
        printf("Invalid! Need at least 3 elements to find the third smallest element.\n");
        return 1;
    }

    printf("Enter %d elements:\n", num);
    for (int i = 0; i < num; i++)
    {
        scanf("%d", &a[i]);
    }

    int first, second, third;

    // Initialize first, second, third with the smallest 3
    if (a[0] <= a[1] && a[0] <= a[2])
    {
        first = a[0];
        if (a[1] <= a[2])
        {
            second = a[1];
            third = a[2];
        }
        else
        {
            second = a[2];
            third = a[1];
        }
    }
    else if (a[1] <= a[0] && a[1] <= a[2])
    {
        first = a[1];
        if (a[0] <= a[2])
        {
            second = a[0];
            third = a[2];
        }
        else
        {
            second = a[2];
            third = a[0];
        }
    }
    else
    {
        first = a[2];
        if (a[0] <= a[1])
        {
            second = a[0];
            third = a[1];
        }
        else
        {
            second = a[1];
            third = a[0];
        }
    }

    for (int i = 3; i < num; i++)
    {
        if (a[i] < first)
        {
            third = second;
            second = first;
            first = a[i];
        }
        else if (a[i] < second && a[i] != first)
        {
            third = second;
            second = a[i];
        }
        else if (a[i] < third && a[i] != second && a[i] != first)
        {
            third = a[i];
        }
    }

    if (first == second || second == third)
    {
        printf("There is no distinct third smallest element.\n");
    }
    else
    {
        printf("The third smallest element is: %d\n", third);
    }

    return 0;
}
  

๐Ÿ“ Explanation:

This program identifies the third smallest distinct element without sorting the array. It initializes the smallest three manually and adjusts their values during iteration while avoiding duplicates.

๐Ÿ’ก Sample Output:

Enter the size of the array:
7
Enter 7 elements:
5 1 9 2 4 1 6
The third smallest element is: 4
  

๐Ÿ” Keywords:

C program to find third minimum value, no sorting, array interview questions in C, C coding logic, array manipulation

Comments

Popular Posts

๐ŸŒ™