Program to find maximum difference between two elements such that larger element appears after the smaller number

Problem statement
Maximum difference between two elements such that larger element appears after the smaller number

Explanation:
Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in arr[].

Examples:

  • If array is [2, 3, 10, 6, 4, 8, 1] then returned value should be 8 (Diff between 10 and 2).
  • If array is [ 7, 9, 5, 6, 3, 2 ] then returned value should be 2 (Diff between 7 and 9)

Conclusion:
You have to take first number of array and check it with remaining array elements.
Then find the largest number in remaining array (excluding number at array index 0).
Then find the difference between largest number from remaining array and first number from array.
This will be the largest difference between two elements.

C program to find maximum difference between two elements such that larger element appears after the smaller number

Output:

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.