#!/usr/bin/python3 # ==================================================================== # From: www.youtube.com/watch?v=2wVjt3yhGwg # Two Pointer Algorithm | Two sum Problem | # solve DS Problen in O(N) Time # ==================================================================== print() print('Find all pairs of integers in a sorted list that sum to 6') print() target = 6 nums = [ -3,2,3,3,6,8,9,15 ] # ordered list of integers ##nums = [ 3,3 ] c = 0 # found count idxa = 0 # index (pointer) a idxb = len(nums) - 1 # index (pointer) b t = 0 # test count while idxa < idxb: t += 1 ## print(f'test[{t}]: idxa = {idxa} idxb = {idxb}') total = nums[idxa] + nums[idxb] if total == target: c += 1 ## print(f'(total = {total}) {nums[idxa]} + {nums[idxb]} = {target}') if total >= target: idxb -= 1 else: idxa += 1 print() print(f'{c} found - end of program')