How to find the kth smallest number in cinstant space
C++
#include <bits/stdc++.h>
using namespace std;
// Function to return the kth smallest
// element from the array
int kthSmallest(int* arr, int k, int n)
{
// Minimum and maximum element from the array
int low = *min_element(arr, arr + n);
int high = *max_element(arr, arr + n);
// Modified binary search
while (low <= high) {
int mid = low + (high - low) / 2;
// To store the count of elements from the array
// which are less than mid and
// the elements which are equal to mid
int countless = 0, countequal = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] < mid)
++countless;
else if (arr[i] == mid) // Equal elements were the real trouble
++countequal;
}
// If mid is the kth smallest
if (countless < k
&& (countless + countequal) >= k) {
return mid;
}
// If the required element is less than mid
else if (countless >= k) {
high = mid - 1;
}
// If the required element is greater than mid
else if (countless < k
&& countless + countequal < k) {
low = mid + 1;
}
}
}
// Driver code
int main()
{
int arr[] = { 7, 10, 4, 3, 20, 15 };
int n = sizeof(arr) / sizeof(int);
int k = 3;
cout << kthSmallest(arr, k, n);
return 0;
}
Also in C++:
- Title
- dfs in c++
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- how to output text in c++
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- min heap priority queue c++
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- ceil c++;
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- qt graphics scene map cursor position
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- cout does not name a type
- Category
- C++
- Title
- set of vectors c++
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- set c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- runtime array size c++
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- sum of two numbers c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- getting a random letter in c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- how to make a heap using stl in c++
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- c++ vector lower_bound index
- Category
- C++
- Title
- c++ convert int to cstring
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- fast input output in c++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- substitution failure is not an error
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- powers of 2 in cpp
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- c++ string
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- c++ set add element
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- matrix transpose tiling
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- mingw32/bin/ld.exe: C:\Users\mfrom\AppData\Local\Temp\ccSKcRks.o:PizzaPi.cpp:(.text$_ZN5PizzaC2Ev[__ZN5PizzaC2Ev]+0xa): undefined reference to `vtable for Pizza' collect2.exe: error: ld returned 1 exit status
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- c++ string contains
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- Find the minimum difference between pairs in a simple path of tree C++
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- how to find length of string in c++
- Category
- C++
- Title
- correct sequence of compilation process in c++
- Category
- C++
- Title
- roscpp publish int32
- Category
- C++