CodeISM Class 3-A (Time Complexity)
CodeISM Class 3-A (Time Complexity)
Big Oh (O)
- It represents the upper bound of a function
- Used to approximate time complexity of a code
Eg. 1:
If f(n) = n - 2
f(n) <= 8.n
f(n) <= 8.g(n)
Where g(n) = n
By definition, f(n) = O(n)
Eg. 2: If f(n) = 3n2 + 5n + 8
By definition of Big Oh, f(n) = O(n2)
[ In polynomial functions, only see the highest degree
term to find Big Oh]
1.(2k) > n
2k > n
k approximately log2 (n)
- You can also use Fast input / output with cin, cout:
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
....
// All your code after this
}
4. Runtime error
int arr[100];
cout<<arr[1000]; // runtime-error
cout<<arr[-1]; // runtime-error
int32_t main()
{
int a=1000000000;
int b=1000000000;
int ans=a*b;
cout<<ans;
return 0;
}
int a=1000000000;
int b=1000000000;
long long ans=a*b;
cout<<ans;
// Output: -1486618624 (something like this)
Still, it will give the same wrong answer
Because you need to convert the integer to long long,
during the multiplication also.
float a=1.00000001;
float b=1.00000000;
if (a==b)
{
cout<<"equal";
}
else
{
cout<<"Not equal";
}
Eg. 1
int arr[n];
Eg. 2
int arr[n][m];