Basic Array Indexing
Problem Statement
Access elements from a NumPy array using basic indexing.
Background
NumPy supports powerful indexing:
- arr[i] - single element
- arr[i, j] - element at row i, column j (for 2D)
- Negative indices count from end
Your Task
Write a function get_elements(arr) that returns a dictionary with:
- "first": First element
- "last": Last element
- "middle": Middle element (use len//2 for index)
Output Format
Return a dictionary with exactly these three keys.
Example:
[10, 20, 30, 40, 50]
{'first': 10, 'last': 50, 'middle': 30}Index 0 for first, -1 for last, len//2 for middle
Constraints:
- Use standard indexing arr[i]
- Use negative indexing for last element
- Array will have at least 1 element
Background Knowledge
NumPy Array Indexing Fundamentals
NumPy arrays are the standard representation for numerical data in Python. They support efficient element access through indexing, which is a fundamental operation in array programming. NumPy indexing follows these core principles:
- Zero-based indexing: The first element is at index 0
- Positive indices: Access elements from the beginning (0, 1, 2,...)
- Negative indices: Access elements from the end (-1 is the last element, -2 is second-to-last, etc.)
- Direct access: Retrieving an element by index takes constant time O(1)
For 1D arrays, arr[i] returns the element at position i. For multi-dimensional arrays, arr[i, j] accesses row i, column j. This indexing mechanism is consistent across NumPy operations and is designed to be intuitive and efficient.
Algorithm Approach
This problem requires three basic indexing operations:
- First element: Access index 0 directly
- Last element: Use negative indexing with index -1 (or equivalently, len(arr) - 1)
- Middle element: Calculate the middle index as len(arr) // 2 using integer division
The approach leverages NumPy's constant-time indexing capability, making this an O(1) operation per access.
Step-by-Step Strategy
Step 1: Get the first element
first = arr
Step 2: Get the last element using negative indexing
last = arr[-1]
Step 3: Calculate and access the middle element
middle_index = len(arr) // 2
middle = arr[middle_index]
Step 4: Return results as a dictionary
return {"first": first, "last": last, "middle": middle}
Complete Solution:
import numpy as np
def get_elements(arr):
return {
"first": arr,
"last": arr[-1],
"middle": arr[len(arr) // 2]
}
Common Pitfalls
- Off-by-one errors: Remember that indexing is zero-based; the first element is at index 0, not 1
- Forgetting negative indexing: Using arr[len(arr) - 1] instead of arr[-1] is valid but less Pythonic
- Integer division: Use // (floor division) instead of / (float division) to get an integer index
- Empty arrays: The constraints guarantee at least 1 element, but in production code, always validate array length
- Type conversion: Ensure the result values are extracted as Python scalars (NumPy handles this automatically with single-element indexing)
Time & Space Complexity
- Time Complexity: O(1) — Each indexing operation (first, last, middle) is a constant-time lookup regardless of array size
- Space Complexity: O(1) — The dictionary stores only three scalar values, consuming constant extra space independent of input size