Skip to main content

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

Popular posts from this blog

Drums of freedom

It is 401am and I have decided to write. This Saturday, Trinidad and Tobago observes African Emancipation Day. It is a day to celebrate the journey to freedom and to honor those who sacrificed so much so that we could enjoy the freedoms we have today. Yet the journey is not over. As we celebrate how far we have come, let us also ask God to open our minds and hearts to the struggles that continue. Freedom is never something we should take for granted. Every day, we witness powerful interests around the world attempting to stifle freedom in pursuit of their own agendas and self-aggrandizement. Too often, they seek to divide in order to conquer. May we resist division with wisdom, courage, and compassion. May unity triumph over hatred, truth over deception, and love over fear. As we remember the past, may we also commit ourselves to protecting the freedoms of the present and building a more just future for generations to come. Happy African Emancipation Day. May God continue to guide us o...

Positive vibration

It was 422pm and I decided to write. There was a peace in my heart that could only come from God. But what should I write about? "God, give me a sign. God, give me direction. Place a topic on my heart. Open my eyes and my ears to Your leading." The next morning, I woke up with a positive vibration. My heart felt light, and my mind was clear. It got me thinking. Sometimes, we simply need to clear our minds of negativity. We need to stop feeding fear, doubt, and discouragement, and instead make room for hope, faith, and positive possibilities. We need to reset our minds, renew our spirits, and re-energize our hearts. Then, we can return stronger to face whatever challenges lie ahead. If something is within our control, let us do our best to improve it. If it is beyond our control, let it go and let God. Life will always have difficulties, but we have a choice in how we respond to them. We can choose to see opportunities where others see obstacles. We can find lessons in our set...

Foundation of my belief

It is 159am and I have decided to write. Some people say that science does not point to an afterlife, heaven or the existence of God. I feel bad because I cannot give them the evidence that they want. I can point them to the Quran and Islamic teachings. The Quran is a miracle and there are signs in that which we can research. Otherwise, there are philosophical and theological arguments. Maybe, you cannot use the science that we have for something (the afterlife) that is outside the realm of science. Also, science is not complete and may never be complete. Scientific knowledge is expanding and sometimes changing, so are we going to wait on science to tell us that there is a God and afterlife? Belief does not need evidence. Faith does not need evidence. At least not the evidence that some are looking for. So what is the foundation of my faith? For some people theology does not fully explain this life so they look to the science we have. For me, science alone does not explain this life, s...