title: Effficent way to do sum and maximum of array in javascript
body: <p>I am working on coding challenge where the scenario is
  let arr= [6,-2,9]</p>
  <p>For day 1, there are no preceding days' information,</p>
  <p>Temperature changes = [6] for the before period.
  For succeeding days, there are values [6,-2,5] and 6
  The maximum of 6 and 9 is 9.</p>
  <p>For day 2, consider [6, -2] -&gt; 6 + (-2) = 4, and [-2, 5] -
  (-2) + 5 = 3. The maximum of 3 and 4 is 4.</p>
  <p>For day 3, consider [6, -2, 5] -&gt; 6 + (-2)+5=9, and
  [5]. The maximum of 9 and 5 is 9.</p>
  <p>Temperature changes before and after each day:
  Day 1: [-1], [-1, 2, 3] -&gt; max(-1, 4) = 4</p>
  <p>Day 2: [-1, 2], [2, 3] -&gt; max(1, 5) =5</p>
  <p>Day 3: [-1, 2, 3], [3] -&gt; max(4, 3) = 4</p>
  <p>Sum each array and take their maximum.</p>
  <p><strong>The maximum from 3 values is [4,5,4]  is 5</strong></p>
  <pre><code>tempChange = [6, -2, 5]
  result
  for (let i = 0; i &lt; tempChange.length; i++) {
  
      if (i == 0) {
          let sum1 = tempChange[0]
          let sum2 = sumArray(tempChange)
          if (sum1 &gt; sum2) {
              result.push(sum1)
          } else {
              result.push(sum2)
          }
      } else if (i == tempChange.length - 1) {
          let sum1 = tempChange[tempChange.length]
          let sum2 = sumArray(tempChange)
          if (sum1 &gt; sum2) {
              result.push(sum1)
          } else {
              result.push(sum2)
          }
      } else {
          let splitArr1 = tempChange.slice(0, i + 1)
          let splitArr2 = tempChange.slice(i, tempChange.length + 1)
  
          console.log(&quot;a1&quot;, splitArr1)
          console.log(&quot;a2&quot;, splitArr2)
          let sum1 = sumArray(splitArr1)
          let sum2 = sumArray(splitArr2)
  
          if (sum1 &gt; sum2) {
              result.push(sum1)
          } else {
              result.push(sum2)
          }
      }
  }
  
  let finalResult = Math.max(...result)
  function sumArray(arr) {
      const sum = arr.reduce((partialSum, a) =&gt; partialSum + a, 0);
      return sum
  }
  </code></pre>
  <p>My solution is failing when the array is [ 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 9999999,9999999]</p>
  <pre><code>Time limit exceeded
  Allowed time limit:10 secs
  Your code did not execute within the time limits. Please optimize your code. For more information on execution time limits, refer to the environment page
  </code></pre>
  <p>Any help will be appreciated</p>