Skip to content

Commit dd32f2f

Browse files
committed
learning about arrays
1 parent bfd0a7d commit dd32f2f

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

Diff for: day3.js renamed to Array/day3.js

File renamed without changes.

Diff for: Array/day4.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// ! Day 4 of learning DSA in
2+
3+
// Todo: Array Data Starcher
4+
5+
// * Q1, Second Largest Number
6+
// * Given an array Arr of size of N Print the Second largest Number
7+
8+
// ? Example
9+
// * Input --> [3,5,6,7,89,33,143]; ---> output: 89
10+
11+
// * The Time Complexity of This Function is O(n log n) (bad Time Time complexity)
12+
function FindSecond(array) {
13+
let newArray = Array.from(new Set(array));
14+
newArray.sort((a, b) => {
15+
return b - a;
16+
});
17+
if (newArray.length > 2) {
18+
return newArray[1];
19+
} else {
20+
return -1;
21+
}
22+
}
23+
const arr = [3, 5, 6, 7, 89, 33, 143];
24+
console.log(FindSecond(arr));
25+
26+
// * The Time Complexity of This Function is O(n) (Liner Search) (bad Time Time complexity)
27+
function Optimise(array) {
28+
let largest = Number.NEGATIVE_INFINITY;
29+
let SLargest = Number.NEGATIVE_INFINITY;
30+
31+
for (let i = 0; i < array.length; i++) {
32+
if (array[i] > largest) {
33+
SLargest = largest;
34+
largest = array[i];
35+
} else if (array[i] != largest && array[i] > SLargest) {
36+
SLargest = array[i];
37+
}
38+
}
39+
return SLargest;
40+
}
41+
console.log(Optimise(arr));
42+
43+
console.clear();
44+
45+
// * Q2, Rotate Array by K
46+
// * Given an integer array num rotate the array to the right by K steps
47+
48+
// ? Example
49+
// * Input --> [1,2,3,4,5,6,7], k = 3, Output --> [5,6,7,1,2,3,4]
50+
// * [1,2,3,4,5,6,7] -> [7,1,2,3,4,5,6] -> [6,7,1,2,3,4,5] -> [5,6,7,1,2,3,4]
51+
52+
// Todo: My Solution (Wrong)
53+
function rotateArray(arr, k) {
54+
if (k > arr.length) {
55+
return -1;
56+
} else {
57+
for (let i = 0; i < k; i++) {
58+
var Array = [];
59+
Array.unshift(arr[arr.length - 1], ...arr);
60+
arr.pop();
61+
}
62+
return Array;
63+
}
64+
}
65+
66+
// *
67+
function rotateArray1(arr, k) {
68+
let size = arr.length;
69+
if (size > k) {
70+
k = k % size;
71+
}
72+
const rotated = arr.splice(size - k, size);
73+
arr.unshift(...rotated);
74+
return arr;
75+
}

Diff for: day2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Todo: Topic 1 (Big O Notation)
44

5-
// Todo: O(n) -> ("signifies that the execution time of the algorithm grows linearly in proportion to the size of the input data(n) ") (Linear Search) (OK Time Complexity)
5+
// Todo: O(n) -> ("signifies that the execution time of the algorithm grows linearly in proportion to the size of the input data(n) ") (Linear Search) (Good Complexity)
66

77
const groceryList = ["Milk", "Eggs", "Bread", "Butter", "Cheese", "Tomatoes"];
88

0 commit comments

Comments
 (0)