What is the difference between Auxiliary space and Space Complexity?
Last Updated :
06 Dec, 2023
Space complexity and Auxiliary space are two of the most often confused and interchangeably used terms when talking about the space complexity of a certain algorithm:
- Auxiliary Space: The extra space that is taken by an algorithm temporarily to finish its work
- Space Complexity: Space complexity is the total space taken by the algorithm with respect to the input size plus the auxiliary space that the algorithm uses.
Let us understand with the help of an example:
Example: Program to find the sum of an Array
Consider the below code to find the sum of an Array.
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Sum of given array is " << sum(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static int sum( int [] arr)
{
int sum = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String[] args)
{
int [] arr = { 12 , 3 , 4 , 15 };
int n = arr.length;
System.out.println( "Sum of given array is "
+ sum(arr));
}
}
|
Python
def array_sum(arr, n):
total_sum = 0
for i in range (n):
total_sum + = arr[i]
return total_sum
if __name__ = = "__main__" :
arr = [ 12 , 3 , 4 , 15 ]
n = len (arr)
print ( "Sum of given array is" , array_sum(arr, n))
|
C#
using System;
class Program {
static int Sum( int [] arr, int n)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
static void Main()
{
int [] arr = { 12, 3, 4, 15 };
int n = arr.Length;
Console.WriteLine( "Sum of given array is "
+ Sum(arr, n));
}
}
|
Javascript
function arraySum(arr, n) {
let totalSum = 0;
for (let i = 0; i < n; i++) {
totalSum += arr[i];
}
return totalSum;
}
if ( true ) {
const arr = [12, 3, 4, 15];
const n = arr.length;
console.log( "Sum of given array is" , arraySum(arr, n));
}
|
Output
Sum of given array is 34
In this program, as you can see, we have an Array, a variable to store the size of the Array, and the variable to store the sum of Array. So the space occupied by each variable will be:
- arr[]: Since this variable will store n elements of Array, so the space for arr[] will be O(n)
- n: This variable will store only the size of the Array, so the space for n will be O(1)
- sum: Similarly, sum stores only the sum of Array, so its space will also be O(1)
How to calculate Space Complexity for the code?
Now let us try to compute the overall space complexity of the above code. It will be:
max(all space used by variables) = max (n, 1, 1) = O(n)
How to calculate Auxiliary Space Complexity for the code?
Now let us try to compute the overall auxiliary space complexity of the above code.
Note: In this case, we cannot consider the space occupied by the Array itself, as it is required as an input in this problem.
Therefore, the auxiliary space for above code will be:
max(all space used by variables) = max (1, 1) = O(1)
Space Complexity vs. Auxiliary Space Complexity for the code
Now although the code remains the same, the Space complexity for the above code will be O(n), whereas Auxiliary space for the same will be O(1), as the initial input space is not considered.
Share your thoughts in the comments
Please Login to comment...