how to improve time period in our codes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

how to improve time period in our codes?

I was solving some challenges of hackerank. they have some time limits for the input? my first question is what is time limit for the input? and what are the ways in which we can improve it? // they give me results as runtime error as my codes usually exceed the time limit edit: sry! I dunno why there are too much tags I entered only four

20th Jan 2018, 7:01 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
10 Answers
+ 3
You can visit the following links, and read books and data on Competitive Programming in the language of your choice : https://www.geeksforgeeks.org/writing-cc-code-efficiently-in-competitive-programming/ https://www.thegeekstuff.com/2015/01/c-cpp-code-optimization https://stackoverflow.com/questions/6852670/how-to-write-fast-low-level-code Also, you may try learning built-in functions, as they are usually handy when your implmentation is slower than expected.
22nd Jan 2018, 2:38 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 4
The time limit error usually occurs when your code is quite long, or uses multiple iterations or reserves a large amount of memory. At the compiler level, even extra iterations in inplemented algorithms may create a large difference, especially when the time constraints are usually 1s or 2s. So the best thing to do is review the challenge discussions and the highest rated codes posted in these challenges, and learn from them. Find how they tackled the same challenge and what was the factor that improved their code to the next level. In the beginning, it may be difficult to understand codes directly, so learning the basic algorithms may help as well. Eventually, you will be able to see improvement. The most important factor though is participating in more and more challenges so that you eventually get the hang of what is to be done.
21st Jan 2018, 4:23 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
what can I do for this in order to get my submission accepted in hackerank?
21st Jan 2018, 2:02 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
thnx sir😥😥
21st Jan 2018, 3:28 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
https://code.sololearn.com/c5OFTn4rVR7C/?ref=app here's the code can u tell me what cud be the possible reasons?
21st Jan 2018, 4:35 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
@kinshuk vasisht now i m getting it its the problem with my algo. i have to create a new one i think one more question how can we know about the stuffs which makes our program slow and such similar questions is there any link to know about such types of problems thnx in advance
21st Jan 2018, 5:23 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 3
@kinshuk thnx man!!! you helped me a lot!!!
22nd Jan 2018, 2:59 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 2
thnx and yes i think my code uses large amount of memory thats why it shows Runtime Error - SIGSEGV
21st Jan 2018, 4:26 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 1
@Saurabh Tiwari The SIGSEGV error is not just due to a large amount of memory. Its more probably raised during a memory leak or accessing invalid memory locations.
21st Jan 2018, 4:33 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
The code runs for me on GCC 4.9.2, but it seems the error occured as you simply initialized the array with a variable instead of a constant and that is no longer allowed without use of pointers. Also, you shouldn't declare variables inside loops as that can make programs slow.
21st Jan 2018, 4:51 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar