Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Effficent way to do sum and maximum of array in javascript

I am working on coding challenge where the scenario is let arr= [6,-2,9]

For day 1, there are no preceding days' information,

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.

For day 2, consider [6, -2] -> 6 + (-2) = 4, and [-2, 5] - (-2) + 5 = 3. The maximum of 3 and 4 is 4.

For day 3, consider [6, -2, 5] -> 6 + (-2)+5=9, and [5]. The maximum of 9 and 5 is 9.

Temperature changes before and after each day: Day 1: [-1], [-1, 2, 3] -> max(-1, 4) = 4

Day 2: [-1, 2], [2, 3] -> max(1, 5) =5

Day 3: [-1, 2, 3], [3] -> max(4, 3) = 4

Sum each array and take their maximum.

The maximum from 3 values is [4,5,4] is 5

tempChange = [6, -2, 5]
result
for (let i = 0; i < tempChange.length; i++) {

    if (i == 0) {
        let sum1 = tempChange[0]
        let sum2 = sumArray(tempChange)
        if (sum1 > 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 > 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("a1", splitArr1)
        console.log("a2", splitArr2)
        let sum1 = sumArray(splitArr1)
        let sum2 = sumArray(splitArr2)

        if (sum1 > sum2) {
            result.push(sum1)
        } else {
            result.push(sum2)
        }
    }
}

let finalResult = Math.max(...result)
function sumArray(arr) {
    const sum = arr.reduce((partialSum, a) => partialSum + a, 0);
    return sum
}

My solution is failing when the array is [ 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 9999999,9999999]

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

Any help will be appreciated

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm not sure I got the algorithm right from the question, but here's a snippet:

const nums1 = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000, 9999999, 9999999]
const nums2 = [6, -2, 9]
const nums3 = [6, -2, 5]
const nums4 = [-1, 2, 3]


const sum = (arr) => arr.reduce((a, c) => a + c, 0)
const calc = (nums) => nums.reduce((a, c, i, d) => {
  const s1 = sum(d.slice(0, i + 1))
  const s2 = sum(d.slice(i, d.length))
  const max = Math.max(s1, s2)
  if (a.max < max) a.max = max
  a.change.push(max)
  return a
}, {
  max: -Infinity,
  change: []
})

const nums1Calc = calc(nums1)
console.log(nums1Calc)
const nums2Calc = calc(nums2)
console.log(nums2Calc)
const nums3Calc = calc(nums3)
console.log(nums3Calc)
const nums4Calc = calc(nums4)
console.log(nums4Calc)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!