Good afternoon! Here's our prompt for today.
The Puzzle in Simple Terms
You have a list of numbers called A
. Your task is to pick two numbers from this list, let's call them A[i]
and A[j]
. Additionally, you will take note of their positions, i
and j
, in the list. You are to find the biggest result you can get from a specific formula.
The Formula to Understand
The formula is simple: You take the difference between the two numbers you picked and add it to the difference between their positions in the list. The trick here is to always consider the differences as positive numbers. We write this as:
1The formula = |A[i] - A[j]| + |i - j|
The vertical bars mean we're taking the absolute value, so negative differences become positive.

Straightforward But Slow Way to Solve It
The most obvious way to solve this is to compare every pair of numbers in the list using the formula. But be warned! This can be very time-consuming if the list is long.
What's Next?
This problem may look simple, but solving it efficiently is where the challenge lies. To prepare for a software engineering career, learning how to solve such problems in a more efficient manner is crucial.
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
var assert = require('assert');
​
function maximumDistance(arr, n) {
// Your code here
}
​
try {
let myArr = [10, 20, 30, 40];
n = myArr.length;
assertIsFunction(maximumDistance);
assert.equal(maximumDistance(myArr, n), 33);
​
console.log(
'PASSED: ' + '`maximumDistance([ 10, 20, 30, 40 ], 4)` should return `33`.'
);
} catch (err) {
console.log(err);
}
​
try {
let myArr = [-70, -64, -6, -56, 64, 61, -57, 16, 48, -98];
n = myArr.length;
assertIsFunction(maximumDistance);
assert.equal(maximumDistance(myArr, n), 167);
​
console.log(
'PASSED: `maximumDistance([ -70, -64, -6, -56, 64, 61, -57, 16, 48, -98 ], 10)` should return `167`.'
);
} catch (err) {
Here's how we would solve this problem...
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.