Given an array x of size n > 3 and an integer N, return the indices of three elements in the array that sum up to the given integer N. If there is no such triplet, return -1.
For example, given x = [1 4 2 5 8 10] and N = 17, the function should return [2 4 5] because x(2) + x(4) + x(5) = 4 + 5 + 8 = 17. With the same x but N = 24, the function should return -1 because there is no triplet whose sum equals 24.
Note that the list of three indices is not necessarily unique. For example, given x = [1 2 3 4 5 6] and N = 9, the expected output can be either [1 2 6] or [1 3 5] since x(1) + x(2) + x(6) = 1 + 2 + 6 = 9 and x(1) + x(3) + x(5) = 1 + 3 + 5 = 9. The tests simply verify that the sum of the elements at the found indices is correct.
Solution Stats
Solution Comments
Show comments
Loading...
Problem Recent Solvers10
Suggested Problems
-
23735 Solvers
-
Calculate the area of a triangle between three points
3422 Solvers
-
Remove from a 2-D matrix all the rows that contain at least one element less than or equal to 4
139 Solvers
-
Rotate input square matrix 90 degrees CCW without rot90
682 Solvers
-
Sum the numbers on the main diagonal
613 Solvers
More from this Author53
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!