Given a Tuple list, check if each tuple has a difference less than K.
Input : test_list = [(3, 4), (1, 2), (7, 8), (9, 13)], K = 2
Output : False
Explanation : 13 – 9 = 4 > 2.Input : test_list = [(3, 4), (1, 2), (7, 8)], K = 2
Output : True
Explanation : All have abs. diff 1 < 2.
Method #1 : Using loop
In this, we keep a boolean variable and check if any element is greater than or equal to K, then mark it False and break.
Python3
# Python3 code to demonstrate working of # Check if all tuples have element difference less than K # Using loop # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 res = True for ele1, ele2 in test_list: # using abs() to compute absolute difference if abs (ele1 - ele2) > = K: res = False # printing result print ( "Are all elements difference less than K ? : " + str (res)) |
The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)] Are all elements difference less than K ? : True
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1), as we are not using any extra data structures that depend on the size of the input.
Method #2 : Using all()
In this, we use all() to check if all the tuples have differences within K.
Python3
# Python3 code to demonstrate working of # Check if all tuples have element difference less than K # Using all() # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 # shorthand to solve this problem res = all ( abs (sub1 - sub2) < K for sub1, sub2 in test_list) # printing result print ( "Are all elements difference less than K ? : " + str (res)) |
The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)] Are all elements difference less than K ? : True
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1), as only constant extra space is used to store the value of K and the result.
Method#3: Using Recursive method.
Python3
# Python3 code to demonstrate working of # Check if all tuples have element difference less than K # Using recursive function def is_lessthanK(lst, K, start = 0 ): if start = = len (lst): # base condition return True if abs (lst[start][ 0 ] - lst[start][ 1 ]) > K: return False return is_lessthanK(lst, K, start + 1 ) # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 # callling function and storing value res = is_lessthanK(test_list, K) # printing result print ( "Are all elements difference less than K ? : " + str (res)) # this code contributed by tvsk |
The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)] Are all elements difference less than K ? : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 Using combinations
Using combinations from the itertools module to generate all possible pairs of tuples.
Approach:
- Import the combinations function from the itertools module.
- Generate all possible pairs of tuples using the combinations function.
- Iterate through each pair of tuples and calculate the absolute difference between each pair of elements in the two tuples.
- If the absolute difference is greater than or equal to K, return False.
- If all pairs have absolute differences less than K, return True.
Python3
from itertools import combinations # function to check tuple differences def check_tuple_difference(test_list, K): for i, j in combinations(test_list, 2 ): diff = abs (i[ 0 ] - j[ 0 ]) + abs (i[ 1 ] - j[ 1 ]) if diff > = K: return False return True # Input list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 13 )] # Custom value K = 2 print (check_tuple_difference(test_list, K)) |
False
Time complexity: O(n^2) because we generate all possible pairs of tuples.
Auxiliary Space: O(1)
Method #5: Using any() and filter() function
Approach:
- Initialize the test_list and K.
- Use the filter() function to filter out the tuples from the test_list that have an element difference greater than or equal to K.
- Check if any tuples are left in the filtered result using the any() function.
- Negate the result using the not operator to get the final result.
- Print the final result.
Python3
# initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 2 res = not any ( filter ( lambda x: abs (x[ 0 ] - x[ 1 ])> = K, test_list)) # printing result print ( "Are all elements difference less than K ? : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original list is : [(3, 4), (1, 2), (7, 8), (9, 8)] Are all elements difference less than K ? : True
Time Complexity: O(n), where n is the length of test_list. The filter() function iterates over each tuple in the list once to check the element difference, and the any() function also iterates over the filtered result to check if any tuples are left.
Auxiliary Space: O(1), because we use only a single boolean variable to store the result. The filter() function and lambda function use negligible space, and the any() function does not create any additional data structure.
Method 6: Using numpy library
- Import the numpy module using the “import” statement.
- Create a list of tuples “test_list” with 4 tuples.
- Initialize an integer variable “K” with a value of 2.
- Convert the list of tuples “test_list” to a numpy array “arr” using the “numpy.array()” method.
- Perform element-wise subtraction on the numpy array “arr” using the expression “arr[:,0]-arr[:,1]”.
- Take the absolute value of the result of step 5 using the “numpy.abs()” method.
- Check if all the elements in the numpy array from step 6 are less than the integer variable “K” using the “numpy.all()” method.
- Store the result of step 7 in a boolean variable “res”.
- Print the value of “res” with an appropriate message.
Python3
import numpy as np # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # initializing K K = 2 # converting list of tuples to numpy array arr = np.array(test_list) # using numpy element-wise operation and all() function res = np. all (np. abs (arr[:, 0 ] - arr[:, 1 ]) < K) # printing result print ( "Are all elements difference less than K ? : " + str (res)) |
OUTPUT : Are all elements difference less than K ? : True
Time complexity: O(n), where n is the length of the list, due to the element-wise operation and the all() function.
Auxiliary space: O(n), due to the use of the numpy array.
Method 7: Use the map() function along with the lambda function.
Step-by-step approach:
- Initialize the test_list and K values as given in the problem.
- Define a lambda function that takes each tuple from the test_list, finds the absolute difference between its elements, and checks if the difference is less than K. The lambda function returns True or False.
- Use the map() function to apply the lambda function to each tuple in the test_list. The map() function returns a list of True/False values.
- Use the all() function to check if all the values in the list are True.
- Print the result.
Python3
# initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 7 , 8 ), ( 9 , 8 )] # initializing K K = 2 # using map() and lambda function res_list = list ( map ( lambda x: abs (x[ 0 ] - x[ 1 ]) < K, test_list)) # using all() function res = all (res_list) # printing result print ( "Are all elements difference less than K ? : " + str (res)) |
Are all elements difference less than K ? : True
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n), for storing the list of True/False values returned by the map() function.