Skip to content

Commit ea79066

Browse files
committed
Added two algorithms
1 parent c8d68bf commit ea79066

File tree

4 files changed

+48
-67
lines changed

4 files changed

+48
-67
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { leaders } from '../leadersinarray.js';
2+
3+
describe('leaders()', () => {
4+
test('should return correct leaders for given array', () => {
5+
expect(leaders([16, 17, 4, 3, 5, 2])).toEqual([17, 5, 2]);
6+
});
7+
8+
test('should handle a single element array', () => {
9+
expect(leaders([10])).toEqual([10]);
10+
});
11+
12+
test('should handle already decreasing array', () => {
13+
expect(leaders([10, 9, 8, 7])).toEqual([10, 9, 8, 7]);
14+
});
15+
16+
test('should handle increasing array', () => {
17+
expect(leaders([1, 2, 3, 4])).toEqual([4]);
18+
});
19+
20+
test('should handle array with duplicates', () => {
21+
expect(leaders([5, 5, 5, 5])).toEqual([5, 5, 5, 5]);
22+
});
23+
});
Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,16 @@
1-
/**
2-
* [Leaders in an Array](https://www.geeksforgeeks.org/leaders-in-an-array/)
3-
*
4-
* A leader in an array is an element that is greater than or equal to
5-
* all the elements to its right.
6-
*
7-
* Example:
8-
* Input: [16, 17, 4, 3, 5, 2]
9-
* Output: [17, 5, 2]
10-
*
11-
* Algorithm:
12-
* - Start from the rightmost element (which is always a leader).
13-
* - Traverse the array from right to left, keeping track of the maximum so far.
14-
* - If the current element is greater than or equal to that maximum, it is a leader.
15-
* - Reverse the list of leaders at the end to preserve original order.
16-
*
17-
* @complexity O(n) — each element is visited once.
18-
* @flow
19-
*/
20-
21-
function leaders(arr) {
1+
export function leaders(arr) {
222
const result = [];
233
const n = arr.length;
24-
25-
// Start with the rightmost element
264
let maxRight = arr[n - 1];
27-
28-
// Rightmost element is always a leader
295
result.push(maxRight);
306

31-
// Traverse the array from right to left
327
for (let i = n - 2; i >= 0; i--) {
338
if (arr[i] >= maxRight) {
349
maxRight = arr[i];
3510
result.push(maxRight);
3611
}
3712
}
3813

39-
// Reverse the result array to maintain original order
4014
result.reverse();
41-
4215
return result;
4316
}
44-
45-
// Driver code
46-
const arr = [16, 17, 4, 3, 5, 2];
47-
const result = leaders(arr);
48-
console.log(result.join(' '));
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isSorted } from '../checkarraysortedornot.js';
2+
3+
describe('isSorted()', () => {
4+
test('should return true for sorted array', () => {
5+
expect(isSorted([10, 20, 30, 40, 50])).toBe(true);
6+
});
7+
8+
test('should return false for unsorted array', () => {
9+
expect(isSorted([10, 50, 30, 20])).toBe(false);
10+
});
11+
12+
test('should return true for array with equal elements', () => {
13+
expect(isSorted([5, 5, 5, 5])).toBe(true);
14+
});
15+
16+
test('should return true for single element array', () => {
17+
expect(isSorted([100])).toBe(true);
18+
});
19+
20+
test('should return true for empty array', () => {
21+
expect(isSorted([])).toBe(true);
22+
});
23+
});
Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,10 @@
1-
/**
2-
* [Check if Array is Sorted](https://www.geeksforgeeks.org/check-if-an-array-is-sorted-in-ascending-order/)
3-
*
4-
* This function checks whether a given array is sorted in non-decreasing (ascending) order.
5-
*
6-
* Algorithm:
7-
* - Traverse the array from the second element onward.
8-
* - Compare each element with its previous one.
9-
* - If any element is smaller than its predecessor, the array is not sorted.
10-
* - Otherwise, if no such pair exists, the array is sorted.
11-
*
12-
* Example:
13-
* Input: [10, 20, 30, 40, 50]
14-
* Output: true
15-
*
16-
* @param {number[]} arr - The input array of numbers.
17-
* @returns {boolean} true if the array is sorted in ascending order, false otherwise.
18-
* @complexity O(n) — each element is checked once.
19-
* @flow
20-
*/
21-
22-
function isSorted(arr) {
1+
export function isSorted(arr) {
232
const n = arr.length;
243

25-
// Iterate over the array and check if
26-
// every element is greater than or equal to previous element.
274
for (let i = 1; i < n; i++) {
285
if (arr[i - 1] > arr[i]) {
296
return false;
307
}
318
}
32-
339
return true;
3410
}
35-
36-
// Driver Code
37-
const arr = [10, 20, 30, 40, 50];
38-
39-
if (isSorted(arr)) {
40-
console.log('true');
41-
} else {
42-
console.log('false');
43-
}

0 commit comments

Comments
 (0)