Challenge: Find Two Numbers That Add Up to n
Solve the challenge of finding two numbers that add up to the target number n.
We'll cover the following
Problem statement
In this problem, you have to implement the FindSum(int[] arr, int n)
method, which will take an array arr
and number n
as inputs and return two numbers from the list that add up to n
.
Input
An array and a number n
.
Output
An array with two integers a
and b
that add up to n
.
Sample input
arr = {1, 21, 3, 14, 5, 60, 7, 6};
n = 81;
Sample output
result = {21, 60};
For example, in this illustration, we are given 81
as the number n
, and when we traverse the whole array, we find that 21
and 60
are the integers that add up to 81
.
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.