0% found this document useful (0 votes)
33 views11 pages

Time Complexity Calculation

calculation of time complexity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Time Complexity Calculation

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

Time complexity calculation

Calculate the time complexity


1 Start
2 For i fromt I to 10 do
3 Print i
4 End For
5 End

Let’s analyze your pseudocode step by step:

pgsql

1 Start
2 For i from 1 to 10 do
3 Print i
4 End For
5 End

Step 1: Loop

The For loop runs from i = 1 to i = 10.

That means it executes 10 times.

Step 2: Work inside the loop

The only operation inside is Print i, which is an O(1) operation.

Step 3: Combine

Total work = 10 × O(1) = O(10).

Step 4: Simplify

In asymptotic notation, constants are dropped.


So, the time complexity = O(1) (constant time), since the loop always runs a fixed number of
times, independent of input size.

✅ Final Answer: The time complexity is O(1).


Do you want me to also calculate the space complexity for this?

didnt got it

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/11
No problem 👍 let me explain slowly with clarity.
Your code is basically this:

mathematica

Start
For i = 1 to 10
Print i
End For
End

Step 1: Look at the loop

The loop runs from 1 to 10.

That means the loop executes exactly 10 times.

Step 2: What happens inside the loop?

Each time, it just does Print i.

Printing is a constant-time operation → O(1).

Step 3: Multiply

Total time = 10 × O(1) = O(10).

Step 4: Big-O rule

In Big-O notation, we ignore constants (like 10).

So, O(10) = O(1).

That’s why the final answer is:


✅ Time Complexity = O(1) (constant time)

👉 If instead of 1 to 10, the loop was 1 to n (where n is an input), then the answer would be O(n).

Do you want me to show you the difference between 1 to 10 (fixed) vs 1 to n (variable)?

how calculation is done??

Great question 👌 you want to know how exactly we calculate the time complexity step by step. Let’s
do it carefully.

Your pseudocode:

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/11
mathematica

For i = 1 to 10
Print i
End For

Step 1: Count the number of iterations

The loop runs from 1 to 10.

That means the loop executes 10 times.

Step 2: Cost of each operation

Inside the loop we have only one statement:

Print i → this is a constant-time operation = O(1).

Step 3: Multiply

Total work done =

(Number of iterations) × (Cost per iteration)

= 10 × O(1) = O(10)

Step 4: Apply Big-O simplification

In time complexity analysis, constants don’t matter.


So O(10) → simplified to O(1).

✅ Final Answer: Time complexity = O(1) (constant time).


⚡ If the loop was , the calculation would be:
1 to n

n × O(1) = O(n)

Do you want me to also draw a small table (iteration vs work) so you can see how the calculation builds
up?

Calculate the Space complexity


1 Start
2 For i from 1 to 10 do
3 Print i

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/11
4 End For
5 End

Perfect 👍 let’s calculate the space complexity step by step for your code:
Code:

mathematica

Start
For i = 1 to 10
Print i
End For
End

Step 1: Variables used

i → the loop counter.

No extra arrays, lists, or data structures are being created.

So, only one variable is needed.

Step 2: Space used inside the loop

The Print i statement doesn’t store anything; it just outputs the value.
No extra memory is used per iteration.

Step 3: Total space

Only O(1) memory is used (for variable i and maybe a few constants).

This doesn’t grow with input size.

✅ Final Answer:
Space Complexity = O(1) (constant space).

👉 Want me to also show you what would happen if instead of printing, the code was storing values in
an array?

is this the correct format to do??

Yes 👍 you’re already on the right track.


Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/11
When you’re asked to calculate time complexity or space complexity, the standard format is
something like this:

✅ Example Format
Code:

mathematica

For i = 1 to 10
Print i
End For

Time Complexity:

1. The loop runs from 1 to 10 → 10 iterations.


2. Each iteration does Print i → O(1) work.

3. Total work = 10 × O(1) = O(10) → O(1) after simplification.

Answer: Time Complexity = O(1) (constant time).

Space Complexity:

1. Only one variable i is used.


2. No extra data structures are created.
3. Space = O(1) (constant space).

Answer: Space Complexity = O(1) (constant space).

👉 This is the clean, step-by-step format most teachers/professors expect.


If the loop went from 1 to n instead of 1 to 10, then your steps would look the same — but the answer
would be O(n) instead of O(1) for time.

Do you want me to show you the general template you can copy-paste for any algorithm (with
placeholders for steps)?

i mean calculation
will university give me full marks for this way??

Ahh I get you now 🙌 — you’re asking whether the way we’re writing/calculating the complexity step
by step is the correct exam/university style that will get full marks.

Short answer: ✅ Yes, if you show the steps properly, you’ll get full marks.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/11
Here’s how to present it in a university exam–friendly format:

Example:

Algorithm:

mathematica

For i = 1 to 10
Print i
End For

Time Complexity Calculation

1. Loop runs from 1 to 10 → total 10 iterations.

2. Work inside loop = Print i → O(1).


3. Total = 10 × O(1) = O(10).
4. In Big-O, constants are ignored → O(1).

Final Answer: Time Complexity = O(1) (constant time).

Space Complexity Calculation

1. Variable used: i (loop counter).

2. No extra memory (no arrays, lists, etc.).


3. Total = O(1) (constant space).

Final Answer: Space Complexity = O(1).

✅ This is exactly the format teachers look for:


Show iterations count

Show operation cost

Multiply
Simplify

Give final Big-O answer

👉 Do you want me to make you a universal template for writing complexity answers in exams (so you
can just plug in any code and solve)?

yeah than give answer in that format

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/11
which teachers look for

Perfect ✅ let’s lock this in exam-friendly style so you can just copy the pattern in any question.
Algorithm

mathematica

Start
For i = 1 to 10
Print i
End For
End

Time Complexity

1. The loop runs from 1 to 10 → total 10 iterations.

2. Each iteration executes Print i → cost = O(1).


3. Total cost = 10 × O(1) = O(10).
4. Ignore constant → O(1).

Final Answer: Time Complexity = O(1) (constant time).

Space Complexity

1. Variable used: i (loop counter).

2. No additional data structures are created.


3. Space required = O(1).

Final Answer: Space Complexity = O(1) (constant space).

⚡ This is the exact step-by-step style (iterations → per-step cost → total → simplified → final answer)
that teachers/university evaluators expect.

Do you want me to also make a general template (like a ready-made skeleton) you can use for any
algorithm in exams?

write seperate seperate for time complexity and space complexity

Got it 👍
I’ll write two separate sections the way exam evaluators usually prefer.

✦ Time Complexity Analysis


Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/11
1. The loop runs from 1 to 10 → total of 10 iterations.

2. Each iteration executes Print i → cost = O(1).


3. Total cost = 10 × O(1) = O(10).
4. In Big-O notation, constants are ignored.

Final Answer: Time Complexity = O(1) (constant time).

✦ Space Complexity Analysis


1. The algorithm uses one variable i (loop counter).

2. No extra memory structures (arrays, lists, etc.) are used.

3. Hence, memory requirement does not grow with input size.

Final Answer: Space Complexity = O(1) (constant space).

👉 This is exactly the separate structured format most universities want.


Do you want me to also show how the answer would look if the loop was from 1 to n (so you can
compare fixed vs variable input)?

didnt got the space complexity

No worries 👍 let me explain space complexity very slowly and clearly with your example.
📌 What is Space Complexity?
It measures how much memory (space in RAM) an algorithm uses while running.

This includes variables, arrays, objects, function calls, etc.

📌 Your Algorithm:
mathematica

For i = 1 to 10
Print i
End For

Step 1: Variables

Only one variable is needed: i (the loop counter).

Whether the loop runs 10 times, 100 times, or 1000 times, you still need just one variable i.

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/11
Step 2: Inside the loop

The statement Print i just displays the value of i.

It does not store anything extra in memory (like in an array).

Step 3: Total Space

Since only i is used → memory required = constant.


Constant space is written as O(1).

✅ Final Answer:
Space Complexity = O(1) (constant space)

👉 Think of it like this:


If you only print values → space stays O(1).

If you store values in an array → space becomes O(n).

Would you like me to also show the same code but storing values in an array so you can clearly see
the difference between O(1) and O(n) space?

how calculation is done??

Excellent question 👌 You want to see step-by-step how we “calculate” space complexity, just like we
did for time. Let’s do that carefully.

Algorithm

mathematica

For i = 1 to 10
Print i
End For

📌 Space Complexity Calculation


Step 1: Identify variables

We have one variable → i (the loop counter).

So memory used = space for i.


Let’s call this = O(1) (constant).

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/11
Step 2: Check inside the loop

The statement Print i only outputs the value of i.

Printing does not store anything extra in memory.


No arrays, no lists, no function calls that use extra memory.

So, additional space = O(0) = 0.

Step 3: Add them up

Total Space = Space for variable i + Extra space in loop

= O(1) + 0 = O(1)

✅ Final Answer: Space Complexity = O(1) (constant space).


👉 If instead of printing, the code did something like:
css

Store i in an array A[10]

Then the calculation would be:

Variable i = O(1)

Array A[10] = O(10) = O(n)

So total space = O(n).

Do you want me to prepare a general calculation template for space complexity (like: Step 1 variables
→ Step 2 inside loop → Step 3 data structures → Total)?

i want easy calculation

Got it 👍 You don’t need over-complicated steps. Let me give you a simple and exam-friendly way to
calculate space complexity.

Algorithm

mathematica

For i = 1 to 10
Print i
End For

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/11
Easy Space Complexity Calculation

1. Variable used: i → needs constant memory.

2. No arrays or extra data structures.


3. Printing does not take extra memory.

Answer: Space Complexity = O(1)

👉 That’s it! Just remember:


Only variables → O(1)

Array / list of size n → O(n)

2D array n×n → O(n²)

Do you want me to make a 1-page shortcut chart with these quick rules for time & space complexity?

Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/11

You might also like