Challenge 3: Find Two Numbers that Add Up to Given Value
Given an array and a number "value", find two numbers from an array that sum to "value".
We'll cover the following
Problem statement
Implement a function findSum(int arr[], int value, int size)
, which takes an array arr
, a number value
, and 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 pair is found, then simply return the array.
Input
An array, value, and size of the array are given.
Output
The output 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
. 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 70+ hands-on prep courses.