-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathDSA.js
3348 lines (2855 loc) Β· 71.1 KB
/
DSA.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// π 1) Reverse an array
function DSA1() {
const customReverse = (array) => {
let rightIndex = array.length - 1;
for (let leftIndex = 0; leftIndex < array.length / 2; leftIndex++) {
// swapping using array destucturing [ a, b ] = [ b, a ]
let temp = array[leftIndex];
array[leftIndex] = array[rightIndex];
array[rightIndex] = temp;
rightIndex--;
}
};
const numbers = [1, 2, 3, 4, 5, 6];
customReverse(numbers);
console.log(numbers);
// while Loop approach
const customReverse2 = (array) => {
let leftIndex = 0;
let rightIndex = array.length - 1;
while (leftIndex < rightIndex) {
let temp = array[leftIndex];
array[leftIndex] = array[rightIndex];
array[rightIndex] = temp;
leftIndex++;
rightIndex--;
}
};
}
// DSA1();
// π 2) Maximum and minimum of an array using minimum number of comparisons
// Input: arr = [3, 5, 4, 1, 9]
// Output: Minimum element is: 1
// Maximum element is: 9
function DSA2() {
const numbers = [3, 5, 4, 1, 9];
let max, min;
if (numbers[0] > numbers[1]) {
max = numbers[0];
min = numbers[1];
} else {
max = numbers[1];
min = numbers[0];
}
for (let i = 2; i < numbers.length; i++) {
if (max < numbers[i]) {
max = numbers[i];
}
if (min > numbers[i]) {
min = numbers[i];
}
}
console.log(min); // 1
console.log(max); // 9
// using Math.min and Math.max
console.log(Math.min(...numbers)); // 1
console.log(Math.max(...numbers)); // 9
// using Math and apply
console.log(Math.min.apply(null, numbers)); // 1
console.log(Math.max.apply(null, numbers)); // 9
}
// DSA2();
// π 3) Find the "Kth" max and min element of an array
// arr[] = 7 10 4 3 20 15
// K = 3;
// Output: kth Min - 7, kth Max - 10
function DSA3() {
const numbers = [7, 10, 4, 3, 20, 15];
function minMax(array, k) {
let min, max;
const sortedArray = array.slice().sort((a, b) => a - b);
min = sortedArray[k - 1];
max = sortedArray[array.length - k];
return { min, max };
}
const { min, max } = minMax(numbers, 3);
console.log(min); // 7
console.log(max); // 10
// simple selection sort simple
const array = [7, 10, 4, 3, 20, 15];
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] > array[j]) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
console.log("array", array);
}
// DSA3();
// π 4) Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order without sort.
// arr = [0, 2, 1, 2, 0]
// Output: [0, 0, 1, 2, 2]
function DSA4() {
const numbers = [0, 2, 1, 2, 0];
let zeroCount = 0;
let oneCount = 0;
let k = 0;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === 0) {
zeroCount++;
} else if (numbers[i] === 1) {
oneCount++;
}
}
for (let i = 0; i < zeroCount; i++) {
numbers[k++] = 0;
}
for (let i = 0; i < oneCount; i++) {
numbers[k++] = 1;
}
while (k < numbers.length) {
numbers[k++] = 2;
}
console.log(numbers);
}
// DSA4();
// π 5) Move all negative numbers to beginning and positive to end without extra space
// Input: [11, -13, -5, 6, -7, 5, -3, -6]
// Output: [-13, -5, -7, -3, -6, 11, 6, 5]
function DSA5() {
// positive number prr j point karenga swap negative number of i with j
// j flag will point to positive number and as soon as get any negative number in loop swap i postion value with
// j postion value and increment j if (value is negative)
const array = [11, -13, -5, 6, -7, 5, -3, -6];
const reArrange = (array) => {
let j = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] < 0) {
if (i !== j) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
j++;
}
}
};
reArrange(array);
console.log(array);
// easy way but not efficient
const arr = [11, -13, -5, 6, -7, 5, -3, -6];
let temp = [];
let k = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
temp[k++] = arr[i];
}
}
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 0) {
temp[k++] = arr[i];
if (k === arr.length) break;
}
}
console.log(temp);
}
// DSA5();
// π 6) Find the Union of two sorted arrays.
// arr1 = [1, 2, 3, 4, 5]
// arr2 = [1, 2, 3, 8, 9]
// output = [ 1, 2, 3, 4, 5, 8, 9 ]
function DSA6() {
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3, 8, 9];
// first, using set
console.log([...new Set([...arr1, ...arr2])]); // [ 1, 2, 3, 4, 5, 8, 9 ]
// second, using concat
const unionR = arr1.concat(
arr2.filter((num) => {
return !arr1.includes(num);
})
);
console.log("unionR", unionR); // [ 1, 2, 3, 4, 5, 8, 9 ]
// third, using while loop
let i = 0;
let j = 0;
let result = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
result.push(arr1[i]);
i++;
} else if (arr1[i] > arr2[j]) {
result.push(arr2[j]);
j++;
} else {
result.push(arr1[i]);
i++;
j++;
}
}
while (i < arr1.length) {
result.push(arr1[i]);
i++;
}
while (j < arr2.length) {
result.push(arr2[j]);
j++;
}
console.log("result", result); // [ 1, 2, 3, 4, 5, 8, 9 ]
}
// DSA6();
// π 6A) Find the Intersection of two sorted arrays.
// arr1 = [1, 2, 3, 4, 5]
// arr2 = [1, 2, 3, 8, 9]
// output = [ 1, 2, 3]
function DSA6A() {
// using filter
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [1, 2, 3, 8, 9];
const intersection = arr1.filter((num) => arr2.includes(num));
console.log("intersection", intersection);
// using while loop sorted array
let i = 0;
let j = 0;
let result = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
i++;
} else if (arr1[i] > arr2[j]) {
j++;
} else {
result.push(arr1[i]);
i++;
j++;
}
}
console.log("result", result); // [ 1, 2, 3]
// using map object
let map = {};
let common = [];
for (let num of arr1) {
map[num] = 1;
}
for (let num of arr2) {
if (map[num] === 1) {
map[num]++;
common.push(num);
}
}
console.log("common", common);
// unique from two arrays
const arr3 = [1, 2, 3, 4];
const arr4 = [1, 2, 5, 6];
// output : [ 3, 4, 5, 6 ]
const uniqueArr3 = arr3.filter((num) => !arr4.includes(num));
const uniqueArr4 = arr4.filter((num) => !arr3.includes(num));
console.log([...uniqueArr3, ...uniqueArr4]); // [ 3, 4, 5, 6 ]
// unique from two arrays [imp]
function uniqueNums() {
const arr1 = [1, 2, 3, 4];
const arr2 = [1, 2, 5, 6];
// output : [ 3, 4, 5, 6 ]
let i = 0;
let j = 0;
let result = [];
while (i < arr1.length && j < arr2.length) {
if (arr1[i] === arr2[j]) {
i++;
j++;
} else if (arr1[i] < arr2[j]) {
result.push(arr1[i]);
i++;
} else {
result.push(arr2[j]);
j++;
}
}
while (i < arr1.length) {
result.push(arr1[i]);
i++;
}
while (j < arr2.length) {
result.push(arr2[j]);
j++;
}
console.log(result);
}
uniqueNums();
}
// DSA6A();
// π 7) Write a program to cyclically rotate an array by one.
// arr1 = [1, 2, 3, 4, 5]
// output = [ 5, 1, 2, 3, 4 ]
function DSA7() {
const arr = [1, 2, 3, 4, 5];
const last = arr[arr.length - 1];
for (let i = arr.length - 1; i > 0; i--) {
arr[i] = arr[i - 1];
}
arr[0] = last;
console.log("arr", arr); // [ 5, 1, 2, 3, 4 ]
// using pop and shift
const arr1 = [1, 2, 3, 4, 5];
const lastNum = arr1.pop();
arr1.unshift(lastNum);
console.log("arr1", arr1);
}
// DSA7();
// π 8) find duplicate in an array
// arr1 = [1, 2, 2, 2, 3, 4, 4, 5]
// output = [ 2, 4 ]
function DSA8() {
// using object and for loop
const arr2 = [3, 4, 5, 1, 2, 4, 3, 5];
const obj2 = {};
const result1 = [];
for (let num of arr2) {
if (obj2[num]) {
if (!result1.includes(num)) result1.push(num);
} else {
obj2[num] = (obj2[num] || 0) + 1;
}
}
console.log("result1", result1);
// using two loop
const array = [1, 2, 2, 2, 3, 4, 4, 5];
const result = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] === array[j]) {
if (!result.includes(array[i])) {
result.push(array[i]);
}
break;
}
}
}
console.log("result", result);
// using sorting and one loop
const arr = [3, 4, 5, 1, 2, 4, 3, 5];
const sorted = [...arr.sort()];
const output = [];
for (let i = 0; i < sorted.length - 1; i++) {
if (sorted[i] === sorted[i + 1]) {
if (!output.includes(sorted[i])) {
output.push(sorted[i]);
}
}
}
console.log("output", output);
// using reduce count obj and Object.entries()
const arr1 = [3, 4, 5, 1, 2, 4, 3, 5];
const final = [];
const countObj = arr1.reduce((acc, curr) => {
return {
...acc,
[curr]: (acc[curr] || 0) + 1,
};
}, {});
for (let [num, count] of Object.entries(countObj)) {
if (count > 1) {
final.push(+num);
}
}
console.log("final", final);
}
// DSA8();
// π 9) Merge 2 sorted arrays without using Extra space ( memory )
// arr1 = [1, 2, 5, 6 ]
// arr2 = [ 3, 4, 7 ]
// output
// arr1 : [1, 2, 3, 4 ]
// arr2 : [ 5, 6, 7 ]
// [ 1, 2, 3, 4, 5, 6, 7]
function DSA9() {
const arr1 = [1, 2, 5, 6];
const arr2 = [3, 4, 7];
let m = arr1.length;
let n = arr2.length;
// first check if arr1[i] > arr2[0], swap num and sort arr2
for (let i = 0; i < m; i++) {
if (arr1[i] > arr2[0]) {
let temp = arr1[i];
arr1[i] = arr2[0];
arr2[0] = temp;
let j = 0;
while (j < n - 1 && arr2[j] > arr2[j + 1]) {
let temp = arr2[j];
arr2[j] = arr2[j + 1];
arr2[j + 1] = temp;
j++;
}
}
}
console.log("arr1", arr1); // [ 1, 2, 3, 4 ]
console.log("arr2", arr2); // [ 5, 6, 7 ]
console.log([...arr1, ...arr2]); // [ 1, 2, 3, 4, 5, 6, 7 ]
}
// DSA9();
// π 10) Merge Intervals
// Input: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
// Output: [ [1, 6], [8, 10], [15, 18]]
// https://www.youtube.com/watch?v=LvygwImtvEw
function DSA10() {
const intervals = [
[1, 3],
[2, 6],
[8, 10],
[15, 18],
];
intervals.sort((a, b) => a[0] - b[0]);
// 1) Using simple for loop
const result = [intervals[0]]; // [[1,3]]
for (let interval of intervals.slice(1)) {
e1 = result[result.length - 1][1]; // 3
s2 = interval[0]; // 2
e2 = interval[1]; // 6
if (e1 >= s2) {
// 3 >= 2
result[result.length - 1][1] = Math.max(e1, e2); // [[1,6]]
} else {
result.push(interval);
}
}
console.log("result", result);
// 2) Using Reduce
const intervals1 = [
[1, 3],
[2, 6],
[8, 10],
[15, 18],
];
intervals1.sort((a, b) => a[0] - b[0]);
const result1 = intervals1.reduce((acc, curr) => {
let last = acc.pop();
if (last) {
if (last[1] > curr[0]) {
let newLast = [last[0], curr[1]];
acc.push(newLast);
} else {
acc.push(last, curr);
}
} else {
acc.push(curr);
}
return acc;
}, []);
console.log("result", result1);
}
// DSA10();
// π 11) Count Inversion
/*
For an array, inversion count indicates how far (or close) the array is from being sorted.
If array is already sorted then the inversion count is 0. If an array is sorted in the reverse
order then the inversion count is the maximum
Input: N = 5, arr[] = {2, 4, 1, 3, 5}
Output: 3
Explanation: The sequence 2, 4, 1, 3, 5
has three inversions (2, 1), (4, 1), (4, 3).
*/
function DSA11() {
const array = [2, 4, 1, 3, 5];
let count = 0;
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[i]) {
count++;
}
}
}
console.log("output", count);
}
// DSA11();
// π 12) Best Time to Buy and Sell Stock:-
/*
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
*/
function DSA12() {
const prices = [7, 1, 5, 3, 6, 4];
let maxProfit = 0;
let lowestPrice = prices[0];
for (let i = 1; i < prices.length; i++) {
lowestPrice = Math.min(prices[i], lowestPrice);
maxProfit = Math.max(prices[i] - lowestPrice, maxProfit);
}
console.log("maxProfit", maxProfit);
// 2) Using two for loop
const arr = [7, 1, 5, 3, 6, 4];
let maxProfit1 = 0;
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] - arr[i] > maxProfit1) {
maxProfit1 = arr[j] - arr[i];
}
}
}
console.log(maxProfit1);
}
// DSA12();
// π 13) find all pairs on integer array whose sum is equal to given number
/*
arr[] = {1, 5, 7, 1}
Output: 2
Explanation:
arr[0] + arr[1] = 1 + 5 = 6
and arr[1] + arr[3] = 5 + 1 = 6.
*/
function DSA13() {
const arr = [1, 5, 7, 1];
let sum = 6;
let count = 0;
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] === sum) {
count++;
console.log(`[${arr[i]},${arr[j]}]`);
}
}
}
console.log("No. of Pairs", count);
// 2) Using hashMap
const hashMap = new Map();
const output = [];
for (let i = 0; i < arr.length; i++) {
let diff = sum - arr[i];
if (hashMap.has(diff)) {
output.push([hashMap.get(diff), arr[i]]);
}
hashMap.set(arr[i], arr[i]);
}
console.log(output, output.length);
// 3) Using one for loop and map
let result = [];
let map = new Map();
for (let num of arr) {
if (map.has(num)) {
let prevCount = map.get(num);
map.set(num, prevCount + 1);
} else {
map.set(num, 1);
}
}
for (let num of arr) {
if (map.has(sum - num)) {
map.delete(sum - num);
result.push([num, sum - num]);
}
}
console.log(result, result.length);
}
// DSA13();
// π 14) find common elements In 3 sorted arrays
/*
n1 = 6; A = {1, 5, 10, 20, 40, 80}
n2 = 5; B = {6, 7, 20, 80, 100}
n3 = 8; C = {3, 4, 15, 20, 30, 70, 80, 120}
Output: 20 80
Explanation: 20 and 80 are the only
common elements in A, B and C.
*/
function DSA14() {
const arr1 = [1, 5, 10, 20, 40, 80];
const arr2 = [6, 7, 20, 80, 100];
const arr3 = [3, 4, 15, 20, 30, 70, 80, 120];
let result = [];
let i = 0,
j = 0,
k = 0;
while (i < arr1.length && j < arr2.length && k < arr3.length) {
if (arr1[i] === arr2[j] && arr2[j] === arr3[k]) {
result.push(arr1[i]);
i++;
j++;
k++;
} else if (arr1[i] < arr2[j]) {
i++;
} else if (arr2[j] < arr3[k]) {
j++;
} else {
k++;
}
}
console.log("result", result); // [ 20, 80 ]
}
// DSA14();
// π 15) Rearrange array in alternating positive & negative items
/*
Input: arr[] = {1, 2, 3, -4, -1, 4}
Output: arr[] = {-4, 1, -1, 2, 3, 4}
*/
function DSA15() {
const arr = [1, 2, 3, -4, -1, 4];
const negative = [];
const positive = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
negative.push(arr[i]);
} else {
positive.push(arr[i]);
}
}
let i = 0;
let j = 0;
let k = 0;
while (i < positive.length && j < negative.length) {
if (k % 2 === 0) {
arr[k++] = positive[i++];
} else {
arr[k++] = negative[j++];
}
}
while (i < positive.length) {
arr[k++] = positive[i++];
}
while (j < negative.length) {
arr[k++] = negative[j++];
}
console.log("arr", arr); // [ 1, -4, 2, -1, 3, 4 ]
}
// DSA15();
// π 16) Find if there is any subarray with sum equal to 0
/*
Input: arr[] = 4 2 -3 1 6
Output: 2, -3, 1 is the subarray
with sum 0.
How to solve:-
4 2 -3 1 6
4 6 3 4<- while summing aray elements 4 is repeated means yes it has an subarray with sum equal to 0.
*/
function DSA16() {
// 1) Using map and sum
const arr = [4, 2, -3, 1, 6];
let map = new Map();
let sum = 0;
function checkSubArray() {
for (let i = 0; i < arr.length; i++) {
sum += arr[i];
if (arr[i] === 0 || map.has(sum) || sum === 0) {
return true;
}
map.set(arr[i]);
}
return false;
}
console.log("subArray with sum", checkSubArray(arr));
// 2) Using 3 for loop
const result = [];
for (let i = 0; i < arr.length - 1; i++) {
let sum = arr[i];
for (let j = i + 1; j < arr.length; j++) {
sum += arr[j];
if (sum === 0) {
let temp = [];
let start = i;
while (start <= j) {
temp.push(arr[start]);
start++;
}
result.push(temp);
}
}
}
console.log(result);
}
// DSA16();
// π 17) Find factorial of a large number
function DSA17() {
function factorial(num) {
if (num < 0) {
console.log("Please provide positive number");
return;
}
if (num === 0 || num === 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}
console.log(factorial(5));
}
// DSA17();
// π 18) find maximum product subarray
/*
Arr[] = [ 6, -3, -10, 0, 2 ]
Output: 180
solution :- posible sub array 10
[6,-3], [6,-3,-10], [6,-3,-10,0], [6,-3,-10,0,2] => i=0
[-3,-10], [-3,-10,0], [-3,-10,0,2] => i=1
[-10,0], [-10,0,2] => i=2
[0,2] => i=3
*/
function DSA18() {
const arr = [6, -3, -10, 0, 2];
function maxProduct(arr) {
let outMaxProd = arr[0];
for (let i = 0; i < arr.length - 1; i++) {
let innerProd = 1;
for (let j = i; j < arr.length; j++) {
innerProd *= arr[j];
if (innerProd > outMaxProd) {
outMaxProd = innerProd;
}
}
}
return outMaxProd;
}
console.log(maxProduct(arr));
}
// DSA18();
// π 19) Find longest coinsecutive subsequence
/*
a[] = {2,6,1,9,4,5,3}
Output:
6 => [1,2,3,4,5,6]
*/
function DSA19() {
const arr = [2, 6, 1, 3, 1, 3, 4, 7, 8, 9, 10, 11];
function longestCoinsecutive(arr) {
arr.sort((a, b) => a - b);
let longestSeq = [arr[0]];
let temp = [arr[0]];
for (let i = 1; i < arr.length; i++) {
if (arr[i] === arr[i - 1]) {
continue;
} else if (arr[i] === arr[i - 1] + 1) {
temp.push(arr[i]);
} else {
if (temp.length > longestSeq.length) {
longestSeq = temp;
temp = [arr[i]];
}
}
}
if (temp.length > longestSeq.length) {
longestSeq = temp;
}
return longestSeq;
}
console.log(longestCoinsecutive(arr));
}
// DSA19();
// π 20) Given Array of size n, find all elements that appear more than k times
/*
Input: arr[] = {3, 1, 2, 2, 1, 2, 3, 3}, k = 2
Output: {2, 3}
*/
function DSA20() {
const arr = [3, 1, 2, 2, 1, 2, 3, 3];
const k = 2;
const map = new Map();
const result = [];
for (let i = 0; i < arr.length; i++) {
if (map.has(arr[i])) {
map.set(arr[i], map.get(arr[i]) + 1);
} else {
map.set(arr[i], 1);
}
}
for (let [key, value] of map) {
if (value > k) {
result.push(key);
}
}
console.log("result", result);
}
// DSA20();
// π 21) Find whether an array is a subset of another array
/*
Input:
a1[] = {11, 1, 13, 21, 3, 7}
a2[] = {11, 3, 7, 1}
Output:
Yes
*/
function DSA21() {
const a = [11, 1, 13, 21, 3, 7];
const b = [11, 3, 7, 1];
function checkSubset(a, b) {
let map = new Map();
for (let i = 0; i < a.length; i++) {
if (!map.has(a[i])) {
map.set(a[i], a[i]);
}
}
for (let i = 0; i < b.length; i++) {
if (!map.has(b[i])) {
return false;
}
}
return true;
}
console.log(checkSubset(a, b));
function checkSubset2(a, b) {
let i = 0; // b=>[11, 3, 7, 1];
let j = 0; // a=>[11, 1, 13, 21, 3, 7];
for (i = 0; i < b.length; i++) {
for (j = 0; j < a.length; j++) {
if (b[i] === a[j]) {
break;
}
}
if (j === a.length) {
// element not found
return false;
}
}
return true;
}
console.log(checkSubset2(a, b));
}
// DSA21();
// π 22) Find the triplet that sum to a given value
/*
n = 6, sum = 13
arr[] = [1 4 45 6 10 8]
Output:
The triplet [1, 4, 8] in
the array sums up to 13.
*/
function DSA22() {
const arr = [1, 4, 45, 6, 10, 8];
const sum = 13;
// 1) Using 3 for loop - O(n3)
function findTriplet(arr, sum) {
let result = [];
for (let i = 0; i < arr.length - 2; i++) {
for (let j = i + 1; j < arr.length - 1; j++) {
for (let k = j + 1; k < arr.length; k++) {
if (arr[i] + arr[j] + arr[k] === sum) {
result.push([arr[i], arr[j], arr[k]]);
}
}
}
}
return result;
}
console.log(findTriplet(arr, sum));
// 2) Using 2 for loop and map - O(n2)time +O(n)space
const arr1 = [1, 4, 45, 6, 10, 8];
const sum1 = 13;
const result = [];
const map = new Map();
for (let num of arr1) {
if (!map.has(num)) {
map.set(num, num);
}
}
for (let i = 0; i < arr1.length - 1; i++) {
for (let j = i + 1; j < arr1.length; j++) {
let key = sum1 - (arr1[i] + arr1[j]);
if (map.has(key) && key !== arr1[j] && key !== arr1[i]) {
result.push([arr1[i], arr1[j], key]);
}
}
}
console.log(result);
// 3) Using sorting and 2 loops
function find3Numbers(A, arr_size, sum) {
let result = [];
let l, r;
A.sort((a, b) => a - b);
console.log(A);
for (let i = 0; i < arr_size - 2; i++) {
l = i + 1;
r = arr_size - 1;
while (l < r) {
if (A[i] + A[l] + A[r] == sum) {
result.push([A[i], A[l], A[r]]);