Problem:
Return true if the sum of two different elements of the array equals the target, otherwise return false
I want to optimize the time complexity of this code.
Now, code has O(n^2) complexity. How can I reduce complexity?
input is unsorted array(number[]
) and target(number
), output is true or false.
Here`s my code.
function find(arr, target) {
for(let i = 0; i < arr.length; i++){
for(let j = i + 1; j < arr.length; j++){
if(target === (arr[i]+arr[j])){
return true;
}
}
}
return false;
}
I think hint is unsorted
array. And I don`t know at all..