Maximum adjacent difference in an array in its sorted formGiven an array, find the maximum difference between its two consecutive elements in its sorted form. Show Maximum difference between two elements such that larger element appears after the smaller numberGiven an array arr[] of integers, find out the maximum difference between any two elements such that larger element appears after the smaller number. Examples : Input : arr = {2, 3, 10, 6, 4, 8, 1} Output : 8 Explanation : The maximum difference is between 10 and 2. Input : arr = {7, 9, 5, 6, 3, 2} Output : 2 Explanation : The maximum difference is between 9 and 7.
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Method 1 (Simple) C++
C
Java
Python3
C#
PHP
Javascript
Output : Time Complexity : O(n^2) Method 2 (Tricky and Efficient) C++
C
Java
Python3
C#
PHP
Javascript
Output: Maximum difference is 109Time Complexity : O(n) Like min element, we can also keep track of max element from right side. Thanks to Katamaran for suggesting this approach. Below is the implementation : C++
Java
Python3
C#
PHP
Javascript
Output: Maximum difference is 109Method 3 (Another Tricky Solution) C++
C
Java
Python3
C#
PHP
Javascript
Output: Maximum difference is 98Time Complexity : O(n) We can modify the above method to work in O(1) extra space. Instead of creating an auxiliary array, we can calculate diff and max sum in same loop. Following is the space optimized version. C++
Java
Python3
C#
PHP
Javascript
Output: Maximum difference is 98Time Complexity : O(n) Below is a variation of this problem:
Article Tags :
Arrays
Amazon Hike MakeMyTrip Ola Cabs SAP Labs Practice Tags :
Amazon Hike MakeMyTrip Ola Cabs SAP Labs Arrays C
DownloadRun Code Output: Maximum adjacent difference in an array in its sorted form in C++C++Server Side ProgrammingProgramming We are given with an array. The array need not be sorted. The task is to find the maximum difference between adjacent elements of that array in its sorted form. So the first thing is to sort the array in increasing or decreasing order. Then we will iterate the array and calculate the adjacent difference of Arr[i+1]-Arr[i]. Then for each iteration we will compare this difference with the one which is found maximum so far. Input − Arr[] = [ 1,5,10,2,7 ] Output − Maximum adjacent difference in array in its sorted form is 3. Explanation − Sorted Arr[] in increasing order = [ 1,2,5,7,10 ]. So the adjacent differences are as follows − Arr[1]-Arr[0]=1, Maximum Difference=1 Arr[2]-Arr[1]=3, Maximum Difference=3 Arr[3]-Arr[2]=2, Maximum Difference=3 Arr[4]-Arr[3]=3, Maximum Difference=3Input − Arr[] = [ 5,11,21,15,20 ] Output − Maximum adjacent difference in array in its sorted form is 6. Explanation − Sorted Arr[] in increasing order = [ 5,11,15,20,21 ]. So the adjacent differences are as follows − Arr[1]-Arr[0]=6, Maximum Difference=6 Arr[2]-Arr[1]=4, Maximum Difference=6 Arr[3]-Arr[2]=5, Maximum Difference=6 Arr[4]-Arr[3]=1, Maximum Difference=6Method 1 (Simple) C++
C program to find maximum difference between two elements#include <stdio.h> /* This function returns the maximum difference between two elements of array, such that larger elements is after smaller element*/ int getMaxDiff(int *array, int size) { /* Initialize maxDiff with diference of first two element */ int i, j; int maxDiff = array[1] - array[0]; /* For every element, check it's difference with all other larger elements */ for(i = 0; i < size; i++){ for(j = i+1; j < size; j++){ if((array[j] - array[i] > maxDiff) && (array[j] > array[i])) maxDiff = array[j] - array[i]; } } return maxDiff; } int main(){ int array[10] = {7, 3, 9, 1, 0, -4, 7, 2, 5, 6}; int maxDiff = getMaxDiff(array, 10); printf("Maximum Difference : %d", maxDiff); return 0; } OutputMaximum Difference : 11 Optimized approach : O(n)
|