- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with an array of integers of size N. The array consists of integers in random order. The task is to find the maximum difference between two elements such that the larger element appears after the smaller number. That is Arr[j]-Arr[i] is maximum such that j>i.

**Input**

Arr[] = { 2,1,3,8,3,19,21}.

**Output** −The maximum difference between two elements such that the larger element appears after the smaller number − 20

**Explanation** − The maximum difference is between 21 and 1 and 21 appears after 1 in the array.

**Input**

Arr[] = {18, 2,8,1,2,3,2,6 }.

**Output** −The maximum difference between two elements such that the larger element appears after the smaller number − 6

**Explanation** − The maximum difference is between 8 and 2 and 8 appears after 2 in the array.

Declare an array of integers which contains pairs of sides of rectangle.( Arr[] )

Create a variable to store the size of the array. (n)

The function maxArea(int arr[],int n) is used to calculate the maximum area for rectangle. It takes an input array and its size as arguments.

Inside maxArea() we declared an array Dim[2] two store highest two sides found after traversing the sorted array (in descending order) arr[].

As arr[] is sorted in descending order the highest 4 sides must be in the beginning. We will iterate arr[] such that a pair of sides is found.

Initialize Dim[] with 0 at first.

Inside the while loop we put the condition that it continues till j<2 that there are no values found for dim[0] and dim[1] or the end of arr[] is reached. (i<n).

If a pair of such sides is found, ( if(arr[i]==arr[i+1]) ), then store it in dim[j] and increment j for next side.

Return the result as a product of dim[0] and dim[1].

Note − sort(arr,n) is supposed to sort arr in descending order.

#include <stdio.h> int maxDiff(int arr[], int n){ // Maximum difference found so far int MD = arr[1] - arr[0]; // Minimum number visited so far int min = arr[0]; for(int i = 1; i < n; i++){ if (arr[i] - min > MD) MD = arr[i] - min; if (arr[i] < min) min = arr[i]; } return MD; } /* Driver program to test above function */ int main(){ int arr[] = {2,5,7,3,4,12}; int n=6; // Function calling printf("Maximum difference is : %d ",maxDiff(arr, n)); return 0; }

If we run the above code it will generate the following output −

The maximum difference between two elements such that the larger element appears after the smaller number : 10

- Related Questions & Answers
- Number of smaller and larger elements - JavaScript
- Maximum sum such that no two elements are adjacent in C++
- Find maximum difference between nearest left and right smaller elements in C++
- Frequency of smaller and larger elements - JavaScript
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum difference between two subsets of m elements in C
- Find maximum difference between nearest left and right smaller elements in Python
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Count Derangements (Permutation such that no element appears in its original position) in C++
- Find the element that appears once in an array where every other element appears twice in C++
- Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum Subarray Sum after inverting at most two elements in C++
- Maximum element between two nodes of BST in C++

Advertisements