How to Find the Third Smallest Number in an Array in C++? Last Updated : 13 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array is the collection of similar data elements that are stored in the contiguous memory location and we can access these elements directly by their index value. In this article, we will learn how to find the third smallest element in an array in C++. If there is no third smallest element, return -1. Example Input: myArray = {1, 45, 54, 71, 76, 17} Output: 45 Explanation: The third smallest element in the array is 45, hence the output is 45. Input: myArray = {10, 20, 10, 20} Output: -1 Explanation: The array has less than three distinct elements, hence the output is -1.Find the Third Smallest Number in an Array in C++ To find the third smallest number in an array in C++, we can use the priority_queue container in the following way: ApproachFirst, create the priority_queue container.Then, start pushing elements into the priority queue.If the size of the priority queue is greater than 3, pop the top element.Keep doing it till all the array elements are pushed.Now, if the priority_queue contains three elements, the top element is the third smallest element.If the priority_queue does not contain three elements, return -1 as there is no third smallest element.C++ Program to Find the Third Smallest Number in an Array C++ // C++ program to find the third smallest number in an array #include <iostream> #include <queue> #include <vector> using namespace std; int main() { // Initialize a vector vector<int> vec = { 7, 4, 6, 3, 9, 1 }; // Create a max heap priority_queue<int> maxHeap; for (int num : vec) { maxHeap.push(num); if (maxHeap.size() > 3) { maxHeap.pop(); } } if (maxHeap.size() == 3) { cout << "The third smallest number is: " << maxHeap.top() << "\n"; } else { cout << "The array does not have a third smallest " "number.\n"; } return 0; } OutputThe third smallest number is: 4 Time Complexity: O(n), where n is the number of elements in the array.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Find the Third Smallest Number in an Array in C++? abhishek9202 Follow Improve Article Tags : C++ Programs C++ cpp-array cpp-priority-queue CPP Examples +1 More Practice Tags : CPP Similar Reads How to Find the Smallest Number in an Array in C++? In C++, arrays are the data types that store the collection of the elements of other data types such as int, float, etc. In this article, we will learn how to find the smallest number in an array using C++. For Example,Input: myVector = {10, 3, 10, 7, 1, 5, 4} Output: Smallest Number = 1Find the Sma 2 min read How to Find the Range of Numbers in an Array in C++? The range of numbers in an array means the difference between the maximum value and the minimum value in the given array. In this article, we will learn how to find the range of numbers in an array in C++. For Example, Input: myArray = {5,10,15,30,25}; Output: Range: 25Range Within an Array in C++To 2 min read How to Find the Second Smallest Element in an Array in C++? In C++, arrays are data structures that store the collection of data elements of the same type in contiguous memory locations. In this article, we will learn how to find the second smallest element in an array in C++. Example:Input:myArray = {10, 5, 8, 2, 7, 3, 15};Output:The second smallest element 3 min read How to Find the Mode in a Sorted Array in C++? The mode of the given numbers can be defined as the value that occurs the most in the given dataset or the value with the highest frequency. In this article, we will discuss how to calculate the mode of the numbers in a sorted array in C++. Example: Input: myVector = {1, 2, 2, 3, 3, 3, 4, 4, 5} Outp 3 min read How to Find Largest Number in an Array in C++? In C++, arrays are used to store the collection of similar elements to be stored in adjacent memory locations. They can store data of any type such as int, char, float, etc. In this article, we will learn how to find the largest number in an array in C++. For Example,Input: myVector = {1, 3, 10, 7, 2 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read How to Find the Length of an Array in C++? In C++, the length of an array is defined as the total number of elements present in the array. In this article, we will learn how to find the length of an array in C++.The simplest way to find the length of an array is by using the sizeof operator. First calculate the size of the array in bytes and 2 min read How to Find the Index of an Element in an Array in C++? Given an array of n elements, the task is to find the index of a specific element in C++.ExamplesInput: arr[] = {11, 13, 9, 21, 51, 1000}, val = 9Output: 2Explanation: As the value 9 is present at index 2.Input: arr[] = {5, 8, 12, 9, 11, 32}, val = 11Output: 4Explanation: As the value 11 is present 3 min read How to Find the Median of Array in C++? In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th 2 min read How to Find the Range of Values in a 2D Array in C++? In C++, 2D arrays are also known as a matrix, and the range of numbers in a 2D array means the maximum and the minimum value in which the numbers lie in the given 2D array. In this article, we will learn how to find the range of numbers in a 2D array in C++. For Example, Input: my2DArray= {{80, 90, 2 min read Like