Apply Operations to an Array

Easy
270.

Apply Operations to an Array

Apply Operations to an Array is an easy coding problem on arrays, two pointers and simulation, asked in Amazon and Adobe interviews.

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], double nums[i] and set nums[i + 1] to 0; 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 <= 40
  • 0 <= nums[i] <= 1000
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var applyOperations = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

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], double nums[i] and set nums[i + 1] to 0; 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 <= 40
  • 0 <= nums[i] <= 1000

Hints for Apply Operations to an Array

  1. Hint 1

    Do the merge pass strictly left to right — a merged pair can affect the next comparison.

  2. Hint 2

    Zeros never merge with each other; the rule requires a non-zero value.

  3. 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

  1. For i from 0 to n - 2: if a[i] == a[i+1] and non-zero, double a[i] and zero a[i+1].
  2. 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 out

Java

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