
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
K’th Smallest/Largest Element in Unsorted Array in C++
In this tutorial, we are going to write a program that finds the k-th smallest number in the unsorted array.
Let's see the steps to solve the problem.
- Initialise the array and k.
- Sort the array using sort method.
- Return the value from the array with the index k - 1.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int findKthSmallestNumber(int arr[], int n, int k) { sort(arr, arr + n); return arr[k - 1]; } int main() { int arr[] = { 45, 32, 22, 23, 12 }, n = 5, k = 3; cout << findKthSmallestNumber(arr, n, k) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
23
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- k-th missing element in an unsorted array in C++
- kth smallest/largest in a small range unsorted array in C++
- Python program to find k'th smallest element in a 2D array
- Find k-th smallest element in given n ranges in C++
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- k-th missing element in sorted array in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- K-th Smallest Prime Fraction in C++
- K-th smallest element after removing some integers from natural numbers in C++
- K-th Smallest in Lexicographical Order in C++
- Find K-th Smallest Pair Distance in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Find the largest pair sum in an unsorted array in C++

Advertisements