Time complexity

Time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm.

int i=0;
for (int i = 0; i < n; ++i) {
  1. Average case time complexity:- The average number of time algorithm is taking to complete the program.
  2. Best case time complexity:- The fastest number of times an algorithm takes to complete.

Space complexity

Space complexity is the amount of memory taken by our program to solve a particular task. A good programmer always focus on reducing space complexity.

public static int fibonacci(n) {
int x = 0, y = 1, z;
if (n === 0) {
return x;
if (n === 1) {
return y;
for (int i = 2; i <= n; ++i) {
z = x + y;
x = y;
y = z;
return z;
List<Integer> list=new ArrayList<>();
for (int i = 0; i < n; ++i) {
  1. O(n): if we simply iterate in one loop.
  2. O(log n): if we divide whole array into 2 parts and do search operation then our number of search will become less then this complexity occurs.
  3. O(1): if we traverse directly through index then this complexity occurs.


Similar to time complexity space complexity also plays a major role. We can wait for program to complete it’s execution but we can’t increase memory at run time so space complexity is more important.