Write a javascript function that find leaders from an input array.
In an array, an element is considered a "leader" if it is greater than all the elements to its right. To find all the leader elements in an array, you can use various approaches. I'll outline a few methods and provide a code example for each.
Input: array = [16, 17, 4, 3, 5, 2]
Output: [17, 5, 2]
Input: array = [7, 10, 4, 3, 6, 5]
Output: [10, 6, 5]
Input: array = [2]
Output: [2]
Input: array = []
Output: ❌ Throw error => Input must be a non-empty array
function findLeaders(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const leaders = [];
let maxFromRight = arr[arr.length - 1];
leaders.push(maxFromRight);
for (let i = arr.length - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
leaders.push(maxFromRight);
}
}
return leaders.reverse();
}function findLeadersBruteForce(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const leaders = [];
const n = arr.length;
for (let i = 0; i < n; i++) {
let isLeader = true;
for (let j = i + 1; j < n; j++) {
if (arr[i] <= arr[j]) {
isLeader = false;
break;
}
}
if (isLeader) {
leaders.push(arr[i]);
}
}
return leaders;
}function findLeadersUsingStack(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const leaders = [];
const stack = [];
const n = arr.length;
// Traverse the array from right to left
for (let i = n - 1; i >= 0; i--) {
// If the stack is empty or the current element is greater than the top of the stack
if (stack.length === 0 || arr[i] > stack[stack.length - 1]) {
stack.push(arr[i]);
}
}
// The stack will have the leaders in reverse order, so reverse it
return stack.reverse();
}function findLeadersDivideAndConquer(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
function findLeadersInRange(start, end) {
if (start === end) return [arr[start]];
const mid = Math.floor((start + end) / 2);
const leftLeaders = findLeadersInRange(start, mid);
const rightLeaders = findLeadersInRange(mid + 1, end);
let rightMax = Math.max(...rightLeaders);
return leftLeaders.filter(x => x > rightMax).concat(rightLeaders);
}
return findLeadersInRange(0, arr.length - 1);
}function findLeadersUsingFunctional(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const maxFromRight = [];
let currentMax = -Infinity;
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] > currentMax) {
currentMax = arr[i];
maxFromRight[i] = arr[i];
} else {
maxFromRight[i] = currentMax;
}
}
return arr.filter((value, index) => value === maxFromRight[index]);
}function findLeadersUsingHelperArray(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const n = arr.length;
const maxFromRight = new Array(n);
maxFromRight[n - 1] = arr[n - 1];
// Fill the maxFromRight array
for (let i = n - 2; i >= 0; i--) {
maxFromRight[i] = Math.max(arr[i], maxFromRight[i + 1]);
}
// Find leaders
const leaders = [];
for (let i = 0; i < n; i++) {
if (arr[i] > maxFromRight[i + 1] || i === n - 1) {
leaders.push(arr[i]);
}
}
return leaders;
}function findLeadersUsingObject(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const leaders = [];
let maxFromRight = -Infinity;
for (let i = arr.length - 1; i >= 0; i--) {
if (arr[i] > maxFromRight) {
leaders.push(arr[i]);
maxFromRight = arr[i];
}
}
return leaders.reverse();
}function findLeadersUsingFunctionalReduce(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
// Compute maximum values from the right
const maxFromRight = arr.reduceRight((acc, current, index) => {
if (current > acc.max) {
acc.max = current;
acc.leaders.push(current);
}
return acc;
}, { max: -Infinity, leaders: [] });
return maxFromRight.leaders.reverse();
}function findLeadersUsingSet(arr) {
if (!Array.isArray(arr) || arr.length === 0) {
throw new Error('Input must be a non-empty array');
}
const leaders = new Set();
let maxFromRight = arr[arr.length - 1];
leaders.add(maxFromRight);
for (let i = arr.length - 2; i >= 0; i--) {
if (arr[i] > maxFromRight) {
maxFromRight = arr[i];
leaders.add(maxFromRight);
}
}
return Array.from(leaders).reverse();
}