Space Complexity
Simply put, the space complexity of an algorithm or a code indicates how much space is used up while executing that code. int sum(int n) { int i,sum = 0; for(i=n;i>=1;i--) sum=sum+i; return sum; } The space complexity of this code is just O(1), because irrespective of the size of n, the amount of space used is constant (the variable 'sum' uses some fixed space). Causes of Space Complexity 1. Variables 2. Data Structures 3. Function Call 4. Allocations In using some data structure or an algorithm, there is always a tradeoff between time and space complexity. You have to decide, according to your problem statement, if you are optimizing for time or space or both, what resources yo...