Convert the Temperature - Solution & Explanation
Problem Statement
You are given a non-negative floating point number rounded to two decimal places celsius, that denotes the temperature in Celsius.
You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].
Return the array ans. Answers within 10-5 of the actual answer will be accepted.
Note that:
Kelvin = Celsius + 273.15Fahrenheit = Celsius * 1.80 + 32.00
Example 1:
Input: celsius = 36.50 Output: [309.65000,97.70000] Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
Example 2:
Input: celsius = 122.11 Output: [395.26000,251.79800] Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
Constraints:
0 <= celsius <= 1000
Approach Overview
Problem Overview: You are given a temperature value in Celsius. The task is to convert it into Kelvin and Fahrenheit using the standard mathematical formulas and return both results. The output is typically returned as a two-element array where the first value is Kelvin and the second is Fahrenheit.
Approach 1: Direct Calculation Approach (Time: O(1), Space: O(1))
The most straightforward solution applies the known temperature conversion formulas directly. Kelvin is computed as celsius + 273.15, and Fahrenheit is computed as celsius * 1.80 + 32.00. Since the problem only requires evaluating two arithmetic expressions, the algorithm performs a constant number of operations regardless of input size.
This approach uses basic arithmetic from math. You read the input value, calculate the two conversions, and store them in a result array or list. No loops, recursion, or extra data structures are required. The time complexity is O(1) because the number of operations never grows, and the space complexity is also O(1) since only two output values are stored.
This is the cleanest and most common solution used in practice. It mirrors how real systems implement temperature conversion utilities. Precision is handled automatically by floating‑point arithmetic in most programming languages.
Approach 2: Separate Functions for Each Conversion (Time: O(1), Space: O(1))
Another design separates the conversion logic into dedicated helper functions. One function converts Celsius to Kelvin, and another converts Celsius to Fahrenheit. Each function performs a single formula calculation and returns the result.
The main function calls both helpers and combines the results into an output array. This structure slightly increases code organization and readability, especially if conversions are reused elsewhere in a larger system. The computational work remains constant because each helper performs a single arithmetic operation.
This pattern reflects good modular design often seen in larger codebases that work with unit conversions or scientific utilities. The algorithmic complexity is still O(1) time and O(1) space because the number of computations and stored values remains fixed. The problem itself primarily tests understanding of mathematical formulas and basic implementation.
Recommended for interviews: The direct calculation approach is what interviewers expect. The problem focuses on correctly applying formulas and returning the results in the required format. Implementing it in a few lines shows clarity and correctness. The separate‑function version demonstrates clean code organization, but algorithmically both approaches are identical and run in constant time.
Approach 1: Direct Calculation Approach
This approach involves directly using the given formulas to convert the Celsius temperature to Kelvin and Fahrenheit. We simply apply the mathematical operations provided and return the results as an array.
In this C solution, we define a function convertTemperature that accepts a Celsius value and a pointer to a result array. The function calculates the Kelvin and Fahrenheit values and stores them in the result array. The main function demonstrates using this function with a sample Celsius value.
Complexity
Time Complexity: O(1) - Constant operations.
Space Complexity: O(1) - Only a fixed amount of space is used.
Approach 2: Separate Functions for Each Conversion
In this approach, we define separate functions for converting Celsius to Kelvin and Celsius to Fahrenheit. Each function handles just one conversion, and a main function invokes both to fetch the final results.
In this C solution, we define two separate functions: toKelvin and toFahrenheit. They handle individual conversions. A convertTemperature function is used to gather results into the output array, and main function is used for demonstration.
Complexity
Time Complexity: O(1) - Basic operations in separate functions.
Space Complexity: O(1) - A constant amount of space is used regardless of input.
Approach 3: Simulation
We can directly simulate according to the problem description.
The time complexity is O(1), and the space complexity is O(1).
Complexity Comparison
| Approach | Complexity |
|---|---|
| Direct Calculation Approach | Time Complexity: O(1) - Constant operations. |
| Separate Functions for Each Conversion | Time Complexity: O(1) - Basic operations in separate functions. |
| Simulation | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Calculation Approach | O(1) | O(1) | Best choice for interviews and simple implementations where only one conversion operation is required. |
| Separate Functions for Each Conversion | O(1) | O(1) | Useful in larger systems where temperature conversions are reused across multiple modules. |
Video Solution
Leetcode | 2469. Convert the Temperature | Easy | Java Solution • Developer Docs • 1,240 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Convert the Temperature easy or hard?
Convert the Temperature Python/Java solution
How to solve Convert the Temperature in O(n)?
What is the best approach for Convert the Temperature?
Is Convert the Temperature asked at Google/Amazon/Meta?
What data structure is used in Convert the Temperature?
What is the time complexity of Convert the Temperature?
Ready to solve this problem?
Practice Convert the Temperature with our built-in code editor and test cases.
Practice on FleetCodeTable of Contents
Practice this problem
Open in Editor