Sum of Three Values - Coderust_ Hacking the Coding Interview
Sum of Three Values - Coderust_ Hacking the Coding Interview
• Description
• Hints
• Try it yourself
• Solution 1
• Runtime complexity
• Memory complexity
• Solution 2
• Runtime complexity
• Memory complexity
• Solution 3
• Runtime complexity
• Memory complexity
Description #
Given an array of integers and a value, determine if there are any three
integers in the array whose sum equals the given value.
3 7 1 2 8 4 5
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 1/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
Target Sum 20 5 + 7 + 8 = 20
Hints #
Sort data
Iterate from both ends
Try it yourself #
C++ Java Python JS Ruby
return false;
}
Solution 1 #
Runtime complexity #
The runtime complexity of this solution is cubic, O(n3 ).
Memory complexity #
The memory complexity of this solution is constant, O(1).
The simple and naive solution is to have three nested loops iterate over all
unordered triples to see if their sum is equal to the given integer or not.
Although this works, it is not efficient. The second and third solutions
have much better time complexities.
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 2/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
int main(){
vector<int> arr = {3, 7, 1, 2, 8, 4, 5};
Solution 2 #
Runtime complexity #
The runtime complexity of this solution is O(n2 logn).
Memory complexity #
The memory complexity of this solution is constant, O(1).
This solution is also simple. We first sort the input array in O(nlogn)
time.
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 3/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
Then we iterate over each pair (a, b) in the array in a nested loop and
calculate the remaining sum (sum - (a + b)) . We try to find the
remaining sum in the array using binary search. If we find it, we have
found the solution, with the three numbers being (a, b) and (sum -
(a+b)) .
return low;
}
if (k != i && k != j) {
return true;
}
}
}
}
return false;
}
int main(){
vector<int> arr = {3, 7, 1, 2, 8, 4, 5};
Solution 3 #
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 4/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
Runtime complexity #
The runtime complexity of this solution is quadratic, O(n2 ).
Memory complexity #
The memory complexity of this solution is constant, O(1).
In this solution, we first sort the array. Then we fix one element e , and try
to find a pair (a, b) in the remaining array such that required_sum - e =
a + b.
We start with the first element e in the array (at index i = 0) and try to
find such a pair (a, b) in the remaining array (i.e., A[i + 1] to A[n - 1] )
that satisfies the condition: a+b = required_sum - e . We can find such a
pair in linear time. If we find such a pair, we have found the solution: a, b
and e and thus, we can stop the iteration. Otherwise, we repeat the above
steps for all elements e at index i = 1 to n - 3 , until we find a pair that
meets the condition.
Let’s see how this algorithm works for the above example:
Initial State
3 7 1 2 8 4 5
Value 20
Remaining Sum
1 of 8
Additional Thoughts:
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 5/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
By sorting the array, we have to change the input array itself. If we are not
allowed to modify the input array, then we can use a hash table to achieve
the same time complexity (see the first solution of Sum of Two Values
problem). However, this will require O(n) of extra memory.
return false;
}
std::sort(arr.begin(), arr.end());
return false;
}
int main(){
vector<int> arr = {3, 7, 1, 2, 8, 4, 5};
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 6/7
02/11/2020 Sum of Three Values - Coderust: Hacking the Coding Interview
Back Next
Mark as Completed
23% completed, meet the criteria and claim your course certi cate!
Buy Certificate
Ask a Question
Report an
(https://discuss.educative.io/tag/sum-of-three-values__miscellaneous__coderust-
Issue
hacking-the-coding-interview)
https://www.educative.io/courses/coderust-hacking-the-coding-interview/k6Xx 7/7