Apply Operations to an Array — Problem & Solution
Apply Operations to an Array solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.
Apply Operations to an Array is an easy coding problem on arrays, two pointers and simulation, asked in Amazon and Adobe interviews.
- Difficulty: Easy
- Topics: Arrays, Two Pointers, Simulation
- Asked at: Amazon, Adobe
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
You are given a 0-indexed array nums of size n. Apply n - 1 operations, for i from 0 to n - 2 in order:
- if
nums[i] == nums[i + 1], doublenums[i]and setnums[i + 1]to0; otherwise skip.
After all the operations, shift every 0 to the end of the array while keeping the non-zero elements in their relative order, and return the result.
Example 1
Input: nums = [1,2,2,1,1,0]
Output: [1,4,2,0,0,0]
Example 2
Input: nums = [0,1]
Output: [1,0]
Example 3
Input: nums = [3,3,3]
Output: [6,3,0]
Explanation: The first pair merges, which leaves the third 3 alone.
Constraints
2 <= nums.length <= 400 <= nums[i] <= 1000
Hints for Apply Operations to an Array
Hint 1
Do the merge pass strictly left to right — a merged pair can affect the next comparison.
Hint 2
Zeros never merge with each other; the rule requires a non-zero value.
Hint 3
Only after the whole merge pass do you compact the zeros to the end.
How to solve Apply Operations to an Array
Simulate the merge pass strictly left to right, then compact the non-zero values to the front and pad with zeros.
Approach
- For
ifrom 0 ton - 2: ifa[i] == a[i+1]and non-zero, doublea[i]and zeroa[i+1]. - Collect non-zero values in order and append zeros to restore the length.
Why it works
The operations are defined in index order and each can influence the next comparison (a just-zeroed slot never merges again), so the simulation must not be reordered; the final shift is a stable partition.
Complexity
- Time —
O(n) - Space —
O(n)
Pitfalls
- Zeros never merge with each other.
Reference solution
Python
def applyOperations(nums):
a = list(nums)
for i in range(len(a) - 1):
if a[i] == a[i + 1] and a[i] != 0:
a[i] *= 2
a[i + 1] = 0
out = [x for x in a if x != 0]
out.extend([0] * (len(a) - len(out)))
return outJava
public static int[] applyOperations(int[] nums) {
int[] a = Arrays.copyOf(nums, nums.length);
for (int i = 0; i + 1 < a.length; i++) {
if (a[i] == a[i + 1] && a[i] != 0) {
a[i] *= 2;
a[i + 1] = 0;
}
}
int[] out = new int[a.length];
int pos = 0;
for (int v : a) {
if (v != 0) out[pos++] = v;
}
return out;
}C++
vector<int> applyOperations(vector<int>& nums) {
vector<int> a = nums;
for (size_t i = 0; i + 1 < a.size(); i++) {
if (a[i] == a[i + 1] && a[i] != 0) {
a[i] *= 2;
a[i + 1] = 0;
}
}
vector<int> out(a.size(), 0);
int pos = 0;
for (int v : a) {
if (v != 0) out[pos++] = v;
}
return out;
}JavaScript
var applyOperations = function(nums) {
const a = nums.slice();
for (let i = 0; i + 1 < a.length; i++) {
if (a[i] === a[i + 1] && a[i] !== 0) {
a[i] *= 2;
a[i + 1] = 0;
}
}
const out = [];
for (let i = 0; i < a.length; i++) {
if (a[i] !== 0) out.push(a[i]);
}
while (out.length < a.length) out.push(0);
return out;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Two Pointers Technique