Fahrenheit to Celsius

Easy
0/40
Average time to solve is 15m
profile
Contributed by
44 upvotes
Asked in company
SYSVINE

Problem statement

Ninja has been assigned to convert temperature from Fahrenheit to Celsius. He has been given a starting Fahrenheit Value (S), ending Fahrenheit value (E), and step size (W). Unfortunately, he does not know how to convert from Fahrenheit to Celsius. Please help him to find the result.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains three space-separated integers ‘S’, ‘E’, and ‘W’.
Output Format:
For each case, you need to print all the Fahrenheit-Celsius conversions starting from ‘S’ and ending at ‘E’ with step size ‘W’. Each entry should be printed in a new line and each line should contain the Fahrenheit temperature and the calculated Celsius temperature separated by a space. The output must be converted to the floor value of the integer.

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= S <= E <= 10000
1 <= W <= 50

Time limit: 1 sec.
Sample Input 1:
2
0 100 20
25 50 25
Sample Output 1:
0 -17
20 -6
40 4
60 15
80 26
100 37
25 -3
50 10
Explanation Of Sample Input 1:
Test case 1:
For the first test case of sample output 1, we start from Fahrenheit temperature 0 and move up to 100 with a step size of 20 units. For every temperature, we have a corresponding celsius temperature present in the output table.

Test case 2:   
For the second test case of sample output 1, we start from Fahrenheit temperature 25 and move up to 50 with a step size of 25 units. For every temperature, we have a corresponding celsius temperature present in the output table.
Sample Input 2:
2
50 150 15
600 650 7
Sample Output 2:
50 10
65 18
80 26
95 35
110 43
125 51
140 60
600 315
607 319
614 323
621 327
628 331
635 335
642 338
649 342
Explanation Of Sample Input 2:
Test case 1:
For the first test case of sample output 2, we have all celsius temperatures for the Fahrenheit range of 50 to 150.
Hint

Conversion of temperature.

Approaches (1)
Mathematics

Here, we can simply run a loop from starting point to the ending point where after each iteration, our loop counter will increase by step size w. For each iteration, we can calculate the celsius temperature and store it in a 2D array.

 

Algorithm:

 

  • Declare a 2D array for storing the result
  • Run a loop from ‘i’ = ‘S’ to ‘E’ with ‘i’ = ‘i’+ ‘W’
    • For each ‘i’, calculate the celsius temperature and put it in the array
  • Return the array
Time Complexity

O(N) where ‘N’ is the difference between the starting and ending temperature.

 

We run a for loop from start to endpoint, we point in a linear manner. So, our time complexity is O(N)

 

Space Complexity

O(N) where ‘N’ is the difference between the starting and ending temperature.

 

Though we take up a 2D array, for each row, we have just 2 columns. Therefore we need a total starting of O(2 * N) which can be generalized to O(N).

Code Solution
(100% EXP penalty)
Fahrenheit to Celsius
Full screen
Console