how can i find the time complexity of these two different algorithms to know which is better ??? 🙂 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how can i find the time complexity of these two different algorithms to know which is better ??? 🙂

https://code.sololearn.com/cGNETfac4UkH/?ref=app

18th Feb 2022, 5:36 AM
Davinder Kumar
Davinder Kumar - avatar
6 Answers
+ 5
First: you have a single calculation, it does not depend on the size of the data, so it is constant time O(1) Second: you loop through the array once, so the time depends on the size of the data with linear complexity O(n)
18th Feb 2022, 5:51 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Find which is the most repeated line of code How much is it repeated ? Ex main (){ getline(input) n = input.length for ( i = 0; i < n; i++ ) } Most repeated is the foor loop Which repeats n times
18th Feb 2022, 5:43 AM
Raul Ramirez
Raul Ramirez - avatar
+ 1
Tibor Santa helpful...
18th Feb 2022, 5:54 AM
Davinder Kumar
Davinder Kumar - avatar
0
In your case the top one is only done once There for its faster
18th Feb 2022, 5:46 AM
Raul Ramirez
Raul Ramirez - avatar
0
I know this but what if there are several lines of code and i have to find time complexity of that code then how will i find .... is there any mathematical formula..?
18th Feb 2022, 5:50 AM
Davinder Kumar
Davinder Kumar - avatar
0
imabhiiiiii🥱 You could just run a few test and see how its time is growing as you increase the size of data
18th Feb 2022, 5:57 AM
Raul Ramirez
Raul Ramirez - avatar