Dot Net For All

Daily Coding Problem – 1 in Java and C#

Hello Friends, as part of this series I have solved an algorithm problem asked in major tech companies. The solution is covered in C# and Java.

Problem 1

Given a list of numbers, return whether any two sums 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?

Solution in C#

Below is the solution for the problem in C#. In the method we have created a temporary array list. The array list contains the array element, if not we will save the difference of Sum and the array element

For example, in our case the sum is 17. In first pass we will check 10 is there is the array list, since it is not there we will save the difference 17 – 10 = 7. And when the pointer comes to 7, it will be there in the temporary array. This will help us to find if two elements adds to the input sum in an array.

Solution in Java:

If any Java lover out there, below is the Daily Coding Problem 1 solution in Java. Though the logic is same as above C# code.

Conclusion

The Daily Coding Problem – 1 has tried to test the working of arrays and basic mathematics and logical skill of the participant.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview