Challenge: Find Two Numbers that Add up to "n"
Given an array and a number "n", find two numbers from the array that sums up to "n". Implement your solution in Java and see if your output matches with the correct output.
We'll cover the following
Problem Statement
In this problem, you have to implement the int[] findSum(int[] arr, int n)
method, which will take a number n
, and an array arr
as input and returns an array of two integers that add up to n
in an array. In case there is more than one pair in the array containing numbers that add up to n
, you are required to return only one such pair. If no such pair is found then return an empty array.
Method Prototype
int[] findSum(int[] arr, int n)
Output
An array with two integers a
and b
that add up to a given number.
Sample Input
arr = {1, 21, 3, 14, 5, 60, 7, 6}
value = 27
Sample Output
arr = {21, 6} or {6, 21}
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.