Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Move Zeroes

User image

Published by

sanya sanya

Published at: 29th Nov, 2023
2.86 mins read

Q8. Given an array A, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Intuition Behind the Algorithm

The core idea behind this algorithm is the efficient reorganization of the array elements. The goal is to shift all zeros to the end of the array while preserving the relative order of non-zero elements. This is achieved through a two-pointer approach where one pointer (j) is used to track the position for the next non-zero element, and the other (i) iterates through the array.

#include <bits/stdc++.h> using namespace std; // Function to move all zeros to the end of the array void moveZeroes(vector<int> &nums) { int n = nums.size(); // Size of the input array int j = 0; // Pointer to track the position for the next non-zero element // Iterate through the array for (int i = 0; i < n; i++) { // Check if the current element is non-zero if (nums[i] != 0) { // Swap the non-zero element with the element at position 'j' swap(nums[i], nums[j]); // Increment 'j' to update the position for the next non-zero element j++; } } } // Main function int main() { int n; // Variable to hold the size of the array cin >> n; // Input for the size of the array vector<int> nums(n); // Vector to store the elements of the array // Input the elements of the array for (int i = 0; i < n; i++) cin >> nums[i]; moveZeroes(nums); // Call the function to rearrange the array // Output the rearranged array for (int i = 0; i < n; i++) cout << nums[i] << " "; return 0; // End of the program }

Code Walkthrough

The function moveZeroes takes a vector of integers nums as its argument. It starts by initializing two variables: n, representing the size of the array, and j, a counter for the position of the next non-zero element.

The algorithm then iterates through the array using a for-loop. Inside the loop, it checks each element. If the current element (nums[i]) is non-zero, it is swapped with the element at the position indicated by j. This operation effectively places the non-zero element in its correct position while moving the zero towards the end. The counter j is then incremented.

In the main function, the array size (n) and the array elements are taken as input from the user. The moveZeroes function is called to perform the rearrangement, and the modified array is printed.

Time and Space Complexity Analysis

  • The time complexity of this algorithm is O(n), where n is the number of elements in the array. This is because the algorithm iterates through the array once, performing a constant number of operations for each element.

  • The space complexity is O(1), indicating that the algorithm uses a constant amount of extra space. The rearrangement is done in place without requiring additional storage proportional to the input size.

Key Points of the Algorithm

  • Two-Pointer Technique: This is crucial for efficiently sorting the elements in a single pass.
  • In-Place Rearrangement: The algorithm does not use extra space for another array, making it space-efficient.
  • Preservation of Order: Non-zero elements maintain their original order post rearrangement.

Conclusion

The presented algorithm efficiently solves the "Move Zeros" problem with optimal time and space complexity.

Library

WEB DEVELOPMENT

FAANG QUESTIONS

Finding Majority Element

String With Unique Characters

Container With Most Water

Array has a Circular Loop or Not??

Sunny Buildings

Distribute Candies

OverHappy Numbers

Move Zeroes

Count Zeroes

Target Zero

On this page

Intuition Behind the Algorithm

Code Walkthrough

Time and Space Complexity Analysis

Key Points of the Algorithm

Conclusion