0% found this document useful (0 votes)
44 views

L6 - Time & Space Complexity-1.2

The document discusses space complexity of algorithms. Space complexity is the amount of auxiliary space required by an algorithm. It includes space for input and any additional temporary space used. Common space complexities are O(1), O(n), O(n log n), and O(n^2). Examples are provided to illustrate how to determine the space complexity of different code snippets.

Uploaded by

Khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

L6 - Time & Space Complexity-1.2

The document discusses space complexity of algorithms. Space complexity is the amount of auxiliary space required by an algorithm. It includes space for input and any additional temporary space used. Common space complexities are O(1), O(n), O(n log n), and O(n^2). Examples are provided to illustrate how to determine the space complexity of different code snippets.

Uploaded by

Khushi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Space Complexity

n0
How To

Calculate Space O(n^2)

Operations
Complexity
O (n log n)

O(n)

Elements
What is Space Complexity?
Space Complexity
Topic/Course
of an algorithm is the total space taken by the algorithm
with respect to the input size.

Space complexity includes both Auxiliary space and space used by input.

Auxiliary Space is the extra space or temporary space used by an algorithm.

Space complexity is a parallel concept to time complexity.

If we need to create an array of size n, this will require O(n) space. If we


create a two-dimensional array of size n*n, this will require O(n2) space.
Topic/Course

Ex.1: Each call adds to the call stack ‘n’ Ex 2. There will be roughly O(n) calls to
times. Hence space complexity is O(n). pairSum. However, those calls do not
However, just because you have n calls exist simultaneously on the call
total doesn't mean it takes O(n) space. stack, so you only need O(1) space.
Consider
It is very possible for O(N) code to run faster than 0( 1) code for specific inputs.
Big O just describes the rate of increase.

For this reason, we drop the constants in runtime. An algorithm that one might
have described as 0(2N) is actually O(N).
Topic/Course

For Eg: for(i=0;i<N;i++){


print(x[1])
for(i=0;i<N;i++){ }
print(x[1]) for(i=0;i<N;i++){
print(y[1]) print(y[1])
} }
Both can be considered the same. The first is O(n) and second is O(2n) which
can be represented as O(n)

The second rule is that you can drop


the termis which aren’t significant.
In this case:-
The Time complexity of this code will be O(N^2) as for
i=0, it will
run 0 times, i=2 it woll run 2 times and hence O(N^2)

Topic/Course

An algorithm like binary search takes O(logN) as the time


complexity as n every iteration array N is divided into N/2
times.

When i=N, it will do N/2 comparisons in the worst case.


When i=N/2, it will do N/4 comparisons in the worst case

Hence the time complexity of binary search is O(logN).


Topic/Course
Predict the space complexity for the given below code snippet
int add (int n)
{
if (n <= 0)
{
return 0;
}
return n + add (n-1);
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


Predict the space complexity for the given below code snippet

int addSequence (int n)


{
int sum = 0;
for(int i = 0; i < n; i++){
sum += pairSum(i, i+1);
}
return sum;
}
int pairSum(int x, int y){
return x + y;
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


Predict the space complexity for the given below code snippet

int foo(int n) {
int* arr = new int[n];
for (int i = 0; i < n;
i++) {
arr[i] = i;
}
return arr[n-1];
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


Predict the space complexity for the given below code snippet

int sum = 0;
for (int i = 0; i < n; i+
+) {
sum += i;
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


Predict the space complexity for the given below code snippet

int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n-1) +
fibonacci(n-2);
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


Predict the space complexity for the given below code snippet
struct Node {
int data;
Node* next;
};

Node* head = nullptr;

void insert(int data) {


Node* newNode = new Node();
newNode->data = data;
newNode->next = head;
head = newNode;
}

A) O(1) B) O(n) C) O (n * log n) D) O(n * n)


THANK YOU

You might also like