2460. Apply Operations to an Array LeetCode Solution

In this guide, you will get 2460. Apply Operations to an Array LeetCode Solution with the best time and space complexity. The solution to Apply Operations to an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Apply Operations to an Array solution in C++
  4. Apply Operations to an Array solution in Java
  5. Apply Operations to an Array solution in Python
  6. Additional Resources
2460. Apply Operations to an Array LeetCode Solution image

Problem Statement of Apply Operations to an Array

You are given a 0-indexed array nums of size n consisting of non-negative integers.
You need to apply n – 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:

If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.

After performing all the operations, shift all the 0’s to the end of the array.

For example, the array [1,0,2,0,0,1] after shifting all its 0’s to the end, is [1,2,1,0,0,0].

Return the resulting array.
Note that the operations are applied sequentially, not all at once.

Example 1:

Input: nums = [1,2,2,1,1,0]
Output: [1,4,2,0,0,0]
Explanation: We do the following operations:
– i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
– i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
– i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
– i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
– i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
After that, we shift the 0’s to the end, which gives the array [1,4,2,0,0,0].

See also  2054. Two Best Non-Overlapping Events LeetCode Solution

Example 2:

Input: nums = [0,1]
Output: [1,0]
Explanation: No operation can be applied, we just shift the 0 to the end.

Constraints:

2 <= nums.length <= 2000
0 <= nums[i] <= 1000

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2460. Apply Operations to an Array LeetCode Solution in C++

class Solution {
 public:
  vector<int> applyOperations(vector<int>& nums) {
    vector<int> ans(nums.size());

    for (int i = 0; i + 1 < nums.size(); ++i)
      if (nums[i] == nums[i + 1]) {
        nums[i] *= 2;
        nums[i + 1] = 0;
      }

    int i = 0;
    for (const int num : nums)
      if (num > 0)
        ans[i++] = num;

    return ans;
  }
};
/* code provided by PROGIEZ */

2460. Apply Operations to an Array LeetCode Solution in Java

class Solution {
  public int[] applyOperations(int[] nums) {
    int[] ans = new int[nums.length];

    for (int i = 0; i + 1 < nums.length; ++i)
      if (nums[i] == nums[i + 1]) {
        nums[i] *= 2;
        nums[i + 1] = 0;
      }

    int i = 0;
    for (final int num : nums)
      if (num > 0)
        ans[i++] = num;

    return ans;
  }
}
// code provided by PROGIEZ

2460. Apply Operations to an Array LeetCode Solution in Python

class Solution:
  def applyOperations(self, nums: list[int]) -> list[int]:
    ans = [0] * len(nums)

    for i in range(len(nums) - 1):
      if nums[i] == nums[i + 1]:
        nums[i] *= 2
        nums[i + 1] = 0

    i = 0
    for num in nums:
      if num > 0:
        ans[i] = num
        i += 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.