Select Git revision
-
Brady James Garvin authoredBrady James Garvin authored
sumToN.js 943 B
export function findElementsSummingTo(list, sum) {
// INSTRUCTIONS: For the functional correctness points, complete this
// JavaScript function so that it finds the two elements of `list` whose total
// is `sum` and returns those two elements in a sorted list. You may assume
// that the input `list` is sorted and that there is always exactly one pair
// of (not necessarily distinct) elements with the correct total.
//
// For the performance points, modify the search so that it runs in linear
// time. Consider that, while a straightforward exhaustive search will need
// Θ(n²) time, your generate step does not need to consider all possible pairs
// because the list is presorted. Instead, you can start with the elements at
// the first and last indices in the list and then, if the check step fails,
// adjust the indices you are looking at for the next iteration based on how
// the check step failed.
}