-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.js
More file actions
38 lines (31 loc) · 971 Bytes
/
array.js
File metadata and controls
38 lines (31 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
let arr=[2,7,11,15]
function twosum(arr,target =6)
{
// convert to sorted array in a way the order of the indexes are preserved
// let sortedArr = arr.sort((a,b) => a-b)
let result = []
for(let i=0; i<arr.length; i++){
result.push([i,arr[i]])
}
result =result.sort((a,b) => a[1] - b[1])
console.log('result' , result);
let left = 0
let right = result.length -1
// 1,2,3,4
// 4,1,0,2
while (left < right) {
let res = result[left][1] + result[right][1]
if (res == target) {
return [
Math.min(result[left][0],result[right][0]),
Math.max(result[left][0],result[right][0])
]
} else if(res > target){
right -=1
}else{
left+=1
}
}
return []
}
console.log(twosum(arr));