- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Find Second Smallest of n Elements with Given Complexity Constraint
This is a C++ program to find Second Smallest of n elements with given complexity constraint.
Algorithm
Begin function SecondSmallest() : /* Arguments to this function are: A pointer array a. Number of elements n */ // Body of the function: A variable s1 is declared as to keep track of smallest number. A variable s2 is declared as to keep track of second smallest number. Initialize both s1 and s2 with INT_MAX. Traverse the data array using iteration. If current array element is lesser than current value in s1, then s2 = s1 and s1 = current array element. Else if array element is in between s1 and s2, then s2 = current array element. if(s2==INT_MAX) Print “no second smallest element is present". else Print s2 as second smallest element. End
Example
#include<iostream> #include <climits> //for INT_MAX using namespace std; int SecondSmallest(int *a, int n) { int s1, s2, i,t; //initialize s1 and s2 s1 =INT_MAX; s2=INT_MAX; for(i = 0; i < n; i++) { //If current element is smaller than s1 if(s1 > a[i]) { //update s1 and s2 s2 = s1; s1 = a[i]; } //if a[i] is in between s1 and s2 else if(s2 > a[i] && a[i]!=s1) { //update only s2 s2 = a[i]; } } if(s2==INT_MAX) cout<<"no second smallest element is present"; else cout<<"Second smallest element is:"<<s2; } int main() { int n, i; cout<<"Enter the number of elements: "; cin>>n; int array[n]; for(i = 0; i < n; i++) { cout<<"Enter "<<i+1<< " "<<"element: "; cin>>array[i]; } SecondSmallest(array, n); //call the function return 0; }
Output
Enter the number of elements: 5 Enter 1 element: 1 Enter 2 element: 2 Enter 3 element: 1 Enter 4 element: 3 Enter 5 element: 4 Second smallest element is:2
- Related Articles
- C++ Program to Implement Quick Sort with Given Complexity Constraint
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Find the smallest and second smallest elements in an array in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- C++ program to find Second Smallest Element in a Linked List
- Find the smallest after deleting given elements using C++
- C++ program to find the smallest element among three elements
- C program to find the second largest and smallest numbers in an array
- Maximum sum of smallest and second smallest in an array in C++ Program
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Find the k smallest numbers after deleting given elements in C++
- C++ Program to find the smallest digit in a given number
- C++ Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
- Program to find smallest string with a given numeric value in Python

Advertisements