- nested loop
remand = [3, 5, -4, 8, 11, 1, -1, 6] goal = 10 def palindrome(array: list[int], goal: int) -> tuple: length = len(array) for i in range(length - 1): for j in range(i + 1, length): if array[i] + array[j] == goal: return array[i], array[j] return () print(palindrome(remand, goal))
- dict
remand = [3, 5, -4, 8, 11, 1, -1, 6] goal = 10 def palindrome(array: list[int], goal: int): v = {} for b in array: mate = goal - b if mate in v: return mate, b v[b] = None return () print(palindrome(remand, goal))
- cursor: low high
remand = [3, 5, -4, 8, 11, 1, -1, 6] goal = 10 def palindrome(array: list[int], goal: int) -> tuple: array.sort() low, high = 0, len(array) - 1 while low < high: if array[low] + array[high] < goal: low += 1 elif array[low] + array[high] > goal: high -= 1 else: return array[low], array[high] return () print(palindrome(remand, goal))