Negative indexing in NumPy - Time & Space Complexity
We want to understand how fast operations run when using negative indexing in numpy arrays.
Specifically, how does the time to access elements change as the array grows?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000)
value = arr[-1]
value2 = arr[-10]
value3 = arr[-100]
This code accesses elements from the end of a numpy array using negative indexes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing an element by index in the array.
- How many times: Three times, each direct access to a single element.
Accessing an element by negative index is just like positive index access; it directly finds the element.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 |
| 100 | 3 |
| 1000 | 3 |
Pattern observation: The number of operations stays the same no matter how big the array is.
Time Complexity: O(1)
This means accessing an element by negative index takes the same short time no matter the array size.
[X] Wrong: "Negative indexing makes the program slower because it has to count from the end."
[OK] Correct: Numpy directly calculates the position internally, so it does not count through elements one by one.
Knowing that negative indexing is fast helps you write clean and efficient code without worrying about speed.
"What if we accessed a slice using negative indexes instead of single elements? How would the time complexity change?"
Practice
Solution
Step 1: Understand negative indexing concept
Negative indexing means counting elements from the end of the array, starting at -1 for the last element.Step 2: Compare options with concept
Only Access elements starting from the end of the array correctly describes accessing elements from the end using negative indexes.Final Answer:
Access elements starting from the end of the array -> Option BQuick Check:
Negative indexing = Access from end [OK]
- Thinking negative index changes array size
- Confusing negative indexing with sorting
- Assuming negative index accesses start only at beginning
arr using negative indexing?Solution
Step 1: Recall negative index for last element
In numpy,-1is used to access the last element of an array.Step 2: Check each option
arr[-1] usesarr[-1]which correctly accesses the last element. Others do not.Final Answer:
arr[-1] -> Option DQuick Check:
Last element = arr[-1] [OK]
- Using arr[-0] which is same as arr[0]
- Using positive index 1 which is second element
- Confusing zero index with negative zero
arr = np.array([10, 20, 30, 40, 50]), what is the output of arr[-3]?Solution
Step 1: Understand negative index -3
Negative index -3 means counting from the end: last is -1 (50), second last -2 (40), third last -3 (30).Step 2: Identify element at index -3
The element atarr[-3]is 30.Final Answer:
30 -> Option CQuick Check:
arr[-3] = 30 [OK]
- Counting negative index from start instead of end
- Mixing up index -3 with positive index 3
- Off-by-one errors in counting
import numpy as np arr = np.array([5, 10, 15, 20]) print(arr[-5])
What is the error in this code?
Solution
Step 1: Check array size and index used
Array has 4 elements, valid negative indexes are -1 to -4.Step 2: Understand index -5 usage
Index -5 is outside valid range, causing IndexError.Final Answer:
IndexError: index -5 is out of bounds for axis 0 with size 4 -> Option AQuick Check:
Negative index out of range = IndexError [OK]
- Assuming negative index wraps around infinitely
- Thinking negative index causes syntax error
- Expecting last element instead of error
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]). What does arr[-1, -2] return?Solution
Step 1: Understand negative indexing in 2D arrays
For rows, -1 means last row: [7, 8, 9]. For columns, -2 means second last column.Step 2: Identify element at arr[-1, -2]
Last row is [7, 8, 9], second last column is element 8.Final Answer:
8 -> Option AQuick Check:
arr[-1, -2] = 8 [OK]
- Mixing row and column indexes
- Using positive indexes instead of negative
- Confusing which axis negative index applies to
Ready to prove it?
Pass the test and this pattern moves unknown
