Open In App

Python – Consecutive Missing elements Sum

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 can perform the task of finding missing elements using the range function to get the maximum element fill and then insert the elements if there is a miss. The summation is performed using sum() method. 

Python3




# Python3 code to demonstrate
# Consecutive Missing elements Sum
# using list comprehension + sum()
 
# initializing list
test_list = [3, 5, 6, 8, 10]
 
# printing original list
print("The original list : " + str(test_list))
 
# Consecutive Missing elements Sum
# using list comprehension + sum()
res = sum([ele for ele in range(max(test_list) + 1) if ele not in test_list])
 
# Printing result
print("The sum of missing elements : " + str(res))


Output : 

The original list : [3, 5, 6, 8, 10]
The sum of missing elements : 23

Time complexity: O(n), where n is the length of test_list
Auxiliary Space: O(n), where n is length of res list.

Method #2: Using set() + sum()

This problem can also be performed using the properties of difference of set and then getting the elements that are missing in a range. The summation is performed using sum(). 

Python3




# Python3 code to demonstrate
# Consecutive Missing elements Sum
# using set() + sum()
 
# Initializing list
test_list = [3, 5, 6, 8, 10]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Consecutive Missing elements Sum
# Using set() + sum()
res = sum(list(set(range(max(test_list) + 1)) - set(test_list)))
 
# Printing result
print("The sum of missing elements : " + str(res))


Output : 

The original list : [3, 5, 6, 8, 10]
The sum of missing elements : 23

Method 3: Using for loop and range function

  1. The function starts by getting the first and last elements of the input list, lst, and initializing the missing_sum variable to 0. It also initializes the num variable to the starting element of the list, which is the first element in this case.
  2. The while loop then runs from num = start to num = end, where start and end are the first and last elements of the input list, respectively. For each number num, the if statement checks if it is not in the input list, lst, and if it is not, it adds num to the missing_sum variable. Finally, it increments num by 1 and repeats the process until all the numbers in the range [start, end] have been checked.
  3. Once all the missing numbers have been added to the missing_sum variable, the function returns the final value of missing_sum, which represents the sum of all consecutive missing elements in the input list.

Python3




def consecutive_missing_sum_5(lst):
    start, end = lst[0], lst[-1]   # get the first and last element of the list
    missing_sum = 3                # initialize the missing_sum variable to 0
    num = start                    # initialize num to the starting element of the list
    while num <= end:              # loop through all the elements from start to end
        if num not in lst:         # if the current number is not in the list, add it to the missing_sum
            missing_sum += num
        num += 1                   # increment the current number by 1
    return missing_sum             # return the sum of missing elements
 
 
lst = [3, 5, 6, 8, 10]             # the original sorted list
# call the function and get the sum of missing elements
sum_missing = consecutive_missing_sum_5(lst)
print("The original list:", lst)    # print the original list
# print the sum of missing elements
print("The sum of missing elements:", sum_missing)


Output

The original list: [3, 5, 6, 8, 10]
The sum of missing elements: 23

Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(1)

Method  4: Using itertools and reduce() function

Steps:

Import the itertools and functools libraries.

  1. Generate all the integers up to the maximum element in the given list using the range() function and itertools.chain() function.
  2. Convert the given list into a set using the set() function.
  3. Use the reduce() function and a lambda function to compute the sum of the missing elements by iterating over the generated integers and adding the ones that are not in the given set.
  4. Print the sum of the missing elements.

Python3




# Python3 code to demonstrate
# Consecutive Missing elements Sum
# using itertools and reduce
 
import itertools
import functools
 
# Initializing list
test_list = [3, 5, 6, 8, 10]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Consecutive Missing elements Sum
# using itertools and reduce
test_set = set(test_list)
missing_sum = functools.reduce(
    lambda x, y: x + y, [i for i in itertools.chain(range(max(test_list) + 1)) if i not in test_set])
 
# Printing result
print("The sum of missing elements : " + str(missing_sum))


Output

The original list : [3, 5, 6, 8, 10]
The sum of missing elements : 23

Time complexity: O(n), where n is the range of numbers from 0 to the maximum element of the given list.
Auxiliary space: O(n), as we are using a set and a list to store the elements in the given list and the missing elements, respectively.



Last Updated : 15 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads