# What is the difference between Auxiliary space and Space Complexity?

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++

 `/* C++ Program to find sum of elements``in a given array */``#include ``using` `namespace` `std;` `// function to return sum of elements``// in an array of size n``int` `sum(``int` `arr[], ``int` `n)``{``    ``int` `sum = 0; ``// initialize sum` `    ``// Iterate through all elements``    ``// and add them to sum``    ``for` `(``int` `i = 0; i < n; i++)``        ``sum += arr[i];` `    ``return` `sum;``}` `// Driver code``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 {``    ``// Function to return the sum of elements in an array``    ``public` `static` `int` `sum(``int``[] arr)``    ``{``        ``int` `sum = ``0``; ``// Initialize sum` `        ``// Iterate through all elements and add them to sum``        ``for` `(``int` `i = ``0``; i < arr.length; i++) {``            ``sum += arr[i];``        ``}` `        ``return` `sum;``    ``}` `    ``// Driver code``    ``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

 `# Function to return the sum of elements in an array of size n``def` `array_sum(arr, n):``    ``total_sum ``=` `0`  `# initialize sum` `    ``# Iterate through all elements and add them to sum``    ``for` `i ``in` `range``(n):``        ``total_sum ``+``=` `arr[i]` `    ``return` `total_sum`  `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ``arr ``=` `[``12``, ``3``, ``4``, ``15``]``    ``n ``=` `len``(arr)` `    ``# Call the array_sum function and print the result``    ``print``(``"Sum of given array is"``, array_sum(arr, n))`

## C#

 `using` `System;` `class` `Program {``    ``// Function to return the sum of elements``    ``static` `int` `Sum(``int``[] arr, ``int` `n)``    ``{``        ``int` `sum = 0; ``// Initialize sum` `        ``// Iterate through all elements and add them to the``        ``// sum``        ``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 to return the sum of elements in an array of size n``function` `arraySum(arr, n) {``    ``let totalSum = 0; ``// Initialize sum` `    ``// Iterate through all elements and add them to sum``    ``for` `(let i = 0; i < n; i++) {``        ``totalSum += arr[i];``    ``}` `    ``return` `totalSum;``}` `// Driver code``if` `(``true``) {``    ``const arr = [12, 3, 4, 15];``    ``const n = arr.length;` `    ``// Call the arraySum function and print the result``    ``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.

Previous
Next