Given a List, Test if all elements in the given range is equal to K.
Input : test_list = [2, 3, 4, 4, 4, 4, 6, 7, 8, 2], i, j = 2, 5, K = 4
Output : True
Explanation : All elements in range are 4.
Input : test_list = [2, 3, 4, 9, 4, 4, 6, 7, 8, 2], i, j = 2, 5, K = 4
Output : False
Explanation : All elements in range are not 4.
Method #1 : Using any() function
In this, we check for the negation of logic to be found, check if we get any element other than K, we return the negation of the true value to get the actual result.
Python3
test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 5
K = 4
res = not any (test_list[idx] ! = K for idx in range (i, j + 1 ))
print ( "Are all elements in range K ? : " + str (res))
|
Output
The original list is : [2, 3, 4, 4, 4, 4, 6, 7, 8, 2]
Are all elements in range K ? : True
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using all()
In this, we check for all elements to be K in the required range of the list using all() function.
Python3
test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 5
K = 4
res = all (test_list[idx] = = K for idx in range (i, j + 1 ))
print ( "Are all elements in range K ? : " + str (res))
|
Output
The original list is : [2, 3, 4, 4, 4, 4, 6, 7, 8, 2]
Are all elements in range K ? : True
Time Complexity: O(n*logn), where n is the length of the input list. This is because we’re using the built-in all() function which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(1), as we’re not using any additional space other than the input list itself.
Method #3 : Using count() and len() methods
Python3
test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 5
K = 4
res = False
if (test_list[i:j].count(K) = = len (test_list[i:j])):
res = True
print ( "Are all elements in range K ? : " + str (res))
|
Output
The original list is : [2, 3, 4, 4, 4, 4, 6, 7, 8, 2]
Are all elements in range K ? : True
Method #4 : Using operator.countOf() and len() methods
Approach:
- Slice the given list from ‘i’ to ‘j’ and set res to False.
- Check whether the count of K in sliced list is equal to the length of sliced list using operator.countOf()
- If equal set res to True.
- Display “Are all elements in range K ?”+res variable.
Python3
import operator
test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ]
print ( "The original list is : " + str (test_list))
i, j = 2 , 5
K = 4
res = False
x = test_list[i:j]
if (operator.countOf(x, K) = = len (x)):
res = True
print ( "Are all elements in range K ? : " + str (res))
|
Output
The original list is : [2, 3, 4, 4, 4, 4, 6, 7, 8, 2]
Are all elements in range K ? : True
Time Complexity: O(N) N – length of sliced list.
Auxiliary Space: O(1)
Method 5: Using for loop
Approach:
- Initialize the list “test_list” with some integer values.
- Initialize the variables “i” and “j” with the range limits, i.e., “i” is the starting index, and “j” is the ending index of the range of elements that we want to check in the list “test_list”.
- Initialize the variable “K” with the element that we want to check in the range of elements.
- Initialize the variable “res” with True. This variable will store the result of whether all the elements in the given range are equal to K or not.
- Iterate through the range of indices from “i” to “j” using a for loop and the range() function.
- Within the loop, check if the element at the current index in the list “test_list” is not equal to K using the != operator.
- If the element at the current index is not equal to K, set the value of “res” to False and break out of the loop.
- Print the final result using the print() function.
Python3
test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ]
i, j = 2 , 5
K = 4
res = True
for x in range (i, j):
if test_list[x] ! = K:
res = False
break
print ( "Are all elements in range K? : " + str (res))
|
Output
Are all elements in range K? : True
Time complexity: O(1)
Auxiliary space: O(1), as we only use a constant amount of extra memory to store the loop variable and the boolean result.
Last Updated :
03 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...