Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

Write a Program to Find the Maximum Difference between Two Adjacent Numbers in an Array of Positive Integers

In this article, we look at a problem : Given an Array of Positive Integers, Find the Maximum Difference between Two Adjacent Numbers.

For each pair of elements we need to compute their difference and find the Maximum value of all the differences in array.

Let us look at an example, Consider this array:

Sample Array with 10 Elements.

For the above shown array, we have to solve the given problem. Here are the steps which we will follow for the implementation:

  • Firstly, for each pair of elements, we have calculated their difference. We have calculated the Absolute Value of their difference because the array is not sorted and elements on either side can be greater or smaller.
  • In the figure above, we have shown the Diff. value for each such pair. We need to find the Maximum Value among these which for the above case is : 11 for the pair (1,12).
  • We are going to iterate through the array, Arr starting from index 1 and calculate the current difference for each pair as : Arr[i]>Arr[i] - Arr[i-1]code>. We have a max_diff variable which maintains the maximum of all the differences, on each iteration we update the max_diff with current difference.

Note: To calculate the Absolute Value we use Math.abs() in java. The approach is for only Positive Values in the array.

Now, let us have a quick look at the implementation:


We implement the same example discussed above in code. Let us analyze the complexity of the code.

Time Complexity: We do a simple single traversal of the array which takes linear time so the time complexitiy is O(n), for n elements in array.

That’s it for the article you can try out this code with different examples and let us know your suggestions or queries.

Related Posts

  • 11 November

    How to print array in java

    Learn about how to print array in java in multiple ways.

  • 09 October

    Initialize 2D array in Java

    Table of ContentsInitialize 2D array Using for loopInitialize 2D array using an initializerInitialize 2D array of variable columns lengthInitialize 2D array with heterogeneous dataInitialize 2D array using initialilzer with columnsInitialize 2D array using Reflection APIInitialize 2D array using the toCharArray() In this article, we will learn to initialize 2D array in Java. The array is […]

  • 24 October

    Java add to array

    Table of ContentsUsing Apache’s common lang libraryBy writing your own utility method In this post, we will see how to add elements to the array. Using Apache’s common lang library You can use varargs add method to add elements to array dynamically. Here are the few add overloaded methods provided by ArrayUtils class If you […]

  • 10 November

    Java Convert Array to List

    Table of ContentsBy using Arrays.asList():Using Collections.addAll() method :Using Java 8’s StreamManual way using add() method: In this post, we will see how to convert array to list in java. There are may ways to convert array to list: By using Arrays.asList(): This is an easy method to convert Array into list.We just need to pass […]

  • 10 November

    Java convert List to Array

    Table of ContentsUsing toArray() method Using Java 8’s StreamUsing Arrays.copyOfUsing System.arraycopyManual method In this post, we will see how to convert List to Array in java. There are many ways to convert List to Array in java Using toArray() method We can use toArray() method to convert List to String. We can either pass a […]

  • 22 September

    Java program to Remove element from array

    Table of ContentsRemove element from array with inbuilt functonRemove element from array without inbuilt functon In this post, we will see how to remove an element from array in java. Unlike Arraylist,Java Arrays class does not provide any direct method to add or delete element. As Array is fixed size in nature, you can not shrink or […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.