开发者

What's the most optimal algorithm for a negative positive addition to an array

开发者 https://www.devze.com 2022-12-07 19:56 出处:网络
Good evening. I had a coding interview on Codesignal with the question 开发者_高级运维below, and I got just 14/20 with the test cases. How will you solve it please.

Good evening. I had a coding interview on Codesignal with the question 开发者_高级运维below, and I got just 14/20 with the test cases. How will you solve it please.

Given an array of nums, add positive and negative in succession and return the sum.

Example : given nums = {2, 3, 4, 5, 7} Answer = 2-3+4-5+7 = 5.

What's the fastest algorithm for this?

I tried to use a two for loops and input -ve with i+1 for the second loop, but that's just brute force and terribly slow

0

精彩评论

暂无评论...
验证码 换一张
取 消