Python – Extract element with relative difference greater than K

Given a list of numbers, the task is to write a Python program to extract all numbers with differences of the next and previous elements with a current greater than K.
Input : test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5], K = 4
Output : [9, 10]Explanation : 9 has 1 as preceding element and 2 as succeeding. 8 and 7 are its difference respectively which are greater than 4.Input : test_list = [2, 7, 4, 1, 9, 2], K = 4
Output : [9]Explanation : 9 has 1 as preceding element and 2 as succeeding. 8 and 7 are its difference respectively which are greater than 4.
Method #1: Using loop
In this, we check using brute force if the next or previous element has elements less than K difference and omit them. Loop is used for the iteration of all the elements.
Python3
# Python3 code to demonstrate working of# Extract element with relative difference # greater than K Using loop# initializing listtest_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 4res = []for idx in range(1, len(test_list)): # using abs to get absolute difference if abs(test_list[idx - 1] - test_list[idx]) > K\ and abs(test_list[idx + 1] - test_list[idx]) > K: res.append(test_list[idx])# printing resultprint("The extracted difference elements : " + str(res)) |
The original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5] The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
This is similar to the above method, the difference only being the usage of list comprehension for availing a shorthand to solving the issue.
Python3
# Python3 code to demonstrate working of# Extract element with relative difference# greater than K Using list comprehension# initializing listtest_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]# printing original listprint("The original list is : " + str(test_list))# initializing KK = 4# using abs to get absolute difference# list comprehension provides shorthandres = [test_list[idx] for idx in range( 1, len(test_list)) if abs(test_list[idx - 1] - test_list[idx]) > K and abs(test_list[idx + 1] - test_list[idx]) > K]# printing resultprint("The extracted difference elements : " + str(res)) |
The original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5] The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using numpy
Note: Install numpy module using command “pip install numpy”
Python3
#importing numpy import numpy as np # initializing listtest_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5] # printing original listprint("The original list is : " + str(test_list)) # initializing KK = 4 # using np.diff to get absolute differenceres = [test_list[idx] for idx in range(1, len(test_list)) if np.diff([test_list[idx - 1], test_list[idx]])[0] > K and np.diff([test_list[idx + 1], test_list[idx]])[0] > K] # printing resultprint("The extracted difference elements : " + str(res)) |
Output:
The original list is : [2, 7, 4, 1, 9, 2, 3, 10, 1, 5] The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using filter()
- Use filter() to iterate through all the elements in the test_list except the first and the last element (since we are checking the relative difference between adjacent elements).
- The lambda function inside filter() checks whether the absolute difference between the current element and its adjacent elements is greater than K.
- The resulting elements that satisfy the condition are stored in the list res.
Python3
test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5]K = 4res = list(filter(lambda x: abs(x - test_list[test_list.index(x) - 1]) > K and abs(x - test_list[test_list.index(x) + 1]) > K, test_list[1:-1]))print("The extracted difference elements : " + str(res)) |
The extracted difference elements : [9, 10]
Time Complexity: O(n)
Auxiliary Space: O(n), for storing the result in the list res



