Daily Coding Problem 6MAY2020
I am subscribed to dailycodingproblem.com. From time to time I will share problems I attempt and have a discussion around it. My solution might have bugs or might not be the most efficient but it would be my attempt. Feel free to criticise.
Today's problem I got
This problem was recently asked by Google.
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
This was my solution in javascript. I am using the Javascript for Android app.
var map = {10:1,15:1,3:1,7:1};
var k=17;
var result=false;
for (m in map) {
if (map[k-m] == 1) result=true;
}
console.log(result);
I used a hash table (problem did not specify that an array had to be used) where the values were the keys and made one pass, looking for the difference. I would say the time complexity of this solution is O(n).
Comments