- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Nth smallest Greater than K
The given problem statement is required to find the Nth smallest greater than K using Python. So we can use a combination of iteration and sorting techniques to find the required element.
Understanding the Problem
The problem at hand is to find the Nth smallest but greater than the number K with the help of Python. Basically we are required to find the number which is greater than K but it should be the Nth number from the number K in the sorted list. For example we will understand this phenomena with the help of the below image −
Approach 1: Algorithm
In this approach we will construct a method in which we will pass three parameters: list of numbers, a value K and also an integer N. This function will work to filter out the numbers which are greater than K and then it will sort the numbers in ascending order. Then the function will check if there are N numbers in the filtered list so return the Nth smallest number.
Step 1 − First initiate the numbers list and the values of K and N.
Step 2 − Next we will define the function to find the Nth smallest number which should be greater than K. Name this function as get_Nth_smallest and inside the function pass num_list, K and N as inputs.
Step 3 − Now filter the number of the given num_list which is greater than K.
Step 4 − After filtering out the numbers we will sort the remaining numbers using the sorted method.
Step 5 − After sorting the numbers we will check the length of the remaining numbers list if it is less than N then return None. Otherwise return the Nth number from the sorted list.
Example
# Initialize the list and variables num_list = [2, 6, 4, 7, 8, 10, 15, 9] K = 5 N = 3 # Define the function to get Nth smallest greater than K def get_Nth_smallest(num_list, K, N): filter_nums = [num for num in num_list if num > K] sorted_num_list = sorted(filter_nums) if len(sorted_num_list) < N: return None return sorted_num_list[N-1] # Call the above function Output = get_Nth_smallest(num_list, K, N) # Print the Output print(f"{N} smallest number greater than {K} is: {Output}")
Output
3 smallest number greater than 5 is: 8
Complexity
The time complexity for finding the Nth Smallest greater than K using Python is O(N log N), here N is the number of items in the given num_list. In the code we have filtered out the numbers which takes O(N) time. And then we perform a sorting operation which takes O(N log N) time. Then we have checked for the Nth smallest number in the sorted list which requires O(1) time. So overall time complexity is O(N log N).
Approach 2: Algorithm
In this approach we will use the filter method with lambda function to get the required result.
Step 1 − First initiate the variables num_list, K and N.
Step 2 − Define a function to find the Nth smallest number which should be greater than K and give this function name as get_Nth_smallest() and pass num_list, K and N as inputs.
Step 3 − Now filter the number of the given num_list using the filter and lambda functions which is greater than K.
Step 4 − After filtering out the numbers we will sort the remaining numbers using the sorted method.
Step 5 − Return the (N - 1) th item as a result.
Example
# Initialize the list and variables num_list = [2, 6, 4, 7, 8, 10, 15, 9] K = 9 N = 2 # Define the function to get Nth smallest greater than K def get_Nth_smallest(num_list, K, N): result = list(filter(lambda a: a > K, num_list)) result.sort() return result[N-1] # Call the above function Output = get_Nth_smallest(num_list, K, N) print(f"{N} smallest number greater than {K} is: {Output}")
Output
2 smallest number greater than 9 is: 15
Conclusion
We have successfully implemented the solution for finding the Nth smallest greater than K using Python. We have used very efficient logic to get the required result. This problem can be helpful when we are required to find the key or data from a large size data and whenever we don't know about the Nth element.