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:
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.
[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].
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 <= 20000 <= nums[i] <= 1000program main
    implicit none
    integer, parameter :: n = 6
    integer, dimension(n) :: nums
    integer :: i
    ! Example 1
    nums = [1, 2, 2, 1, 1, 0]
    call solve(nums)
    write (*, '(A)') 'Example 1:'
    write (*, '(*(G0))') nums
    ! Example 2
    nums = [0, 1]
    call solve(nums)
    write (*, '(A)') 'Example 2:'
    write (*, '(*(G0))') nums
contains
subroutine solve(nums)
    implicit none
    integer, dimension(:), intent(inout) :: nums
    integer :: i
    do i = 1, size(nums) - 1
        if (nums(i) == nums(i + 1)) then
            nums(i) = nums(i) * 2
            nums(i + 1) = 0
        end if
    end do
    nums = pack(nums, nums /= 0)
end subroutine solve
end program maintemp.f95:15:4:
   15 |     nums = [0, 1]
      |    1
Error: Different shape for array assignment at (1) on dimension 1 (6 and 2)
          ! This is a full Fortran script with a valid main entry point that solves the problem.
! The program should run with all provided examples and output to stdout.
! 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].
! 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
! This is a full Fortran script with a valid main entry point that solves the problem.
! The program should run with all provided examples and output to stdout.
! 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
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start': (.text+0x1b): undefined reference to `main' collect2: error: ld returned 1 exit status
def performOps(nums):
    n = len(nums)
    for i in range(n - 1):
        if nums[i] == nums[i + 1]:
            nums[i] *= 2
            nums[i + 1] = 0
    nonZeroIndex = 0
    for i in range(n):
        if nums[i] != 0:
            nums[nonZeroIndex] = nums[i]
            nonZeroIndex += 1
    while(nonZeroIndex < n):
        nums[nonZeroIndex] = 0
        nonZeroIndex += 1
    return nums
Iterate through the nums array from index 0 to n - 2.
For each index i, check if nums[i] equals nums[i + 1]. If equal, multiply nums[i] by 2 and set nums[i + 1] to 0.
After the operations, shift all 0's to the end of the array. To do this, keep track of a nonZeroIndex and copy only non-zero elements to that index. Increment the nonZeroIndex for every non-zero element copied. Finally, fill the rest of the array with 0's starting from the nonZeroIndex.
#include <vector>
using namespace std;
vector<int> performOps(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;
        }
    }
    int nonZeroIndex = 0;
    for (int i = 0; i < n; ++i) {
        if (nums[i] != 0) {
            nums[nonZeroIndex++] = nums[i];
        }
    }
    while (nonZeroIndex < n) {
        nums[nonZeroIndex++] = 0;
    }
    return nums;
}
Iterate through the nums array from index 0 to n - 2.
For each index i, check if nums[i] equals nums[i + 1]. If equal, multiply nums[i] by 2 and set nums[i + 1] to 0.
After the operations, shift all 0's to the end of the array. To do this, keep track of a nonZeroIndex and copy only non-zero elements to that index. Increment the nonZeroIndex for every non-zero element copied. Finally, fill the rest of the array with 0's starting from the nonZeroIndex.