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
Time complexity: O(n), where n is the number of elements in the list
Auxiliary space: O(1), as only a few variables are used in the code.
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
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(1). We are only storing the original list, k, and the minimum value greater than k in memory, and these do not depend on the size of the input.
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
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary space: O(1), since the code only uses a few variables to store the input list, the value of k, and the minimum value greater than k.
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
Time complexity: O(nlogn), where n is the length of the list. This is because we first sort the list using the sort() method, which has a time complexity of O(nlogn).
Auxiliary space: O(1), as we are not using any extra space apart from the given list and the variable for k.
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
Time complexity: O(n log n), where n is the length of the input list.
Auxiliary space complexity: O(1), because the algorithm uses a constant amount of extra space regardless of the input size.
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
Time complexity: 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.
Auxiliary space: O(n) as it requires creating a copy of the list in the form of a heap.
Method#7:Using recursion
Python3
def find_smallest_greater_than_k(test_list, k, index, min_val): if index = = len (test_list): return min_val if min_val > test_list[index] and test_list[index] > k: min_val = test_list[index] return find_smallest_greater_than_k(test_list, k, index + 1 , min_val) # Initializing list test_list = [ 1 , 4 , 7 , 5 , 10 ] # Initializing k k = 6 # Printing original list print ( "The original list is : " + str (test_list)) # Calling function to find minimum value greater than K min_val = find_smallest_greater_than_k(test_list, k, 0 , 10000000 ) # Printing result print ( "The minimum value greater than 6 is : " + str (min_val)) #This code is contributed by Vinay Pinjala. |
The original list is : [1, 4, 7, 5, 10] The minimum value greater than 6 is : 7
Time Complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...