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
Greatest sum and smallest index difference in JavaScript
This problem asks us to find the maximum value of the expression (arr[i] + arr[j]) + (i - j) for any pair of indices in an array. Let's break down the solution step by step.
Problem Statement
Given an array of integers, we need to find an index pair (i, j) such that (arr[i] + arr[j]) + (i - j) is maximized. The function should return this maximum value.
Understanding the Formula
The expression (arr[i] + arr[j]) + (i - j) can be rearranged as:
(arr[i] + i) + (arr[j] - j)
This rearrangement helps us optimize the solution by treating arr[i] + i and arr[j] - j as separate components.
Example Walkthrough
Let's trace through the example:
const arr = [8, 1, 5, 2, 6];
// For i = 0, j = 2:
// (arr[0] + arr[2]) + (0 - 2) = (8 + 5) + (-2) = 11
console.log("Array:", arr);
console.log("Index 0, Index 2:", (8 + 5) + (0 - 2));
Array: [ 8, 1, 5, 2, 6 ] Index 0, Index 2: 11
Optimized Solution
The key insight is to maintain the maximum value of arr[i] + i seen so far, and for each new position j, calculate max(arr[i] + i) + (arr[j] - j):
const arr = [8, 1, 5, 2, 6];
const findMaximum = (arr = []) => {
let max = arr[0] + 0; // Initialize with arr[0] + 0
let res = -Infinity; // Track maximum result
for(let i = 1; i < arr.length; i++){
// Calculate current maximum using previous max and current element
res = Math.max(res, max + arr[i] - i);
// Update max to include current element
max = Math.max(arr[i] + i, max);
}
return res;
};
console.log("Maximum value:", findMaximum(arr));
Maximum value: 11
How It Works
The algorithm works by:
- Starting with
max = arr[0] + 0(the first element plus its index) - For each subsequent element at index
i:- Calculate the potential maximum using the current
maxandarr[i] - i - Update
maxto be the maximum of currentarr[i] + iand previousmax
- Calculate the potential maximum using the current
- Return the overall maximum found
Time Complexity
This solution runs in O(n) time with O(1) space complexity, making it much more efficient than checking all possible pairs which would be O(n²).
Alternative Brute Force Approach
For comparison, here's the brute force solution that checks all pairs:
const bruteForceSolution = (arr) => {
let maxValue = -Infinity;
for(let i = 0; i < arr.length; i++){
for(let j = 0; j < arr.length; j++){
let currentValue = (arr[i] + arr[j]) + (i - j);
maxValue = Math.max(maxValue, currentValue);
}
}
return maxValue;
};
console.log("Brute force result:", bruteForceSolution([8, 1, 5, 2, 6]));
Brute force result: 11
Conclusion
The optimized solution transforms the problem by rearranging the formula to (arr[i] + i) + (arr[j] - j), allowing us to solve it in linear time. This approach is much more efficient than checking all possible index pairs.
