Python – Smallest missing element after K
Given a List, get the smallest missing element after K in List.
Input : test_list = [1, 3, 4, 5, 7, 9, 10], K = 5
Output : 6
Explanation : After 5, 6 is 1st element missing in list.
Input : test_list = [1, 3, 4, 5, 7, 9, 11], K = 9
Output : 10
Explanation : After 9, 10 is 1st element missing in list.
Approach: Using loop
In this, we iterate through numbers and check for element missing in list, which is greater than K using conditionals.
Python3
test_list = [ 1 , 3 , 4 , 5 , 7 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
K = 7
ele = 1
while ( 1 ):
if ele > K and ele not in test_list:
res = ele
break
ele = ele + 1
print ( "The Smallest element greater than K in list : " + str (res))
|
Output
The original list is : [1, 3, 4, 5, 7, 9, 10]
The Smallest element greater than K in list : 8
Method #2: Using set difference
The steps of the algorithm can be described as follows:
- Create a set greater_than_K containing the range of integers between K+1 and the maximum element of the list test_list plus 1.
- Create a set missing_elements by taking the set difference between greater_than_K and the set of elements in the list test_list.
- Return the minimum element in the set missing_elements.
Python3
def smallest_missing_element(test_list, K):
greater_than_K = set ( range (K + 1 , max (test_list) + 2 ))
missing_elements = greater_than_K - set (test_list)
return min (missing_elements)
test_list = [ 1 , 3 , 4 , 5 , 7 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
K = 7
res = smallest_missing_element(test_list,K)
print ( "The Smallest element greater than K in list : " + str (res))
|
Output
The original list is : [1, 3, 4, 5, 7, 9, 10]
The Smallest element greater than K in list : 8
Time complexity: O(n), where n is the length of the input list test_list, because we need to iterate through the list to find the maximum element.
Auxiliary space: O(m), where m is the number of elements between K+1 and the maximum element of the list, because we need to create a set containing these elements. In the worst case, m can be O(n), which would make the space complexity of the algorithm O(n). However, in practice, the number of missing elements is likely to be smaller than the length of the input list, so the actual space complexity of the algorithm is likely to be smaller than O(n).
Method #3 : Using for loops
Approach
- Slice list from index of K to end of list
- Create a new list in range of K to max of list
- Now check for the element that is present in new list and not present in sliced list
- Display the element
Python3
test_list = [ 1 , 3 , 4 , 5 , 7 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
K = 7
res = 0
y = test_list[test_list.index(K):]
z = []
for i in range (K, max (y) + 1 ):
z.append(i)
for i in z:
if i not in y:
res = i
break
print ( "The Smallest element greater than K in list : " + str (res))
|
Output
The original list is : [1, 3, 4, 5, 7, 9, 10]
The Smallest element greater than K in list : 8
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 4: using the heapq module:
First create an empty heap. Then, we loop through the elements of the input list and push any element greater than K onto the heap using heapq.heappush(). We then initialize a variable smallest to K+1 and loop through the heap using heapq.heappop(). If the popped element is not equal to smallest, we return smallest. Otherwise, we increment smallest by 1 and continue the loop. If we have processed all the elements in the heap and still haven’t found a missing element, we return smallest.
Python3
import heapq
def smallest_missing_element(test_list, K):
heap = []
for x in test_list:
if x > K:
heapq.heappush(heap, x)
smallest = K + 1
while heap:
num = heapq.heappop(heap)
if num ! = smallest:
return smallest
smallest + = 1
return smallest
test_list = [ 1 , 3 , 4 , 5 , 7 , 9 , 10 ]
print ( "The original list is : " + str (test_list))
K = 7
res = smallest_missing_element(test_list, K)
print ( "The Smallest element greater than K in list : " + str (res))
|
Output
The original list is : [1, 3, 4, 5, 7, 9, 10]
The Smallest element greater than K in list : 8
Time complexity: O(nlogn), where n is the number of elements in the list.
Auxiliary space: O(k), where k is the number of elements greater than K in the list.
Method #6: Using Counter and range
- Initialize a variable missing to K+1
- Initialize a counter dictionary to count the occurrence of each element in the list.
- Iterate over the range from K+1 to the maximum element in the list.
- For each number, check if it exists in the counter dictionary. If it does not, return the number as it is the smallest missing element.
- If the iteration completes without finding a missing element, return the maximum element in the list + 1.
Python3
def smallest_missing_element(test_list, K):
missing = K + 1
counter = {}
for num in test_list:
if num > K:
counter[num] = counter.get(num, 0 ) + 1
for i in range (K + 1 , max (test_list) + 1 ):
if i not in counter:
return i
return max (test_list) + 1
test_list = [ 1 , 3 , 4 , 5 , 7 , 9 , 10 ]
K = 7
print ( "The original list is : " + str (test_list))
res = smallest_missing_element(test_list, K)
print ( "The Smallest element greater than K in list : " + str (res))
|
Output
The original list is : [1, 3, 4, 5, 7, 9, 10]
The Smallest element greater than K in list : 8
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...