Given a list of integer elements, check for each element if its difference with successive element is K.
Input : test_list = [5, 6, 3, 2, 5, 3, 4], K = 1
Output : [True, False, True, False, False, True]
Explanation : 5, 6; 3, 2; and 3, 4 have 1 diff. between them.Input : test_list = [5, 6, 3, 2, 5, 3, 4], K = 2
Output : [False, False, False, False, True, False]
Explanation : Only 5, 3 has 2 diff between it.
Method #1 : Using list comprehension
This is one of the ways in which this task can be performed. In this, we check for each element with next one, if difference is K then we tag it True, else False.
Python3
# Python3 code to demonstrate working of # K difference Consecutive elements # Using list comprehension # initializing list test_list = [ 5 , 6 , 3 , 2 , 5 , 3 , 4 ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 3 # using list comprehension and abs() to compute result res = [ True if abs (test_list[idx] - test_list[idx + 1 ]) = = K else False for idx in range ( len (test_list) - 1 )] # printing result print ( "The difference list result : " + str (res)) |
The original list : [5, 6, 3, 2, 5, 3, 4] The difference list result : [False, True, False, True, False, False]
Time Complexity: O(n*n), where n is the elements of list
Auxiliary Space: O(n), where n is the size of list
Method #2 : Using zip() + list comprehension
This is another way in which this task can be performed. In this the consecutive list is paired using zip() and computations run through list comprehension.
Python3
# Python3 code to demonstrate working of # K difference Consecutive elements # Using zip() + list comprehension # initializing list test_list = [ 5 , 6 , 3 , 2 , 5 , 3 , 4 ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 3 # using list comprehension and abs() to compute result # zip() used to pair Consecutive elements list res = [ abs (a - b) = = K for a, b in zip (test_list, test_list[ 1 :])] # printing result print ( "The difference list result : " + str (res)) |
The original list : [5, 6, 3, 2, 5, 3, 4] The difference list result : [False, True, False, True, False, False]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method 3: Using a for loop:
Step-by-step approach:
- Import the numpy module as np.
- Create a list named test_list and initialize it with values [5, 6, 3, 2, 5, 3, 4].
- Print the original list using the print() function.
- Initialize a variable K with value 3.
- Create an empty list named res to store the result.
- Use a for loop to iterate over the range from 0 to len(test_list)-1. The loop variable i takes values 0, 1, 2, 3, 4, 5 (because len(test_list) is 7).
- In each iteration of the loop, use the abs() function to compute the absolute difference between the i-th and (i+1)-th elements of the test_list. If this absolute difference equals K, then append True to the res list, else append False.
- Print the result list using the print() function.
Python3
# Python3 code to demonstrate working of # K difference Consecutive elements # Using for loop # initializing list test_list = [ 5 , 6 , 3 , 2 , 5 , 3 , 4 ] # printing original list print ( "The original list : " + str (test_list)) # initializing K K = 3 # initializing empty list for result res = [] # using for loop and abs() to compute result for i in range ( len (test_list) - 1 ): if abs (test_list[i] - test_list[i + 1 ]) = = K: res.append( True ) else : res.append( False ) # printing result print ( "The difference list result : " + str (res)) |
The original list : [5, 6, 3, 2, 5, 3, 4] The difference list result : [False, True, False, True, False, False]
Time complexity: O(n)
Auxiliary space: O(n) as well, where n is the length of the input list.