What is Space Complexity

What is Space Complexity?




  • Hello guys i hope you read my other articles about Run time Complexity and Big O notation where we saw why we need to know Time complexity of any algorithm we use or we build.
  • Now exactly like that there are some scenarios where we need to think about space complexity because our algorithm will grow as per input get grow and by understanding how much space our algorithm is taking we can improvise our algorithm wherever space complexity really makes some impact for example:- small devices like smart watch.
  • We can calculate space complexity using Big O notation.
  • Below is example:- 
  • In the example above we can see there is array which takes no. of inputs like 1000 or millions; but there is a for loop in which one loop variable is created and it is independent of our array therefore this method will allocate some additional memory as O(1) for this loop variable.
  • Now what will happen if i take on more array with same length as peoples array have.



  • Our method will assign it some memory O(n) and this what its space complexity its all depends no. of variable inside methods are get declare with mentioned size.





Comments

Popular Posts