Challenge 10: Find Two Numbers that Add Up to "Value"
Given an array and a “value,” find two numbers from the array that sum to “value.”
We'll cover the following
Problem statement
Implement a function int[] findSum(int[] arr, int sum, int size)
which takes an array arr
, a number value
, size
of the array as input, and returns an array of two numbers that add up to value
. In case there is more than one pair in the array containing numbers that add up to value
, you are required to return only one such pair. If no such pairs are found, then simply return the array.
Input
This is an array, value, and size of the array.
Output
This is an array with two integers that add up to value
.
Sample input
arr = [1,21,3,14,5,60,7,6]
value = 81
Sample output
arr = [21,60]
For example, in this illustration, you are given 81 as the number value
and when you traverse the whole array you 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.