Given two elements, write a Python program to check that they don’t occur as their neighbors in the list.
Examples:
Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 7, 4 Output : True Explanation : 7 doesn't occur are 4's neighbour.
Input : test_list = [3, 7, 2, 1, 4, 5, 7, 9], i, j = 5, 4 Output : False Explanation : 5 occurs are 4's neighbour.
Method #1: Using loop
In this, we check for next and previous elements to not be ‘i’ or ‘j’ while iteration of list.
Python3
# Python3 code to demonstrate working of# Test for Non-neighbours in List# Using loop# Initializing listtest_list = [3, 7, 2, 1, 4, 5, 7, 9]# Printing original listprint("The original list is : " + str(test_list))# Initializing i, ji, j = 7, 4res = Truefor idx in range(1, len(test_list) - 1): if test_list[idx] == i: # check for surrounding element to be j if i if test_list[idx - 1] == j or test_list[idx + 1] == j: res = False break# Printing resultprint("Are i, j Non-neighbours' : " + str(res)) |
The original list is : [3, 7, 2, 1, 4, 5, 7, 9] Are i, j Non-neighbours' : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using all() function
This is a one-liner approach to solving this problem. In this, we perform the task of checking for all elements for neighbors using all() in the generator expression in all() functions.
Python3
# Python3 code to demonstrate working of# Test for Non-neighbours in List# Using all()# initializing listtest_list = [3, 7, 2, 1, 4, 5, 7, 9]# printing original listprint("The original list is : " + str(test_list))# initializing i, ji, j = 7, 4# checking for preceding and succeeding element # not to be j if curr is ires = all(test_list[idx - 1] != j and test_list[idx + 1] != j for idx in range(1, len(test_list) - 1) if test_list[idx] == i)# printing resultprint("Are i, j Non-neighbours' : " + str(res)) |
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using find() method
Python3
# Python3 code to demonstrate working of# Test for Non-neighbours in List# Initializing listtest_list = [3, 7, 2, 1, 4, 5, 7, 9]# Printing original listprint("The original list is : " + str(test_list))# Initializing i, ji, j = 7, 4x=list(map(str,test_list))y="*".join(x)a=str(i)+"*"+str(j)b=str(j)+"*"+str(i)res = Trueif y.find(a)!=-1 or y.find(b)!=-1: res=False # Printing resultprint("Are i, j Non-neighbours' : " + str(res)) |
The original list is : [3, 7, 2, 1, 4, 5, 7, 9] Are i, j Non-neighbours' : True
Time Complexity: O(N), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(N), where n is the number of elements in the list “test_list”.
Method 4: Using slicing and set operations
Steps:
- Find the indices of i and j in the list using the index() method.
- Create two slices of the list:
- Slice from the start to the index of i.
- Slice from the index of j to the end.
- Create a set by concatenating the two slices and check if j is present in the set.
- If j is not present in the set, then return True else return False.
Python3
# Python3 code to demonstrate working of# Test for Non-neighbours in List# Using slicing and set operations# Initializing listtest_list = [3, 7, 2, 1, 4, 5, 7, 9]# Printing original listprint("The original list is : " + str(test_list))# Initializing i, ji, j = 7, 4# Finding indices of i and ji_idx = test_list.index(i)j_idx = test_list.index(j)# Creaing two slicesslice1 = set(test_list[:i_idx])slice2 = set(test_list[j_idx+1:])# check if j is not present in the setres = j not in slice1.union(slice2)# printing resultprint("Are i, j Non-neighbours' : " + str(res)) |
The original list is : [3, 7, 2, 1, 4, 5, 7, 9] Are i, j Non-neighbours' : True
Time complexity: O(N)
Auxiliary space: O(N)
