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) function, 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. You are required to return only one such pair. If no such pair found then simply return the array.

Function 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.

Learn to code, grow your skills, and succeed in your tech interview