Question:
write a function named sums that takes 3 arguments: 2 lists of integers nums1 and nums2, and an integer target; then return a tuple containing indices i and j of the two numbers in nums1 and nums2, respectively, where the numbers at that indices add up to target (i.e, nums1[i] + num2[j] = target). If there is no solution, return “Not found”. The function sums must contain exactly only one loop (either a for-loop or a while-loop), otherwise you will get no point. A nested-loop is considered as more than one loop.Answer:
You can solve it in O(n) with a dictionary and a single for loop;(3, 0)
Be aware that while there is a single
for
loop, the code is looping to construct the dictionary, it’s just not visible directly.If you have better answer, please add a comment about this, thank you!