contact@bryanmorfe.com
Bryan Morfe

Challenge 1: Two Numbers That Add Up To a Sum

Share

4 shares

Challenge 1: Two Numbers That Add Up To a Sum

Can you solve this challenge efficiently? Find two numbers in an array that add up to a given number. Submit your solutions and gain recognition.

Bryan Morfe Bryan Morfe Aug 26, 2019

23

4

Welcome To The First Challenge

Hey! I know I have been out for almost two months. I apologize. I've been quite busy with work-related matters, but the good news is that I am back and I will try my best to not let it go this long again!

Why Challenges?

I bring you the first challenge ever posted on this blog. I know that I have references to many GREAT sites for coding challenges, including HackerRank, Leetcode, and others. However, in this opportunity, I want to bring my own challenges (even if you can find them on those sites) so that we can share different solutions and analyze them together. The good thing about this is that we can all improve–as programmer and problem solvers–because we can collectively review each other's codes, and make suggestions. The best solutions will be posted in a separate page with the names of the authors, as well as the name of anyone that contributed to improve the solution.

Sending Solutions

As you may have seen in the title, this challenge is called challenge "1". When solutions are available, a separate post titled "Solutions to Challenge 1" will contain all the solutions and reviews of the solutions. Not all solutions will make it to the post, but yours might! When solutions are available, a link at the end of each challenge will be provided. If you have solutions, you may post them in your comments, or you can email them directly to solutions@bryanmorfe.com with the subject "Solution to Challenge X". Make sure to replace X with the number of the challenge. In the future, a solution submission board will be provided. You may email the source code in plain text or in a text file. Please do not send compressed files. Please test your solutions before submission. Please specify the specific programming language, version/environment used to be able to accurately retest your solution.

You may use any programming language for your solution. However, note that judgement of the solution depends on the language used as some solutions seemly equal may vary in efficiency depending on the internals of the programming language used.

Also, multiple solutions are accepted. If you previously submitted, then resubmission could help the chances of getting your newer solution posted.

Sending Challenges

Do you have any interesting challenges that think you we might want to solve? Great! Please email them to me at challenges@bryanmorfe.com with the subject "I've Got A New Challenge", and it might get posted! The more challenges we post, the better we become! Challenges don't necessarily have to be for programming, it could be an interesting brainteaser that might be asked in an interview. Also, the challenge could cover topics such as Data Structures, Algorithms, Big O, System Design, etc.

This Challenge

The Problem

You are given an array of numbers–integers–and a separate integer called sum. You must write a function that identifies if any two integers (different indices) in the array add up to sum. The array is not guaranteed to be sorted. The function must return an array with the indices of the two elements that add up to sum, and in the event that no two elements add up to sum, then return an empty array.

Examples

  1. Array = {4, 2, 5, 1}, sum = 5, then your function must return {0, 3} because 4 + 1 = 5.
  2. Array = {2, 2, 4, 0}, sum = 4, then your function must return either {0, 1} because 2 + 2 = 4 or {2, 3} because 4 + 0 = 4.

Last Thought

There is a linear solution to this problem O(n), but even if you cannot find a linear solution, feel free to share your solution as it may have some other efficiency that could be applied to more efficient solutions. With that being said, you should try to solve the problem to the best of your abilities. If a better solution is found after initial submission, you are welcome to resubmit.

Also, please leave a comment letting me know what you think of the challenge, and even to make improvements to the challenge itself.

23

4

Subscribe To My Newsletter