Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Arrays/cpp/a.exe
Binary file not shown.
30 changes: 30 additions & 0 deletions Arrays/cpp/pth_smallest_element_in_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// P’th Smallest/Largest Element in Unsorted Array


/* Given an array and a number p where p is smaller than size of array, we need to find the p’th smallest element in the given array.
It is given that all array elements are distinct.*/


#include <bits/stdc++.h>
using namespace std;

// Function to return p'th smallest element in a given array
int pthSmallest(int arr[], int n, int p)
{
// Sort the given array
sort(arr, arr + n);

// Return p'th element in the sorted array
return arr[p - 1];
}

int main()
{
int arr[] = { 112, 13, 15, 17, 119 };
int n = sizeof(arr) / sizeof(arr[0]), p = 2;
cout << "p'th smallest element is " << pthSmallest(arr, n, p);
return 0;
}


// Time Complexity of this solution is O(N Log N)