Question Bank
/

JavaScript Largest Difference in Array: Three Approaches Quiz

JavaScript Largest Difference in Array: Three Approaches Quiz

Three seeded ways to compute the maximum pairwise gap in an integer array (sort and subtract ends, reduce with Math.max and Math.min, single-pass tracker), plus two companions on edge cases and complexity.

Question Bank
Hard
JavaScript
quiz
arrays
math
array-manipulation-patterns

743 views

6

Implement largestDiff(arr) by sorting the array ascending and subtracting the first element from the last. Return 0 for arrays with fewer than two elements.

Examples

Example 1:

Input: largestDiff([2, 24, 7, 4, 13, 6, 30, 8])
Output: 28
Explanation: after sorting the smallest is 2 and the largest is 30, so the maximum difference is 30 - 2 = 28.

4 more questions, with full solutions and explanations, are available for premium members.

Upgrade to Premium