Python program to test if all elements in list are maximum of K apart
Given a list of numbers, the task is to write a Python program to test if all elements are maximum of K apart.
Examples:
Input : test_list = [475, 503, 425, 520, 470, 500], K = 100
Output : True
Explanation : Maximum element is 520 and minimum is 425, 520-425 = 95, which is less than 100, hence elements are in range.
Input : test_list = [475, 503, 425, 540, 470, 500], K = 100
Output : False
Explanation : Maximum element is 540 and minimum is 425, 520-425 = 115, which is more than 100, hence elements are not in range.
Method #1 : Using sort()
In this, we perform the task of sorting elements to get the min and max elements accessible using sort(). The next step is to get the difference among them, if less than the range, True is returned.
Python3
test_list = [ 475 , 503 , 425 , 520 , 470 , 500 ]
print ( "The original list is : " + str (test_list))
K = 100
test_list.sort()
res = test_list[ - 1 ] - test_list[ 0 ] < K
print ( "Are elements in range ? : " + str (res))
|
Output
The original list is : [475, 503, 425, 520, 470, 500]
Are elements in range ? : True
Time Complexity: O(n log(n))
Auxiliary Space: O(1)
Method #2 : Using min() + max()
Instead of changing the ordering or original list, min() and max() are used to get minimum and maximum elements respectively, and the difference between them gets the required result.
Python3
test_list = [ 475 , 503 , 425 , 520 , 470 , 500 ]
print ( "The original list is : " + str (test_list))
K = 100
res = max (test_list) - min (test_list) < K
print ( "Are elements in range ? : " + str (res))
|
Output
The original list is : [475, 503, 425, 520, 470, 500]
Are elements in range ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #3: Using numpy.ptp() to find range
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_list = [ 475 , 503 , 425 , 520 , 470 , 500 ]
print ( "The original list is : " + str (test_list))
K = 100
res = np.ptp(test_list) < K
print ( "Are elements in range ? : " + str (res))
|
Output:
The original list is : [475, 503, 425, 520, 470, 500]
Are elements in range ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #4: Using a for loop to check if all elements are in range
- Initialize a boolean variable flag as True
- Loop through each element elem in the list test_list
- Check if the absolute difference between elem and min(test_list) is greater than K. If yes, set flag to False and break out of the loop.
- Check if the absolute difference between elem and max(test_list) is greater than K. If yes, set flag to False and break out of the loop.
- After the loop, if flag is still True, then all elements are within the range K.
Python3
test_list = [ 475 , 503 , 425 , 520 , 470 , 500 ]
print ( "The original list is : " + str (test_list))
K = 100
flag = True
for elem in test_list:
if abs (elem - min (test_list)) > K:
flag = False
break
if abs (elem - max (test_list)) > K:
flag = False
break
print ( "Are elements in range ? : " + str (flag))
|
Output
The original list is : [475, 503, 425, 520, 470, 500]
Are elements in range ? : True
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(1)
Method #5: using the built-in function all() and a list comprehension.
Step-by-step approach:
- Define a function in_range() that takes two arguments: a list of integers and the range limit K.
- Use a list comprehension to create a list of Boolean values indicating whether each element in the list is within the range [min(test_list) – K, max(test_list) + K].
- Use the built-in function all() to check if all the Boolean values are True. If they are, return True; otherwise, return False.
Python3
def in_range(lst, K):
in_range_lst = [ min (lst) - K < = x < = max (lst) + K for x in lst]
if all (in_range_lst):
return True
else :
return False
test_list = [ 475 , 503 , 425 , 520 , 470 , 500 ]
K = 100
print (in_range(test_list, K))
|
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), because we are creating a new list of Boolean values with the same length as the input list.
Last Updated :
02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...