Python | Find smallest element greater than K
Given a list, write a Python program to find the smallest number which is greater than a specific element K. Let’s see all the approaches to solve this problem, from naive to one-liners so that they can be used in programming whenever required.
Method #1: Naive Method Using loop we keep on re-initializing the named variable if we find the element smaller than the previous value than the named variable and greater than K.
Python3
# Python3 code to demonstrate # smallest number greater than K # using naive method # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) # Using naive method # to find smallest number # greater than K min_val = 10000000 for i in test_list: if min_val > i and i > k: min_val = i # Printing result print ( "The minimum value greater than 6 is : " + str (min_val)) |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is: 7
Method #2 : Using min() + generator expression min() returns the minimum number in a sequence and coupling it with a generator expression can perform this task in much concise way and hence more useful when required to save time.
Python3
# Python3 code to demonstrate # smallest number greater than K # using min() + generator expression # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) # Using min() + generator expression # to find smallest number # greater than K min_val = min (i for i in test_list if i > k) # Printing result print ( "The minimum value greater than 6 is : " + str (min_val)) |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is : 7
Method #3 : min() + filter() Similar approach to method above, just to filter the numbers in list greater than k, filter() instead of generator expression is used in this approach. Works in a similar way as above.
Python3
# Python3 code to demonstrate # smallest number greater than K # using min() + filter() # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) # Using min() + filter() # to find smallest number # greater than K min_val = min ( filter ( lambda i: i > k, test_list)) # Printing result print ( "The minimum value greater than 6 is : " + str (min_val)) |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is : 7
Method #4 : Using sort() + bisect_right() bisect_right() coupled with sort() performs the task of binary search for us, and hence is a good option to achieve the solution to this problem. bisect_right() because it returns strictly greater number, not the number itself if it is present in the list.
Python3
# Python3 code to demonstrate # smallest number greater than K # using sort() + bisect_right() from bisect import bisect_right # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) # Using sort() + bisect_right() # to find smallest number # greater than K test_list.sort() min_val = test_list[bisect_right(test_list, k)] # Printing result print ( "The minimum value greater than 6 is : " + str (min_val)) |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is : 7
Method #5 : Using sort() and for loop
Python3
# Python3 code to demonstrate # smallest number greater than K # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) test_list.sort() for i in test_list: if (i > k): res = i break # Printing result print ( "The minimum value greater than 6 is : " + str (res)) |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is : 7
Method #6 : Using heapq module
Approach is using the heapq module in Python. The heapq module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. We can use the heapq.heapify function to transform the list into a heap, and then use the heapq.heappop function to pop the smallest element from the heap until we find an element that is greater than k.
For example:
Python3
import heapq def find_smallest_greater_than_k(lst, k): heapq.heapify(lst) while lst: element = heapq.heappop(lst) if element > k: return element return None lst = [ 1 , 4 , 7 , 5 , 10 ] k = 6 print (find_smallest_greater_than_k(lst, k)) # Output: 7 #This code is contributed by Edula Vinay Kumar Reddy |
7
This approach has a time complexity of O(n * log n) as it involves transforming the list into a heap and then performing log n operations for each element in the list. The space complexity is O(n) as it requires creating a copy of the list in the form of a heap.
Please Login to comment...