Skip to content

Commit a4dd7c3

Browse files
committed
ADDED TWO ARRAY PROGRAMS
1 parent 08d8c6b commit a4dd7c3

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* [Leaders in an Array](https://www.geeksforgeeks.org/dsa/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) {
22+
const result = [];
23+
const n = arr.length;
24+
25+
// Start with the rightmost element
26+
let maxRight = arr[n - 1];
27+
28+
// Rightmost element is always a leader
29+
result.push(maxRight);
30+
31+
// Traverse the array from right to left
32+
for (let i = n - 2; i >= 0; i--) {
33+
if (arr[i] >= maxRight) {
34+
maxRight = arr[i];
35+
result.push(maxRight);
36+
}
37+
}
38+
39+
// Reverse the result array to maintain original order
40+
result.reverse();
41+
42+
return result;
43+
}
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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) {
23+
let n = arr.length;
24+
25+
// Iterate over the array and check if
26+
// every element is greater than or equal to previous element.
27+
for (let i = 1; i < n; i++) {
28+
if (arr[i - 1] > arr[i]) {
29+
return false;
30+
}
31+
}
32+
33+
return true;
34+
}
35+
36+
// Driver Code
37+
let arr = [10, 20, 30, 40, 50];
38+
let n = arr.length;
39+
40+
if (isSorted(arr)) {
41+
console.log("true");
42+
} else {
43+
console.log("false");
44+
}

0 commit comments

Comments
 (0)