How to avoid multiple nested for-loops when one nested for-loop has range up to the current iteration of the outer for-loop? For example, consider the following code: This program returns a triplet from a list arr such that arr[i] - arr[j] = arr[j] - arr[k] = d and i<j<k.
d =3
arr = [1, 2, 4, 5, 7, 8, 10]
list1 = []
for biggest in range(0, len(arr)):
for bigger in range(0, biggest):
for big in range(0, bigger):
if abs(arr[big] - arr[bigger]) == d and abs(arr[bigger] - arr[biggest]) == d:
list1.append([arr[big], arr[bigger], arr[biggest]])
print(list1))
Are there any other alternatives to using multiple nested loops?