Open In App

Python program to test if all elements in list are maximum of K apart

Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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




# 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”

Python3




# 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

  • 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




# 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:

  • 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):
    # 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.



Last Updated : 02 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads