Menu Close

Leetcode 2460: Apply Operations to an Array Solution

Here, we will see how to solve Apply Operations to an Array Solution of leet code 2460 problem.

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 0s 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].

Example 2:

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

Apply Operations to an Array Solution code in C++

Code 1:

class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        for(int i = 0; i < nums.size() - 1; i++) {
            if(nums[i] == nums[i+1]) {
                nums[i] *= 2;
                nums[i+1] = 0;
            }
        }
        int i = 0;
        for(int j = 0; j < nums.size();) {
            if(nums[j] == 0) {
                j++;
            } else {
                nums[i] = nums[j];
                i++;
                j++;
            }
        }
        while(i < nums.size()) {
            nums[i++] = 0;
        }
        return nums;
    }
};

Code 2:

class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n - 1; ++i) {
            if (nums[i] == nums[i+1]) {
                nums[i] *= 2;
                nums[i+1] = 0;
            }
        }
        vector<int> ret;
        for (int i: nums) {
            if (i != 0) {
                ret.push_back(i);
            }
        }
        while (ret.size() < n) {
            ret.push_back(0);
        }
        return ret;
    }
};

Code 3:

class Solution {
public:
    vector<int> applyOperations(vector<int>& nums) {
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i]==nums[i-1])
            {
                nums[i] = 0;
                nums[i - 1] *= 2;
            }
        }
        int cnt=0;
        vector<int> ans;
        for(int x:nums)
        {
            if(x==0)
                cnt++;
            else
                ans.push_back(x);
        }
        while(cnt>0)
        {
            ans.push_back(0);
            cnt--;
        }
        return ans;
    }
};

Code in Go

Code 1:

func applyOperations(nums []int) []int {
    var i, j = 0, 0
    
    for ;i < len(nums) - 1; i++ {
        if nums[i] == nums[i+1] {
            nums[i] *= 2
            nums[i+1] = 0
        }
    }
    i = 0
    
    for ;j < len(nums); j++ {
        if nums[j] != 0 {
            nums[i] = nums[j]
            i += 1
        }
    }
    for i < len(nums) {
        nums[i] = 0
        i += 1
    }
    return nums
}

Output:

Input: nums = [1,2,2,1,1,0]
Output: [1,4,2,0,0,0]

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy, golang, golang program, Leetcode

Leave a Reply

Your email address will not be published.

Contact Us