Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Maximum difference between a number JavaScript
In this problem statement, our task is to write a function for finding the maximum difference between two numbers in an array using JavaScript. We'll explore both a brute force approach and an optimized solution.
Understanding the Problem
We need to find the maximum difference between any two numbers in an array where the larger number appears after the smaller number in the array order. For example, in array [2, 3, 5, 6, 4, 1], the maximum difference is 4 (6 - 2 = 4), not 5 (6 - 1), because 1 comes after 6.
Method 1: Brute Force Approach (O(n²))
This approach uses nested loops to check all possible pairs:
function maxDiffBruteForce(array) {
if (array.length < 2) {
return 0;
}
let maxDifference = array[1] - array[0];
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
let currentDiff = array[j] - array[i];
if (currentDiff > maxDifference) {
maxDifference = currentDiff;
}
}
}
return maxDifference;
}
console.log("Brute Force Result:");
console.log(maxDiffBruteForce([2, 3, 5, 6, 4, 1])); // 4
console.log(maxDiffBruteForce([7, 1, 5, 3, 6, 4])); // 5
Brute Force Result: 4 5
Method 2: Optimized Approach (O(n))
We can solve this more efficiently by tracking the minimum value seen so far and calculating the maximum difference in a single pass:
function maxDiffOptimized(array) {
if (array.length < 2) {
return 0;
}
let minElement = array[0];
let maxDifference = array[1] - array[0];
for (let i = 1; i < array.length; i++) {
// Update max difference if current element minus min gives larger difference
maxDifference = Math.max(maxDifference, array[i] - minElement);
// Update minimum element seen so far
minElement = Math.min(minElement, array[i]);
}
return maxDifference;
}
console.log("Optimized Result:");
console.log(maxDiffOptimized([2, 3, 5, 6, 4, 1])); // 4
console.log(maxDiffOptimized([7, 1, 5, 3, 6, 4])); // 5
console.log(maxDiffOptimized([5, 4, 3, 2, 1])); // -1 (decreasing array)
Optimized Result: 4 5 -1
How the Optimized Solution Works
The optimized approach maintains two variables: the minimum element encountered so far and the maximum difference found. For each element, it calculates the difference with the minimum element and updates the maximum difference accordingly.
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n²) | O(1) | Small arrays, easy to understand |
| Optimized | O(n) | O(1) | Large arrays, production code |
Edge Cases
console.log("Edge Cases:");
console.log(maxDiffOptimized([])); // 0 (empty array)
console.log(maxDiffOptimized([5])); // 0 (single element)
console.log(maxDiffOptimized([3, 3, 3])); // 0 (all same elements)
Edge Cases: 0 0 0
Conclusion
The optimized O(n) solution is preferred for production code as it handles large arrays efficiently. The brute force approach helps understand the problem but becomes impractical for large datasets due to its O(n²) complexity.
