Sum of Three Values
Given an array of integers and a value, determine if there are any three integers in the array whose sum equals the given value.
We'll cover the following...
We'll cover the following...
Statement
Given an array of integers and an integer value, determine if there are any three integers in the array whose sum equals the given value. Return true if three such integers from the array are found. Otherwise, return false.
Example
Consider this array and the target sums:
Sample input
([3, 7, 1, 2, 8, 4, 5], 20)
Expected output
true
Try it yourself
Solution 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’s not efficient.
There exist other solutions with much better time complexities. We’ll look at them later in this lesson.
Time complexity
The time complexity of this solution is cubic, ...