Array Access and Update at Index in DSA Java - 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 grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
int[] arr = new int[1000];
// Access element at index 500
int value = arr[500];
// Update element at index 500
arr[500] = 42;
This code accesses and updates a single element in the array by its index.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct access to an array element by index.
- How many times: Exactly once for each access or update.
Accessing or updating an element by index takes the same amount of 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 if the array size grows.
Time Complexity: O(1)
This means accessing or updating an element by index takes the same fixed time regardless of array size.
[X] Wrong: "Accessing an element by index takes longer if the array is bigger."
[OK] Correct: Arrays allow direct jumps to any position, so the time does not depend on size.
Knowing that array access and update are constant time helps you choose the right data structure for fast lookups and changes.
"What if we had to search for a value instead of accessing by index? How would the time complexity change?"
Practice
arr[2] do in a Java array named arr?Solution
Step 1: Understand array indexing
In Java, arrays use zero-based indexing, soarr[2]accesses the third element.Step 2: Identify the operation
The expression reads the value stored at index 2 without modifying the array.Final Answer:
Accesses the element at index 2 of the array -> Option CQuick Check:
arr[2] means get element at index 2 [OK]
- Thinking arr[2] adds or deletes elements
- Confusing index with array length
- Assuming indexing starts at 1
nums to 10?Solution
Step 1: Recall Java array update syntax
In Java, array elements are accessed and updated using square brackets:array[index] = value;Step 2: Match correct syntax
Onlynums[1] = 10;uses correct brackets and assignment.Final Answer:
nums[1] = 10; -> Option BQuick Check:
Use square brackets for array index update [OK]
- Using parentheses () instead of brackets []
- Using curly braces {} which are invalid for arrays
- Trying to access with dot notation
int[] arr = {5, 10, 15, 20};
arr[2] = 25;
System.out.println(arr[2]);Solution
Step 1: Understand initial array values
The arrayarrstarts as [5, 10, 15, 20].Step 2: Update element at index 2
We setarr[2] = 25;changing the third element from 15 to 25.Step 3: Print updated element
System.out.println(arr[2]);prints the updated value 25.Final Answer:
25 -> Option AQuick Check:
arr[2] updated to 25, so output is 25 [OK]
- Printing old value before update
- Confusing index 2 with index 3
- Assuming array is unchanged
int[] numbers = {1, 2, 3};
numbers[3] = 4;Solution
Step 1: Check array size and valid indices
The arraynumbershas length 3, valid indices are 0, 1, 2.Step 2: Analyze index used for update
Attemptingnumbers[3] = 4;accesses index 3, which is out of bounds.Step 3: Identify error type
This causes a runtimeArrayIndexOutOfBoundsException.Final Answer:
ArrayIndexOutOfBoundsException at runtime -> Option AQuick Check:
Index 3 invalid for length 3 array [OK]
- Assuming index 3 is valid for length 3 array
- Confusing compile-time and runtime errors
- Ignoring zero-based indexing
int[] data = {2, 4, 6, 8, 10};, which code snippet correctly doubles the value at index 3 and then prints the entire array?Solution
Step 1: Identify correct index and operation
Index 3 corresponds to the fourth element (value 8). Doubling means multiplying by 2.Step 2: Check loop correctness for printing
The loop must run from 0 todata.length - 1to print all elements without error.Step 3: Verify each option
data[3] = data[3] * 2; for(int i = 0; i < data.length; i++) { System.out.print(data[i] + " "); } correctly doublesdata[3]and loops from 0 to length-1. data[3] *= 2; for(int i = 1; i <= data.length; i++) { System.out.print(data[i] + " "); } uses invalid loop bounds and will cause runtime error. data[3] = data[3] + 2; for(int i = 0; i < data.length; i++) { System.out.print(data[i] + " "); } adds 2 instead of doubling. data[4] = data[4] * 2; for(int i = 0; i < data.length; i++) { System.out.print(data[i] + " "); } doubles wrong index (4).Final Answer:
data[3] = data[3] * 2; for(int i = 0; i < data.length; i++) { System.out.print(data[i] + " "); } -> Option DQuick Check:
Correct index and loop bounds ensure proper update and print [OK]
- Using wrong index for update
- Incorrect loop bounds causing errors
- Adding instead of multiplying
Ready to prove it?
Pass the test and this pattern moves unknown
