Dsa
Monotonic Stack
Hard
Earliest Expiration
You are given a list of medication expiration dates as integers representing days from today. Your task is to determine the earliest expiration date for every medication such that it has a valid preceding medication still available. A medication's expiration date is valid if it occurs after the previous medication's expiration date. Output the earliest valid expiration date for each medication, or -1 if there is no valid previous medication.
Input Format:
- The first line contains an integer n (1 <= n <= 10^5), the number of medications.
- The second line contains n space-separated integers, each representing the expiration date of a medication.
Output Format:
- Output a single line containing n space-separated integers, representing the earliest valid expiration date for each medication, or -1 where applicable.
Example:
Input:
5
5 2 6 3 4
Output:
-1 5 -1 6 6
Key concepts
monotonic_stackintervalsscheduling
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now