Understanding Array Manipulation — Level 2: Implementation Guide
Dive into the technical details of array manipulation. Learn in-place algorithms, carry propagation techniques, and implement common operations with production-ready code.
Dive into the technical details of array manipulation. Learn in-place algorithms, carry propagation techniques, and implement common operations with production-ready code.
Author
Mr. Oz
Date
Read
8 mins
Level 2
In Level 1, we explored the bookshelf analogy. Now let's get our hands dirty with real implementation. We'll write production code to manipulate arrays in-place, handle carry propagation, and understand the patterns that make array operations efficient.
Let's implement the "add one" operation from Level 1. Here's the algorithm:
def plus_one(digits):
n = len(digits) - 1
# Process from right to left
while n >= 0:
if digits[n] < 9:
digits[n] += 1
return digits
digits[n] = 0
n -= 1
# All digits were 9, insert 1 at beginning
return [1] + digits
Key points:
public int[] plusOne(int[] digits) {
int n = digits.length - 1;
while (n >= 0) {
if (digits[n] < 9) {
digits[n]++;
return digits;
}
digits[n] = 0;
n--;
}
// All digits were 9, create new array
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
Key points:
digits.length instead of len(digits)++) adds 1 and returns the valuevector<int> plusOne(vector<int>& digits) {
int n = digits.size() - 1;
while (n >= 0) {
if (digits[n] < 9) {
digits[n]++;
return digits;
}
digits[n] = 0;
n--;
}
// All digits were 9, insert 1 at beginning
digits.insert(digits.begin(), 1);
return digits;
}
Key points:
&) to avoid copyingvector::insert() shifts all elements when inserting at the beginningsize() method instead of lengthBeyond the "plus one" operation, here are fundamental patterns you'll use frequently:
# Reverse an array in-place
def reverse_array(arr):
left, right = 0, len(arr) - 1
while left < right:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
return arr
# Remove all instances of a value
def remove_element(arr, val):
write_index = 0
for read_index in range(len(arr)):
if arr[read_index] != val:
arr[write_index] = arr[read_index]
write_index += 1
return arr[:write_index]
# Calculate running sum in-place
def running_sum(arr):
for i in range(1, len(arr)):
arr[i] += arr[i - 1]
return arr
❌ Pitfall 1: Integer Overflow
Problem: Converting the entire array to an integer fails for large numbers.
# BAD: Can overflow for large arrays
num = int(''.join(map(str, digits)))
num += 1
return list(map(int, str(num)))
Solution: Work with digits directly, as shown in our algorithm above.
❌ Pitfall 2: Creating Unnecessary Copies
Problem: Creating a new array when you could modify in-place.
# BAD: Uses extra space
result = digits.copy()
# modify result...
Solution: Modify the input array directly unless you need to preserve the original.
❌ Pitfall 3: Off-By-One Errors
Problem: Incorrect loop bounds or index calculations.
# BAD: Misses the first element
for i in range(len(digits) - 1, 0, -1):
# process...
Solution: Use range(len(digits) - 1, -1, -1) or while n >= 0 to include index 0.
write_index instead of iTime Complexity
Space Complexity
Level 1
Learn the fundamentals of array manipulation through an engaging bookshelf analogy.
Author
Mr. Oz
Duration
5 mins
Level 2
Implementation details, in-place algorithms, carry propagation, and common array operations.
Author
Mr. Oz
Duration
8 mins
Level 3
Memory layout, CPU cache performance, and optimization strategies for array operations.
Author
Mr. Oz
Duration
12 mins