Number of distinct absolute values of sorted array elements

Posted by itungnt on Wednesday, March 9, 2016

function solution(A) {

// write your code in JavaScript (Node.js 4.0.0)

var count=0;l=0,r=A.length-1;

while(l <= r){ count++; if(Math.abs(A[l]) === Math.abs(A[r])){ l++; r–; } else if(Math.abs(A[l]) > Math.abs(A[r])){

l++;

}

else if(Math.abs(A[l]) < Math.abs(A[r])){ r–; } } return count; }


comments powered by Disqus