

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
kth smallest/largest in a small range 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.
Let's see the code.
Example
#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[] = { 3, 5, 23, 4, 15, 16, 87, 99 }, k = 5; cout << findKthSmallestNumber(arr, 7, k) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
16
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Questions & Answers
- K’th Smallest/Largest Element in Unsorted Array in C++
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- Kth Largest Element 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++
- Kth Largest Element in an Array in Python
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Kth Smallest Number in Multiplication Table in C++
- Smallest Range II in C++
- Kth smallest element after every insertion in C++
- Kth Smallest Element in a BST in Python
- C++ Program to Find kth Largest Element in a Sequence
- Kth Largest Element in a Stream in Python
- Find kth smallest number in range [1, n] when all the odd numbers are deleted in C++
Advertisements