Open In App

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 code to demonstrate working of
# Test if all elements are in range size
# Using sort()
 
# initializing list
test_list = [475, 503, 425, 520, 470, 500]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 100
 
# sorting list
test_list.sort()
 
# checking if greater than range
res = test_list[-1] - test_list[0] < K
 
# printing result
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 code to demonstrate working of
# Test if all elements are in range size
# Using min() + max()
 
# initializing list
test_list = [475, 503, 425, 520, 470, 500]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 100
 
# using min() and max() rather than
# changing order
res = max(test_list) - min(test_list) < K
 
# printing result
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”




# import numpy
import numpy as np
 
# initializing list
test_list = [475, 503, 425, 520, 470, 500]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 100
 
# calculating range value
res = np.ptp(test_list) < K
 
# printing result
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




# Python3 code to demonstrate working of
# Test if all elements are in range size
# Using a for loop
 
# initializing list
test_list = [475, 503, 425, 520, 470, 500]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 100
 
# using a for loop to check if all elements are in range
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
 
# printing result
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:




def in_range(lst, K):
    # Create a list of Boolean values indicating whether each element is in range
    in_range_lst = [min(lst) - K <= x <= max(lst) + K for x in lst]
     
    # Check if all Boolean values are True
    if all(in_range_lst):
        return True
    else:
        return False
 
# Example usage:
test_list = [475, 503, 425, 520, 470, 500]
K = 100
print(in_range(test_list, K))

Output
True

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.


Article Tags :