Bloomberg Interview Question

Given a sum, find two numbers in an array with that sum.

Interview Answers

Anonymous

Jun 17, 2015

Using JS: nums = [2,13,61,512,3,6,7,88,17] sum = 15 for(var i = 0; i -1) { return nums[match]; } }

Anonymous

Aug 3, 2015

The other answer works, but is not efficient (It is O(n^2)) To make it O(n), simply make a hashmap. For every number, check to see if the number exists in the hashmap, otherwise store [sum-currentNumber]

Anonymous

Mar 29, 2020

Please find the answer below which I compiled now, //Php Program $array = [3, 5, 2, -4, 8, 11]; $numberToSum = 7; $length = count($array); $arrayData = []; for($j=0;$j<$length;$j++){ for($i=$j+1;$i<$length;$i++){ if($array[$j] + $array[$i] == $numberToSum){ $arrayData[] = [$array[$j],$array[$i]]; } } } print_r($arrayData);