ExamAdda Logo

Daily Temperatures

Easy

Given an array of integers temperatures representing the daily temperatures, return an array answer such that:

answer[i]

 

is the number of days you have to wait after the ith day to get a warmer temperature.

If there is no future day for which this is possible, keep answer[i] = 0.

Example 1

Input

8
73 74 75 71 69 72 76 73

Output

1 1 4 2 1 1 0 0

Explanation

For example, after 73, the next warmer temperature is 74, so the answer is 1.

After 75, the next warmer temperature is 76, which occurs 4 days later.

Example 2

Input

4
30 40 50 60

Output

1 1 1 0

Explanation

Each temperature has a warmer temperature on the following day except for the last day.

Constraints

1 <= temperatures.length <= 10^5
30 <= temperatures[i] <= 100

Hints:

Hint 1

A brute-force solution checks future days for every temperature, but this can take O(n²) time.

Hint 2

Use a monotonic decreasing stack to store indices of temperatures whose warmer day has not been found yet.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 10, 2026

Last updated

Aug 10, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output