-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeComplexity.cpp
More file actions
44 lines (25 loc) · 1.21 KB
/
timeComplexity.cpp
File metadata and controls
44 lines (25 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int ans = n * (n + 1) / 2;
cout << ans;
// recurrence relation , recursion -> O(n)
// total number of recursive calls -> recursive tree for calculating the complexity
// question : O(n^(1/2)) vs O(n) , O(n^(1/2)) vs O(log n), which is bigger time complexity
// ans : 1-> O(n^(1/2)) since this is half of the o(n) and the number of operations will be reduced
// 2. O(log n), because of the logarithmic function
// no loop then time complexity is constant for here O(1)
// linear time complexity
// depending on a fixed number of running . Ex - kadane's algo O(n)
// swap and bubble sort have general complexities of -> O(n^2)/O(n^3)
// binary search has O(log n) , closes to constant and very optimized and most preferred
// sortings have O(nlog n) and also in greedy algos
// O(n!) is very worst , mainly found in the factorial
// space complexity can be defined as the SC = hight of the call stack * memory in each call
// fibonacci series tc -> O(2^n), a golden ratio is also referred to the fibo complexity -> O(1.618^n)
// merger sort -> O(nlogn)
return 0;
}