# Program to find the smallest and second smallest elements in a given array of integers in C++

Example:

Given array {10, 18, 25, 30, 5, 11}

Smallest element: 5

Second smallest element: 10

Algorithm:

1. Initialize an array of size n and populate it with user input.
2. Sort the array in ascending order using the sort() function.
3. Print the first two elements of the sorted array as the smallest and second smallest numbers, respectively.

Code:

``````#include <bits/stdc++.h>

using namespace std;

int main() {
int array[100], i, n;
cout << "Enter number of elements in the array: ";
cin >> n;
cout << "\nEnter array: ";
for (i = 0; i < n; i++) {
cin >> array[i];
}
//sorting the array
sort(array, array + n);
//first two elements are the result
cout << "Smallest number is:  " << array[0] << "\nSecond smallest number is " << array[1] << endl;
return 0;
}

``````

Output

``````Enter number of elements in the array: 5

Enter array: 4 5 1 3 2
Smallest number is:  1
Second smallest number is 2

``````

Time Complexity: O(NlogN) Space Complexity: O(N)