-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEG-13_Sprint-1_PS.js
More file actions
143 lines (120 loc) · 3.59 KB
/
Copy pathEG-13_Sprint-1_PS.js
File metadata and controls
143 lines (120 loc) · 3.59 KB
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
// 01. Check if a Year is a Leap Year
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
console.log("1. Leap Year:", isLeapYear(2024));
// 02. Generate Fibonacci Sequence Up to N Terms
function generateFibonacci(n) {
if (n <= 0) return [];
if (n === 1) return [0];
const fib = [0, 1];
for (let i = 2; i < n; i++) {
fib.push(fib[i - 1] + fib[i - 2]);
}
return fib;
}
console.log("02. Fibonacci Sequence:", generateFibonacci(7));
// 03. Calculate the Greatest Common Divisor (GCD)
function findGCD(a, b) {
let x = Math.abs(a);
let y = Math.abs(b);
while (y) {
const temp = y;
y = x % y;
x = temp;
}
return x;
}
console.log("3. GCD:", findGCD(48, 18));
// 04. Calculate the Least Common Multiple (LCM)
function findLCM(a, b) {
if (a === 0 || b === 0) return 0;
return Math.abs(a * b) / findGCD(a, b);
}
console.log("4. LCM:", findLCM(12, 18));
// 05. Check if a Number is Prime
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
}
return true;
}
console.log("5. Prime Check:", isPrime(29));
// 06. Merge Two Sorted Arrays into One Sorted Array
function mergeSortedArrays(arr1, arr2) {
const merged = [];
let i = 0;
let j = 0;
while (i < arr1.length && j < arr2.length) {
if (arr1[i] <= arr2[j]) {
merged.push(arr1[i]);
i++;
} else {
merged.push(arr2[j]);
j++;
}
}
while (i < arr1.length) {
merged.push(arr1[i]);
i++;
}
while (j < arr2.length) {
merged.push(arr2[j]);
j++;
}
return merged;
}
console.log("6. Merge Sorted Arrays:", mergeSortedArrays([1, 3, 5], [2, 4, 6]));
// 07. Find the Median of an Unsorted Array
function findMedian(nums) {
if (!nums || nums.length === 0) return 0;
const sorted = [...nums].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
if (sorted.length % 2 !== 0) {
return sorted[mid];
}
return (sorted[mid - 1] + sorted[mid]) / 2;
}
console.log("7. Median:", findMedian([7, 1, 3, 4, 9]));
// 08. Find the Second Largest Number in an Array
function findSecondLargest(nums) {
let max = -Infinity;
let secondMax = -Infinity;
for (const num of nums) {
if (num > max) {
secondMax = max;
max = num;
} else if (num > secondMax && num < max) {
secondMax = num;
}
}
return secondMax === -Infinity ? null : secondMax;
}
console.log("8. Second Largest:", findSecondLargest([10, 20, 4, 45, 99, 99]));
// 09. Find Most Frequent Element (Mode) in an Array
function findMode(arr) {
if (!arr || arr.length === 0) return null;
const frequencyMap = new Map();
let maxCount = 0;
let mode = null;
for (const item of arr) {
const count = (frequencyMap.get(item) || 0) + 1;
frequencyMap.set(item, count);
if (count > maxCount) {
maxCount = count;
mode = item;
}
}
return mode;
}
console.log("9. Mode Value:", findMode([1, 3, 3, 2, 1, 3, 4]));
// 10. Natural Sorting of Strings with Embedded Numbers
function naturalSort(arr) {
return [...arr].sort((a, b) =>
a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })
);
}
console.log("10. Natural Sort:", naturalSort(["file10.txt", "file2.txt", "file1.txt"]));