Company: Hudson River Trading(HRT)_4th june_on campus _iit guwahti
Difficulty: medium
Imagine that you have a time machine. You are given an array years . You start in the year years[0] . First, you want to travel to years[1] , then to years[2] , and so on. Your task is to calculate the time required to visit all the years from the list in order. The time required to travel from the year A to the year B is calculated as follows: 0 hours if A = B 1 hour if A < B (going forwards in time) 2 hours if A > B (going backwards in time) Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(years.length) will fit within the execution time limit. Example For years = [2000, 1990, 2005, 2050] , the output should be solution(years) = 4 . First you go from 2000 to 1990 , which requires 2 hours. Then you go from 1990 to 2005 , which requires 1 hour. Then you go from 2005 to 2050 , which requires 1 hour. In total, you need 2 + 1 + 1 = 4 hours. For years = [2000, 2021, 2005] , the output should be solution(years) = 3 .