What's the most optimal algorithm for a negative positive addition to an array
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
精彩评论