Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    2460 - Apply Operations to an Array.

    Easy

    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 <code>i<sup>th</sup></code> operation ( 0-indexed ), you will apply the following on the <code>i<sup>th</sup></code> 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: nums0 and nums1 are not equal, so we skip this operation.

    • i = 1: nums1 and nums2 are equal, we multiply nums1 by 2 and change nums2 to 0. The array becomes 1, **<ins>4</ins>** , **<ins>0</ins>** ,1,1,0.

    • i = 2: nums2 and nums3 are not equal, so we skip this operation.

    • i = 3: nums3 and nums4 are equal, we multiply nums3 by 2 and change nums4 to 0. The array becomes 1,4,0, **<ins>2</ins>** , **<ins>0</ins>** ,0.

    • i = 4: nums4 and nums5 are equal, we multiply nums4 by 2 and change nums5 to 0. The array becomes 1,4,0,2, **<ins>0</ins>** , **<ins>0</ins>** .

    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.

    Constraints:

    • 2 &lt;= nums.length &lt;= 2000

    • 0 &lt;= nums[i] &lt;= 1000

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final IntArray applyOperations(IntArray nums)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait