Assignment operators in C++ - Time & Space Complexity
We want to see how the time taken by assignment operators changes as the input size changes.
How does the cost of assigning values grow when working with bigger data?
Analyze the time complexity of the following code snippet.
int arr[1000];
for (int i = 0; i < 1000; i++) {
arr[i] = i * 2;
}
int x = 5;
x = 10;
This code assigns values to each element of an array and then assigns a new value to a single variable.
- Primary operation: Assignment inside the loop to each array element.
- How many times: 1000 times, once for each element.
As the number of elements increases, the number of assignments grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 assignments |
| 100 | 100 assignments |
| 1000 | 1000 assignments |
Pattern observation: The number of assignments grows directly with the input size.
Time Complexity: O(n)
This means the time to assign values grows in a straight line as the number of items increases.
[X] Wrong: "Assigning a single variable inside a loop makes the whole loop take constant time."
[OK] Correct: Even if the variable assignment is simple, doing it many times inside a loop adds up and grows with the number of repetitions.
Understanding how simple assignments add up helps you explain how loops affect performance in real code.
"What if we replaced the array with a linked list and assigned values similarly? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of
The operator+=+=adds the right side value to the left side variable and updates the variable with the new value.Step 2: Compare with other operators
=assigns a value,-=subtracts, and*=multiplies. Only+=adds and assigns.Final Answer:
+=-> Option AQuick Check:
Add and assign =+=[OK]
- Confusing = with +=
- Thinking -= adds values
- Mixing *= with addition
x by 5 and store the result back in x?Solution
Step 1: Identify the correct assignment operator syntax
The operator to multiply and assign is*=, sox *= 5;is correct.Step 2: Check other options for syntax errors
x =* 5;andx = 5*;are invalid syntax.x = 5 *;is incomplete.Final Answer:
x *= 5;-> Option BQuick Check:
Multiply and assign =*=[OK]
- Placing * after = instead of before
- Leaving incomplete expressions
- Confusing with simple assignment =
int a = 10; a += 5; a -= 3; std::cout << a;
Solution
Step 1: Calculate
Starting witha += 5;a = 10, adding 5 givesa = 15.Step 2: Calculate
Subtracting 3 from 15 givesa -= 3;a = 12.Final Answer:
12 -> Option AQuick Check:
10 + 5 - 3 = 12 [OK]
- Adding and subtracting in wrong order
- Confusing += and -= effects
- Ignoring the updated value after first operation
int x = 7; x =+ 3; std::cout << x;
Solution
Step 1: Analyze
This is not the addition assignmentx =+ 3;+=. Instead,=+means assign positive 3 to x.Step 2: Determine the value of x after assignment
x becomes 3, not 10, because=+is just assignment with unary plus.Final Answer:
3 -> Option CQuick Check:
=+is assignment, not addition [OK]
- Confusing =+ with += operator
- Expecting addition instead of assignment
- Assuming output is sum without checking syntax
int n = 20;, which code snippet correctly divides n by 4 and then subtracts 2 from the result, updating n accordingly?Solution
Step 1: Understand the required operations order
First dividenby 4, then subtract 2 from the result, updatingneach time.Step 2: Check each option
n /= 4; n -= 2;doesn /= 4;thenn -= 2;, matching the requirement.n -= 2; n /= 4;reverses order.n = (n - 2) / 4;subtracts first due to parentheses (18 / 4 = 4).n = n - 2 / 4;subtracts incorrectly.Step 3: Confirm correct update
n /= 4; n -= 2;updatesnstepwise as requested.Final Answer:
n /= 4; n -= 2; -> Option DQuick Check:
Divide then subtract =n /= 4; n -= 2;[OK]
- Reversing operation order
- Misunderstanding combined expressions
- Incorrect operator precedence assumptions
Ready to prove it?
Pass the test and this pattern moves unknown
