Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Created October 27, 2017 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpolvora/a2164c4e00dc7d3290337d30c5770999 to your computer and use it in GitHub Desktop.
Save jpolvora/a2164c4e00dc7d3290337d30c5770999 to your computer and use it in GitHub Desktop.
int arrayMaximalAdjacentDifference(int[] inputArray) {
var major = 0;
for (int i = 0; i < inputArray.Length; i++) {
var previous = i - 1;
var next = i + 1;
if (previous < 0 || next > inputArray.Length -1) continue;
var current = inputArray[i];
var preval = inputArray[previous];
var nextval = inputArray[next];
int t1 = 0;
if (current >= preval) {
t1 = current - preval;
} else {
t1 = preval - current;
}
if (t1 >= major) major = t1;
if (current >= nextval) {
t1 = current - nextval;
} else {
t1 = nextval - current;
}
if (t1 >= major) major = t1;
}
return major;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment