OVERVIEW
In-place Modification refers to solving problems by modifying the input data
structure directly, without using extra space proportional to input size.
The goal is to achieve:
- O(1) auxiliary space
- Optimal time complexity
- Memory-efficient solutions
This pattern is very common in array problems and interview questions.
WHEN TO USE
Use in-place modification when:
- The problem explicitly asks for in-place changes
- Extra space is restricted
- The final state of the array matters more than preserving the original
- Elements can be safely overwritten or rearranged
TIME & SPACE
Time Complexity : Usually O(N)
Space Complexity : O(1)
CORE IDEA
- Reuse the input array for writing results
- Maintain one or more pointers to track write positions
- Carefully avoid overwriting unprocessed data
- Often combined with Two Pointers patterns
EXAMPLE 1: Remove Duplicates from Sorted Array
Problem:
Given a sorted array, remove duplicates in-place and return new length.
Logic:
- One pointer reads elements
- Another pointer writes unique values
def remove_duplicates(nums):
if not nums:
return 0
write = 1
for read in range(1, len(nums)):
if nums[read] != nums[read - 1]:
nums[write] = nums[read]
write += 1
return write
EXAMPLE 2: Move Zeroes to End
Problem:
Move all zeroes to the end while maintaining relative order of non-zero elements.
Logic:
- Write pointer tracks position for next non-zero
- Read pointer scans array
def move_zeroes(nums):
write = 0
for read in range(len(nums)):
if nums[read] != 0:
nums[write], nums[read] = nums[read], nums[write]
write += 1
return nums
EXAMPLE 3: Remove Element (Leetcode-style)
Problem:
Remove all occurrences of a given value in-place.
Logic:
- Skip unwanted values
- Overwrite them with valid ones
def remove_element(nums, val):
write = 0
for read in range(len(nums)):
if nums[read] != val:
nums[write] = nums[read]
write += 1
return write
EXAMPLE 4: Reverse Array In-Place
Problem:
Reverse an array using constant space.
Logic:
- Swap symmetric elements
- Move inward
def reverse_array(nums):
left = 0
right = len(nums) - 1
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1
return nums
EXAMPLE 5: Replace Elements with Greatest Element on Right Side
Problem:
Replace each element with the greatest element to its right.
Last element becomes -1.
Logic:
- Traverse from right
- Keep track of maximum so far
def replace_with_greatest(nums):
max_so_far = -1
for i in range(len(nums) - 1, -1, -1):
current = nums[i]
nums[i] = max_so_far
max_so_far = max(max_so_far, current)
return nums
IDENTIFICATION CHECKLIST
Ask these questions:
- Am I allowed to modify the input?
- Can I reuse array slots safely?
- Is extra space discouraged?
- Can pointer-based overwriting solve this?
If yes, use In-place Modification.
COMMON PITFALLS
- Overwriting values before they are processed
- Forgetting write pointer increments
- Assuming input order does not matter when it does
- Using extra arrays unnecessarily
SUMMARY
- Modify input directly
- Constant extra space
- Often paired with two pointers
- Essential for space-optimized solutions
Top comments (0)