Calculating the maximum difference between two adjacent numbers in an array

22,596

Solution 1

Something like this should do the trick:

public static int maxDiff(int[] numbers) {
      if (numbers.length < 2) {
          return 0;
      }
      if (numbers.length == 2) {
          return Math.abs(numbers[1] - numbers[0]);
      }
      int max = Math.abs(numbers[1] - numbers[0]);
      for (int i = 2; i < numbers.length; i++) {
          int diff = Math.abs(numbers[i-1] - numbers[i]);
          if (diff > max) {
              max = diff;
          }
      }
      return max;
}

Solution 2

This piece of code can help you:

    int[] numbers = {12, 8, 34, 10, 59};

    int diff = 0;
    int previous = 0;

    for (int n : numbers) {
        diff = Math.max(diff, Math.abs(n - previous));
        previous = n;
    }

Variable "diff" will contain the value you look for.

Solution 3

You must assure to take the absolute difference, don't forget it. That's why I used the Math.abs() function.

public static int maxDiff(int[] numbers) {
      int diff = Math.abs(numbers[1] - numbers[0]);
      for(int i = 1; i < numbers.length-1; i++)
          if(Math.abs(numbers[i+1]-numbers[i]) > diff)
              diff = Math.abs(numbers[i+1] - numbers[i]);
      return diff;
}

Solution 4

For the specific question you asked:

public static int maxDiff(int[] arr) {
    if(arr.length < 2)
        return -1; // error condition: throw exception?

    int maxdiff = Integer.MIN_VALUE;
    for(int i = 1; i < arr.length; ++i) {
        int diff = Math.abs(arr[i] - arr[i-1]);
        if(diff > maxdiff)
            maxdiff = diff;
    }

    return maxdiff;
}

If you want the max difference across all numbers in the array (not just adjacent ones) the most efficient way to do it would be to iterate the array just once to find the minimum and maximum, then return the absolute value of the two values subtracted from each other.

public static int maxDiff(int[] arr) {
    if(arr.length < 2) 
        return -1; // error condition: throw exception?

    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for(int i = 0; i < arr.length; ++i) {
        if(arr[i] < min)
            min = arr[i];
        if(arr[i] > max)
            max = arr[i]; 
    }

    return Math.abs(max - min);
}
Share:
22,596
user3080698
Author by

user3080698

Updated on July 09, 2022

Comments

  • user3080698
    user3080698 almost 2 years

    Recently I've been assigned a task which asks to me to "calculate the maximum difference between two adjacent numbers in the array that is passed to it". I'm fairly new to Java (I have only done VB in the past) and since this topic was not well explained to me, I'm not quite sure how to go about it.

    Here is some additional information about the task itself:

    The function has to pass the following test. The function maxDiff should calculate the maximum difference between two adjacent numbers in the array that is passed to it.

    @Test
    public void assessmentTest() {
     int [] numbers = {12, 8, 34, 10, 59};
     assertEquals(49, maxDiff(numbers));
     int [] numbers2 = {-50, 100, 20, -40};
     assertEquals(150, maxDiff(numbers2));
    }