Python - Smallest missing element after K
Last Updated :
13 Mar, 2023
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
# Python3 code to demonstrate working of
# Smallest missing element after K
# Using loop
# initializing list
test_list = [1, 3, 4, 5, 7, 9, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 7
ele = 1
# infinite loop to break at element found
while(1):
# checking if greater than K and not in list
if ele > K and ele not in test_list:
res = ele
break
ele = ele + 1
# printing result
print("The Smallest element greater than K in list : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Smallest missing element after K
# Using set difference
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)
# initializing list
test_list = [1, 3, 4, 5, 7, 9, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 7
res=smallest_missing_element(test_list,K)
# printing result
print("The Smallest element greater than K in list : " + str(res))
#this code contributed by tvsk
OutputThe 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
# Python3 code to demonstrate working of
# Smallest missing element after K
# Using loop
# initializing list
test_list = [1, 3, 4, 5, 7, 9, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
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
# printing result
print("The Smallest element greater than K in list : " + str(res))
OutputThe 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
# initializing list
test_list = [1, 3, 4, 5, 7, 9, 10]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 7
# calling function to get result
res = smallest_missing_element(test_list, K)
# printing result
print("The Smallest element greater than K in list : " + str(res))
OutputThe 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):
# Initialize missing to K+1
missing = K + 1
# Initialize a counter dictionary to count the occurrence of each element in the list
counter = {}
for num in test_list:
# If the number is greater than K, add it to the counter dictionary
if num > K:
counter[num] = counter.get(num, 0) + 1
# Iterate over the range from K+1 to the maximum element in the list
for i in range(K+1, max(test_list)+1):
# If the current number is not in the counter dictionary, it is the smallest missing element
if i not in counter:
return i
# If no missing element is found, return the maximum element in the list + 1
return max(test_list) + 1
# Driver code to test the function
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))
OutputThe 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)
Similar Reads
Python - Consecutive Missing elements Sum Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get a summation of all the missing elements. Letâs discuss certain ways in which this can be done. Method #1 : Using list comprehension + sum() We
5 min read
Python - Nth smallest Greater than K This article offers various approaches to solve the problem of finding Nth smallest number in python list greater than a specific element K in python list. This basically provides all the approaches from naive to one-liners so that they can be used in programming whenever required. Method 1 : Naive
8 min read
Python Program for k-th missing element in sorted array Given an increasing sequence a[], we need to find the K-th missing contiguous element in the increasing sequence which is not present in the sequence. If no k-th missing element is there output -1. Examples : Input : a[] = {2, 3, 5, 9, 10}; k = 1; Output : 1 Explanation: Missing Element in the incre
4 min read
Python - Remove characters till K element Sometimes, while working with Python, we can have problem in which we need to get all elements in list occurring after particular character in list. This kind of problem can have application in data domains and web development. Lets discuss certain ways in which this task can be performed. Method #1
5 min read
Python - Alternate Minimum element in list Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct the list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications and sometime
4 min read
Python - Non K distant elements Given a list, the task is to write a Python program to extract all the elements such that no element is at K distant from one other. Examples: Input : test_list = [8, 10, 16, 20, 3, 1, 7], K = 2 Output : [16, 20, 7] Explanation : 16 + 2 = 18, 16 - 2 = 14, both are not in list, hence filtered. Input
2 min read
Python | Get Kth element till N Sometimes, we may come across a utility in which we require to get the first N sublist elements that too only a particular index. This can have an application in queuing to get only the Kth N personâs name. Letâs discuss certain ways in which this can be done. Method #1 : Using list comprehension an
3 min read
Python - Maximum element till K value One of the problem that is basically a subproblem for many complex problems, finding maximum number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this
3 min read
Python - Modulo K elements removal Due to the upcoming of Machine Learning, focus has now moved on handling the certain values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of certain values in essential and knowledge
6 min read
Python | Find missing elements in List Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
7 min read