Space Complexity

Space Complexity

Continuing the second day of my 100-day coding challenge. I'm back to talk about space complexity of a program is defined as the input size of the program or the amount of space required space to execute successfully the functionalities of the code. Memory space that a program takes varies from system to system.

Learning about space complexity made me think that the types of code I studied previously in my coding journey, were a mess to the memory management of the computer system. Now following the concept of space complexity, I'm realising the difference between the codes I formed now and the ones I formed previously.

Space Complexity also comes in two types: -

  1. Input Space = As the name itself says it's the space required to store the input data by the user.

  2. Auxiliary Space = It's the type of space complexity that is occupied during the solvation of the problem.

Input(x) //take input and store it into a variable x --- 1 unit of space

Input(y) //take input and store it into another variable y --- 1 unit of space

z = x + y //storage location z is used to solve the problem between x and y. Hence, it's known as auxiliary space --- 1 unit of space

Total space occupied by the above code = 3 units of space

Space Complexity is one the basic concept among the topics of DSA. It helps to create effective programmes that uses the memory space effectively. Applying the concept of space complexity into our program helps us achieve effective results with the less memory usage and makes our code sensible to read, while also allowing us to avoid beginner mistakes in a code like declaring global variable isn't a recommended practice.