289. Move Zeroes and Count Minimum Writes
Move Zeroes and Count Minimum Writes
Given a list of integers, move every 0 to the end while preserving the relative order of all non-zero values.
Implement two separate methods:
  • One method returns a new list with all zeroes moved to the end.
  • Another method returns the minimum number of write operations required to transform the original list into that reordered list.

Write Operation

A write operation occurs when the value stored at an index is changed to a different value.
  • Changing the value at an index from one value to another counts as one write.
  • Assigning an index the value it already contains does not count as a write.
  • The minimum write count is the number of indices whose values differ between the original list and the correctly reordered list.

Method Signatures

Move Zeroes

List<Integer> moveZeroes(List<Integer> nums)

Count Minimum Writes

int countMinimumWrites(List<Integer> nums)

Move Zeroes

Parameters

  • nums: The list whose zero values must be moved to the end.

Return Value

Return a new list containing all non-zero values in their original relative order, followed by all zero values.
The input list nums must not be modified.

Count Minimum Writes

Parameters

  • nums: The original list for which the minimum write count must be calculated.

Return Value

Return the minimum number of indices whose values would need to change to transform nums into the list returned by moveZeroes(nums).
This method must only calculate the write count and must not modify nums.

Requirements

  • All non-zero values must remain in their original relative order.
  • All zero values must appear after all non-zero values.
  • moveZeroes must return a new list and must not modify the input list.
  • countMinimumWrites must return the minimum number of required writes and must not modify the input list.
  • An index counts as a write only when its original value differs from the value at the same index in the reordered list.
  • Calling either method must not affect the result of subsequently calling the other method with the same input list.

Constraints

  • 1 ≤ nums.size() ≤ 10,000
  • 0 ≤ i < nums.size()
  • -2,147,483,648 ≤ nums.get(i) ≤ 2,147,483,647

Examples

Example 1

nums = [4, 0, -2, 0, 7, 0, 5]
result = moveZeroes(nums)
writes = countMinimumWrites(nums)
result = [4, -2, 7, 5, 0, 0, 0] and writes = 5.
The values at indices 1, 2, 3, 4, and 6 differ between the original and reordered lists.
The original list remains nums = [4, 0, -2, 0, 7, 0, 5].

Example 2

nums = [0, 0, 9, 0]
result = moveZeroes(nums)
writes = countMinimumWrites(nums)
result = [9, 0, 0, 0] and writes = 2.
Only the values at indices 0 and 2 differ between the original and reordered lists.
The original list remains nums = [0, 0, 9, 0].

Example 3

nums = [8, -3, 6]
result = moveZeroes(nums)
writes = countMinimumWrites(nums)
result = [8, -3, 6] and writes = 0.
The list is already in the required order, so no index value needs to change.

Example 4

nums = [0, 5, 0, 5]
result = moveZeroes(nums)
writes = countMinimumWrites(nums)
result = [5, 5, 0, 0] and writes = 2.
Although values are moved conceptually, only indices 0 and 3 contain different values in the reordered list. Indices 1 and 2 already contain their required values.


Please use Laptop/Desktop or any other large screen to add/edit code.