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
# Python3 code to demonstrate working of # Test K in Range # Using any() # initializing list test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Range i, j = 2 , 5 # initializing K K = 4 # any() to check if any element other than K present # negation gives result res = not any (test_list[idx] ! = K for idx in range (i, j + 1 )) # printing result print ( "Are all elements in range K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Test K in Range # Using all() # initializing list test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Range i, j = 2 , 5 # initializing K K = 4 # all() to check all elements to be K res = all (test_list[idx] = = K for idx in range (i, j + 1 )) # printing result print ( "Are all elements in range K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Test K in Range # Initializing list test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing Range i, j = 2 , 5 # Initializing K K = 4 res = False if (test_list[i:j].count(K) = = len (test_list[i:j])): res = True # Printing the result print ( "Are all elements in range K ? : " + str (res)) |
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
# Python3 code to demonstrate working of # Test K in Range import operator # Initializing list test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ] # Printing original list print ( "The original list is : " + str (test_list)) # Initializing Range i, j = 2 , 5 # Initializing K K = 4 res = False x = test_list[i:j] if (operator.countOf(x, K) = = len (x)): res = True # Printing the result print ( "Are all elements in range K ? : " + str (res)) |
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
# Input list test_list = [ 2 , 3 , 4 , 4 , 4 , 4 , 6 , 7 , 8 , 2 ] i, j = 2 , 5 K = 4 # Flag initially to false res = True for x in range (i, j): if test_list[x] ! = K: # Setting flag to true res = False break # Printing the result print ( "Are all elements in range K? : " + str (res)) |
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.