Hello @amsuarez
,
Thanks for adding a popular problem with easy explanation.

Here is my approach to solve the problem using Python.
I have included both brute force(O(n^2)), dictionary method and tested both method for several test cases:

deftwo_sum_brute_force(nums,target):total_numbers=len(nums)foriinrange(total_numbers):forjinrange(i+1,total_numbers):ifnums[i]+nums[j]==target:return[i,j]deftwo_sum_dictionary(nums,target):total_numbers=len(nums)d={}foriinrange(total_numbers):current_value=nums[i]other_value=target-current_valueifother_valueind:returnlist(sorted([i,d[other_value]]))ifcurrent_valuenotind:d[current_value]=iif__name__=="__main__":test_cases=[[[2,7,11,15],9],[[2,7,11,15],13],[[2,7,11,15],26]]expected_results=[[0,1],[0,2],[2,3],]total_test_cases=len(test_cases)foriinrange(total_test_cases):nums,target=test_cases[i]expected_result=expected_results[i]# Test Brute Force Solution
result=two_sum_brute_force(nums,target)assert_message="two_sum_brute_force({}, {}) returns {}. Expected {}".format(nums,target,result,expected_result)assertresult==expected_result,assert_message# Test Dictionary Solution
result=two_sum_dictionary(nums,target)assert_message="two_sum_dictionary({}, {}) returns {}. Expected {}".format(nums,target,result,expected_result)assertresult==expected_result,assert_messageprint("Passed all {} test cases".format(total_test_cases))

Hello @amsuarez ,

Thanks for adding a popular problem with easy explanation.

Here is my approach to solve the problem using Python.

I have included both brute force(O(n^2)), dictionary method and tested both method for several test cases:

Thanks for sharing love to see everyone's approach!