Skip to content

Instantly share code, notes, and snippets.

@jpolvora
Created October 27, 2017 14:55

Revisions

  1. jpolvora created this gist Oct 27, 2017.
    30 changes: 30 additions & 0 deletions arrayMaximalAdjacentDifference.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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;
    }