Array Access and Update at Index in DSA C++ - Time & Space Complexity
We want to understand how fast we can get or change a value in an array by its position.
The question is: how does the time to access or update an element change as the array grows?
Analyze the time complexity of the following code snippet.
int arr[1000];
int index = 500;
// Access element at index
int value = arr[index];
// Update element at index
arr[index] = 42;
This code accesses and updates the element at a specific position in the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct access to an array element by index.
- How many times: Each access or update happens once per operation.
Accessing or updating an element takes the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant even as the array size grows.
Time Complexity: O(1)
This means accessing or updating an element by index takes the same short time no matter how big the array is.
[X] Wrong: "Accessing an element takes longer if the array is bigger because it has to look through all elements."
[OK] Correct: Arrays allow direct access by position, so the computer jumps straight to the spot without checking others.
Knowing that array access and update are very fast helps you choose the right data structure and explain your choices clearly in interviews.
"What if we used a linked list instead of an array? How would the time complexity for access and update change?"
Practice
arr[2] do in an array named arr?Solution
Step 1: Understand array indexing
Arrays use zero-based indexing, soarr[2]refers to the third element.Step 2: Identify the operation
The expressionarr[2]accesses (reads) the element at index 2 without changing it.Final Answer:
Accesses the element at index 2 of the array -> Option AQuick Check:
arr[2] means read element at index 2 [OK]
- Thinking arr[2] updates the element
- Confusing index 2 with index 0
- Assuming arr[2] deletes an element
nums to 10 in C++?Solution
Step 1: Recall zero-based indexing
The 4th element is at index 3 because counting starts from 0.Step 2: Check correct syntax for assignment
Use square brackets for indexing and single equals sign for assignment:nums[3] = 10;.Final Answer:
nums[3] = 10; -> Option BQuick Check:
Use nums[3] = 10; to update 4th element [OK]
- Using parentheses instead of brackets
- Using double equals (==) instead of single equals (=)
- Using curly braces for indexing
int arr[5] = {1, 2, 3, 4, 5};
arr[1] = arr[3] + 2;
std::cout << arr[1];Solution
Step 1: Identify initial array values
arr = [1, 2, 3, 4, 5]Step 2: Calculate new value for arr[1]
arr[3] is 4, so arr[1] = 4 + 2 = 6Step 3: Output the updated arr[1]
std::cout prints 6Final Answer:
6 -> Option AQuick Check:
arr[1] = arr[3] + 2 = 6 [OK]
- Confusing arr[1] and arr[3] values
- Forgetting to add 2
- Printing old value of arr[1]
data to 100:int data[3] = {10, 20, 30};
data(1) = 100;Solution
Step 1: Check array indexing syntax
Arrays use square brackets[]for indexing, not parentheses().Step 2: Identify the incorrect line
data(1) = 100;is invalid syntax; correct isdata[1] = 100;.Final Answer:
Using parentheses instead of square brackets for indexing -> Option CQuick Check:
Use square brackets [] to access array elements [OK]
- Using parentheses for indexing
- Assuming array size is wrong
- Missing semicolon errors
int scores[5] = {10, 20, 30, 40, 50};, which code snippet correctly doubles the value at index 2 and then updates index 4 to be the sum of the new index 2 value and index 0 value?Solution
Step 1: Initial array
scores = [10, 20, 30, 40, 50]Step 2: Double index 2
scores[2] = 30 * 2 = 60Step 3: Update index 4
scores[4] = 60 + 10 = 70Step 4: Trace options
- Snippet with *= 2 then [4] = [4] + [0]: [2]=60, [4]=50+10=60 (wrong)
- Snippet with *= 2 then [4] = [0] + [2]: [2]=60, [4]=10+60=70 (correct)
- Snippet with [2] = [2] * [2] then [4] = [0] + [2]: [2]=900, [4]=910 (wrong)
- Snippet with [2] = [2] * 2 then [4] = [2] + [4]: [2]=60, [4]=60+50=110 (wrong)
Final Answer:
scores[2] *= 2; scores[4] = scores[0] + scores[2]; -> Option DQuick Check:
scores[2] *= 2 (60); scores[4] = 10 + 60 (70) [OK]
- Adding to old index 4 value
- Squaring index 2 instead of doubling
- Summing new index 2 with index 4
Ready to prove it?
Pass the test and this pattern moves unknown
