main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
// Please write a function (in either PHP or Javascript) that takes an array of integers as an argument, and efficiently returns the sum of the two largest elements in the array. For example, if the array passed in is [3, 17, 8, -20, 0], the return value should be 25. (17 + 8)

// Keep in mind that the array can be made up of any valid integers, and that the length of the array is unknown (but could potentially be very large).

function tempestFunction($arr) {
  $highestVal = max($arr);
  unset($arr[array_search($highestVal, $arr)]);
  $secondHighestVal = max($arr);

  return $highestVal + $secondHighestVal;
}

echo tempestFunction([3, 17, 8, -20, 0]).PHP_EOL;
PHP 7.0.8 (cli) (built: Jun 23 2016 23:39:14) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies