forked from Prakshalshah78/DSA_Relevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_06.js
80 lines (71 loc) · 1.28 KB
/
10_06.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//unique
function unique(arr){
let ans = arr[0];
for(let i=1;i<arr.length;i++){
ans = ans ^arr[i];
}
return ans;
}
let arr = [3,2,"1",2,"3"]
//some elem ^ 0
console.log(unique(arr))
//subArray
function subArray(n){
for(i=0;i<n;i++){
for(j=i;j<n;j++){
for(k=i;k<=j;k++){
console.log(arr[k])
}
console.log("\n")
}
}
}
arr=[1,2,3,4]
subArray(arr.length)
//findIntersaction
function findIntersaction(arr1,arr2){
let i=0;j=0;
while(i<arr1.length&&j<arr2.length){
if(arr1[i]<arr2[j]){
i++;
}
else if(arr2[j]<arr1[i]){
j++
}
else{
console.log(arr2[j])
i++;
j++;
}
}
}
let arr1=[1,2,3,3,4,5,6]
let arr2=[3,3,6]
findIntersaction(arr1,arr2)
//===Triplets=======
function findTriplets(arr,target){
let l,r;
arr.sort((a,b)=>a-b);
for(let i=0;i<arr.length;i++){
l=i+1;
r=arr.length-1;
while(l<r){
if(arr[i]+arr[l]+arr[r]==target){
console.log(arr[i],arr[l],arr[r])
return true;
}
else if(arr[i]+arr[l]+arr[r]<target){
l++;
}
else{
r--;
}
}
}
return false;
}
let arr=[1,2,5,6,7,8,3]
findTriplets(arr,8)
//arr=[1,2,3,5,6,7,8]
VM36:10 1 2 5
true