An Interest In:
Web News this Week
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
April 3, 2022 07:47 am GMT
Original Link: https://dev.to/urstrulyvishwak/sumzero-with-on-in-javascript-2126
sumZero with O(n) in Javascript.
Given a sorted array. Write a function sumZero and return the first found pair otherwise undefined.
cases:
sumZero([-3,-2,-1,0,1,2,3]); // [-3,3];
sumZero([0,1,2,3]); // undefined
sumZero([-3,1,2]); // undefined
function sumZero(arr) { let left = 0; let right = arr.length - 1; while (left < right) { const result = arr[left] + arr[right]; if (result === 0) return [arr[left], arr[right]]; result < 1 ? left++ : right--; }}
Thanks.
Love to see your response
- Like - You reached here means. I think, I deserve a like.
- Comment - We can learn together.
- Share - Makes others also find this resource useful.
- Subscribe / Follow - to stay up to date with my daily articles.
- Encourage me - You can buy me a Coffee
Let's discuss further.
- Just DM @urstrulyvishwak
Or mention
@urstrulyvishwak
For further updates:
Original Link: https://dev.to/urstrulyvishwak/sumzero-with-on-in-javascript-2126
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To