On the HackerRank coding environment, your coding solution has an execution time-limit depending on the chosen programming language. When you run your code, this is the time-limit within which your code is expected to read input, process and return output for all the test cases, failing which, you will see the "Terminated due to timeout" message.
Refer the HackerRank Coding Environment Specifications Page, to know the specific time-limit for your chosen programming language, and accordingly, optimize your code for faster execution to achieve the output.
Your test setter expects optimal solutions in coding Questions, and therefore, on the HackerRank coding environment, there are preset execution time-limits for different programming languages. For example, the Python language has a 10-second time-limit as compared to the C language which has a 2-second execution time limit.
For a detailed understanding, refer The "Terminated due to timeout" status in your HackerRank Tests topic.