From a4dd7c32ab81fb5fa168fec8fd34eb83c0ce0fc2 Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Sun, 12 Oct 2025 12:35:59 +0530 Subject: [PATCH 1/3] ADDED TWO ARRAY PROGRAMS --- Data-Structures/Array/leadersinarray.js | 48 +++++++++++++++++++ .../Array/test/checkarraysortedornot.js | 44 +++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 Data-Structures/Array/leadersinarray.js create mode 100644 Data-Structures/Array/test/checkarraysortedornot.js diff --git a/Data-Structures/Array/leadersinarray.js b/Data-Structures/Array/leadersinarray.js new file mode 100644 index 0000000000..5b8bc70170 --- /dev/null +++ b/Data-Structures/Array/leadersinarray.js @@ -0,0 +1,48 @@ +/** + * [Leaders in an Array](https://www.geeksforgeeks.org/dsa/leaders-in-an-array/) + * + * A leader in an array is an element that is greater than or equal to + * all the elements to its right. + * + * Example: + * Input: [16, 17, 4, 3, 5, 2] + * Output: [17, 5, 2] + * + * Algorithm: + * - Start from the rightmost element (which is always a leader). + * - Traverse the array from right to left, keeping track of the maximum so far. + * - If the current element is greater than or equal to that maximum, it is a leader. + * - Reverse the list of leaders at the end to preserve original order. + * + * @complexity O(n) — each element is visited once. + * @flow + */ + +function leaders(arr) { + const result = []; + const n = arr.length; + + // Start with the rightmost element + let maxRight = arr[n - 1]; + + // Rightmost element is always a leader + result.push(maxRight); + + // Traverse the array from right to left + for (let i = n - 2; i >= 0; i--) { + if (arr[i] >= maxRight) { + maxRight = arr[i]; + result.push(maxRight); + } + } + + // Reverse the result array to maintain original order + result.reverse(); + + return result; +} + +// Driver code +const arr = [16, 17, 4, 3, 5, 2]; +const result = leaders(arr); +console.log(result.join(" ")); diff --git a/Data-Structures/Array/test/checkarraysortedornot.js b/Data-Structures/Array/test/checkarraysortedornot.js new file mode 100644 index 0000000000..3ae89a4e8d --- /dev/null +++ b/Data-Structures/Array/test/checkarraysortedornot.js @@ -0,0 +1,44 @@ +/** + * [Check if Array is Sorted](https://www.geeksforgeeks.org/check-if-an-array-is-sorted-in-ascending-order/) + * + * This function checks whether a given array is sorted in non-decreasing (ascending) order. + * + * Algorithm: + * - Traverse the array from the second element onward. + * - Compare each element with its previous one. + * - If any element is smaller than its predecessor, the array is not sorted. + * - Otherwise, if no such pair exists, the array is sorted. + * + * Example: + * Input: [10, 20, 30, 40, 50] + * Output: true + * + * @param {number[]} arr - The input array of numbers. + * @returns {boolean} true if the array is sorted in ascending order, false otherwise. + * @complexity O(n) — each element is checked once. + * @flow + */ + +function isSorted(arr) { + let n = arr.length; + + // Iterate over the array and check if + // every element is greater than or equal to previous element. + for (let i = 1; i < n; i++) { + if (arr[i - 1] > arr[i]) { + return false; + } + } + + return true; +} + +// Driver Code +let arr = [10, 20, 30, 40, 50]; +let n = arr.length; + +if (isSorted(arr)) { + console.log("true"); +} else { + console.log("false"); +} From c8d68bf80f199da2dad228ff3db6f2b56766b3b0 Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Sun, 12 Oct 2025 13:57:36 +0530 Subject: [PATCH 2/3] ADDED TWO ARRAY PROGRAM --- Data-Structures/Array/leadersinarray.js | 4 ++-- Data-Structures/Array/test/checkarraysortedornot.js | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Data-Structures/Array/leadersinarray.js b/Data-Structures/Array/leadersinarray.js index 5b8bc70170..84f498d084 100644 --- a/Data-Structures/Array/leadersinarray.js +++ b/Data-Structures/Array/leadersinarray.js @@ -1,5 +1,5 @@ /** - * [Leaders in an Array](https://www.geeksforgeeks.org/dsa/leaders-in-an-array/) + * [Leaders in an Array](https://www.geeksforgeeks.org/leaders-in-an-array/) * * A leader in an array is an element that is greater than or equal to * all the elements to its right. @@ -45,4 +45,4 @@ function leaders(arr) { // Driver code const arr = [16, 17, 4, 3, 5, 2]; const result = leaders(arr); -console.log(result.join(" ")); +console.log(result.join(' ')); diff --git a/Data-Structures/Array/test/checkarraysortedornot.js b/Data-Structures/Array/test/checkarraysortedornot.js index 3ae89a4e8d..39019c1693 100644 --- a/Data-Structures/Array/test/checkarraysortedornot.js +++ b/Data-Structures/Array/test/checkarraysortedornot.js @@ -20,7 +20,7 @@ */ function isSorted(arr) { - let n = arr.length; + const n = arr.length; // Iterate over the array and check if // every element is greater than or equal to previous element. @@ -34,11 +34,10 @@ function isSorted(arr) { } // Driver Code -let arr = [10, 20, 30, 40, 50]; -let n = arr.length; +const arr = [10, 20, 30, 40, 50]; if (isSorted(arr)) { - console.log("true"); + console.log('true'); } else { - console.log("false"); + console.log('false'); } From ea790667a176a2610ec9189541eb1ff20f04bc4e Mon Sep 17 00:00:00 2001 From: VeeruYadav45 Date: Sun, 12 Oct 2025 14:15:26 +0530 Subject: [PATCH 3/3] Added two algorithms --- Data-Structures/Array/LeadersInArray.js.test | 23 ++++++++++++ Data-Structures/Array/leadersinarray.js | 34 +----------------- .../Array/test/CheckArraySortedOrNot.js.test | 23 ++++++++++++ .../Array/test/checkarraysortedornot.js | 35 +------------------ 4 files changed, 48 insertions(+), 67 deletions(-) create mode 100644 Data-Structures/Array/LeadersInArray.js.test create mode 100644 Data-Structures/Array/test/CheckArraySortedOrNot.js.test diff --git a/Data-Structures/Array/LeadersInArray.js.test b/Data-Structures/Array/LeadersInArray.js.test new file mode 100644 index 0000000000..7ec937aa8a --- /dev/null +++ b/Data-Structures/Array/LeadersInArray.js.test @@ -0,0 +1,23 @@ +import { leaders } from '../leadersinarray.js'; + +describe('leaders()', () => { + test('should return correct leaders for given array', () => { + expect(leaders([16, 17, 4, 3, 5, 2])).toEqual([17, 5, 2]); + }); + + test('should handle a single element array', () => { + expect(leaders([10])).toEqual([10]); + }); + + test('should handle already decreasing array', () => { + expect(leaders([10, 9, 8, 7])).toEqual([10, 9, 8, 7]); + }); + + test('should handle increasing array', () => { + expect(leaders([1, 2, 3, 4])).toEqual([4]); + }); + + test('should handle array with duplicates', () => { + expect(leaders([5, 5, 5, 5])).toEqual([5, 5, 5, 5]); + }); +}); diff --git a/Data-Structures/Array/leadersinarray.js b/Data-Structures/Array/leadersinarray.js index 84f498d084..2f27dd8928 100644 --- a/Data-Structures/Array/leadersinarray.js +++ b/Data-Structures/Array/leadersinarray.js @@ -1,34 +1,9 @@ -/** - * [Leaders in an Array](https://www.geeksforgeeks.org/leaders-in-an-array/) - * - * A leader in an array is an element that is greater than or equal to - * all the elements to its right. - * - * Example: - * Input: [16, 17, 4, 3, 5, 2] - * Output: [17, 5, 2] - * - * Algorithm: - * - Start from the rightmost element (which is always a leader). - * - Traverse the array from right to left, keeping track of the maximum so far. - * - If the current element is greater than or equal to that maximum, it is a leader. - * - Reverse the list of leaders at the end to preserve original order. - * - * @complexity O(n) — each element is visited once. - * @flow - */ - -function leaders(arr) { +export function leaders(arr) { const result = []; const n = arr.length; - - // Start with the rightmost element let maxRight = arr[n - 1]; - - // Rightmost element is always a leader result.push(maxRight); - // Traverse the array from right to left for (let i = n - 2; i >= 0; i--) { if (arr[i] >= maxRight) { maxRight = arr[i]; @@ -36,13 +11,6 @@ function leaders(arr) { } } - // Reverse the result array to maintain original order result.reverse(); - return result; } - -// Driver code -const arr = [16, 17, 4, 3, 5, 2]; -const result = leaders(arr); -console.log(result.join(' ')); diff --git a/Data-Structures/Array/test/CheckArraySortedOrNot.js.test b/Data-Structures/Array/test/CheckArraySortedOrNot.js.test new file mode 100644 index 0000000000..28fed379c1 --- /dev/null +++ b/Data-Structures/Array/test/CheckArraySortedOrNot.js.test @@ -0,0 +1,23 @@ +import { isSorted } from '../checkarraysortedornot.js'; + +describe('isSorted()', () => { + test('should return true for sorted array', () => { + expect(isSorted([10, 20, 30, 40, 50])).toBe(true); + }); + + test('should return false for unsorted array', () => { + expect(isSorted([10, 50, 30, 20])).toBe(false); + }); + + test('should return true for array with equal elements', () => { + expect(isSorted([5, 5, 5, 5])).toBe(true); + }); + + test('should return true for single element array', () => { + expect(isSorted([100])).toBe(true); + }); + + test('should return true for empty array', () => { + expect(isSorted([])).toBe(true); + }); +}); diff --git a/Data-Structures/Array/test/checkarraysortedornot.js b/Data-Structures/Array/test/checkarraysortedornot.js index 39019c1693..deb62326d7 100644 --- a/Data-Structures/Array/test/checkarraysortedornot.js +++ b/Data-Structures/Array/test/checkarraysortedornot.js @@ -1,43 +1,10 @@ -/** - * [Check if Array is Sorted](https://www.geeksforgeeks.org/check-if-an-array-is-sorted-in-ascending-order/) - * - * This function checks whether a given array is sorted in non-decreasing (ascending) order. - * - * Algorithm: - * - Traverse the array from the second element onward. - * - Compare each element with its previous one. - * - If any element is smaller than its predecessor, the array is not sorted. - * - Otherwise, if no such pair exists, the array is sorted. - * - * Example: - * Input: [10, 20, 30, 40, 50] - * Output: true - * - * @param {number[]} arr - The input array of numbers. - * @returns {boolean} true if the array is sorted in ascending order, false otherwise. - * @complexity O(n) — each element is checked once. - * @flow - */ - -function isSorted(arr) { +export function isSorted(arr) { const n = arr.length; - // Iterate over the array and check if - // every element is greater than or equal to previous element. for (let i = 1; i < n; i++) { if (arr[i - 1] > arr[i]) { return false; } } - return true; } - -// Driver Code -const arr = [10, 20, 30, 40, 50]; - -if (isSorted(arr)) { - console.log('true'); -} else { - console.log('false'); -}