@@ -33,7 +33,7 @@ console.log(isPalindrome("racecar")); // Output: true
33
33
``` js
34
34
function binarySearch (arr , target ) {
35
35
let left = 0 ,
36
- right = arr .length - 1 ;
36
+ right = arr .length - 1 ;
37
37
while (left <= right) {
38
38
const mid = Math .floor ((left + right) / 2 );
39
39
if (arr[mid] === target) return mid;
@@ -46,7 +46,7 @@ function binarySearch(arr, target) {
46
46
console .log (binarySearch ([1 , 2 , 3 , 4 , 5 ], 4 )); // Output: 3
47
47
```
48
48
49
- ** Explanation** : Efficiently searches for a target in a sorted array using a divide-and-conquer approach.
49
+ ** Explanation** : Searches for a target in a sorted array using a divide-and-conquer approach (Time complexity: O(log n)) .
50
50
51
51
## 4. Fibonacci Sequence (Recursive)
52
52
@@ -61,7 +61,7 @@ console.log(fibonacci(6)); // Output: 8
61
61
62
62
** Explanation** : Generates the nth Fibonacci number recursively by summing the two preceding numbers.
63
63
64
- ⚠️ ** Note** : This approach has ** exponential time complexity O(2< sup >n</ sup >) ** and is inefficient for large inputs. Consider memoization or iteration for better performance.
64
+ ⚠️ ** Note** : This approach has exponential time complexity O(2^n) and is inefficient for large inputs. Use memoization or iteration for better performance.
65
65
66
66
## 5. Factorial of a Number
67
67
@@ -108,9 +108,7 @@ console.log(findMax([1, 2, 3, 4, 5])); // Output: 5
108
108
109
109
``` js
110
110
function mergeSortedArrays (arr1 , arr2 ) {
111
- let merged = [],
112
- i = 0 ,
113
- j = 0 ;
111
+ let merged = [], i = 0 , j = 0 ;
114
112
while (i < arr1 .length && j < arr2 .length ) {
115
113
if (arr1[i] < arr2[j]) {
116
114
merged .push (arr1[i]);
@@ -126,7 +124,7 @@ function mergeSortedArrays(arr1, arr2) {
126
124
console .log (mergeSortedArrays ([1 , 3 , 5 ], [2 , 4 , 6 ])); // Output: [1, 2, 3, 4, 5, 6]
127
125
```
128
126
129
- ** Explanation** : Combines two sorted arrays into one sorted array by comparing elements sequentially.
127
+ ** Explanation** : Merges two sorted arrays into one sorted array by comparing elements sequentially.
130
128
131
129
## 9. Bubble Sort
132
130
@@ -145,7 +143,7 @@ function bubbleSort(arr) {
145
143
console .log (bubbleSort ([5 , 3 , 8 , 4 , 2 ])); // Output: [2, 3, 4, 5, 8]
146
144
```
147
145
148
- ** Explanation** : Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order.
146
+ ** Explanation** : Sorts an array by repeatedly swapping adjacent elements if they are in the wrong order (Time complexity: O(n²)) .
149
147
150
148
## 10. Find the GCD (Greatest Common Divisor)
151
149
@@ -158,4 +156,4 @@ function gcd(a, b) {
158
156
console .log (gcd (48 , 18 )); // Output: 6
159
157
```
160
158
161
- ** Explanation** : Uses the Euclidean algorithm to compute the greatest common divisor of two numbers.
159
+ ** Explanation** : Uses the Euclidean algorithm to compute the greatest common divisor of two numbers (Time complexity: O(log min(a, b))) .
0 commit comments