Skip to content

2016. Maximum Difference Between Increasing Elements #1814

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the maximum difference between two elements in an array such that the smaller element appears before the larger element. If no such pair exists, we should return -1.

Approach

  1. Initialization: Start by initializing the minimum element encountered so far as the first element of the array. Also, initialize the maximum difference as -1, indicating no valid pair has been found yet.
  2. Iterate through the array: For each subsequent element in the array:
    • Check for Valid Pair: If the current element is greater than the minimum element encountered so far, calculate the difference between the current element and the minimum element. Update the maximum difference if this difference is …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Jun 16, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Jun 16, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants