coding-interview|September 03, 2020|1 min read

Move Zeroes - Leet Code Solution

TL;DR

Use a write pointer — copy every non-zero element to the write position, then fill the rest with zeros. Two-pointer approach, O(n) time, O(1) space, in-place.

Move Zeroes - Leet Code Solution

Problem Statement

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

Example

Input: [0,1,0,3,12]
Output: [1,3,12,0,0]

Solution (copying to left)

We can keep a index variable which will keep a tab on all non-zero values.

On iteration, we can move each non-zero value to left side.

Code

public void moveZeroes_simple(int[] nums) {
   int left=0;
   
   for (int i=0; i<nums.length; i++) {
      if (nums[i] != 0) {
         nums[left] = nums[i];
         left ++;
      }
   }
   
   //copy zeroes to remaining array
   for (int i=left; i<nums.length; i++) {
      nums[i] = 0;
   }
}

Complexity

Its O(n)

Another Solution (Swapping)

We can do a slight modification to above solution. The point where we just move non-zero value to left. We can do a swap as well.

Code

public void moveZeroes(int[] nums) {
   int left=0;
   
   for (int i=0; i<nums.length; i++) {
      if (nums[i] != 0) {
         //swap
         int t = nums[i];
         nums[i] = nums[left];
         nums[left] = t;
         
         left ++;
      }
   }
}

Complexity

Its O(n)
But, its better since we are not using another loop to copy zero.

Related Posts

Replace all spaces in a string with %20

Replace all spaces in a string with %20

Problem Statement Replace all spaces in a string with ‘%20’ (three characters…

Valid Palindrome - Leet Code Solution

Valid Palindrome - Leet Code Solution

Problem Statement Given a string, determine if it is a palindrome, considering…

Valid Anagrams - Leet Code Solution

Valid Anagrams - Leet Code Solution

Problem Statement Given two strings s and t , write a function to determine if t…

Rotate Image - Leet Code Solution

Rotate Image - Leet Code Solution

Problem Statement You are given an n x n 2D matrix representing an image, rotate…

Validate Sudoku - Leet Code Solution

Validate Sudoku - Leet Code Solution

Problem Statement Determine if a 9x9 Sudoku board is valid. Only the filled…

Plus One - Leet Code Solution

Plus One - Leet Code Solution

Problem Statement Given a non-empty array of digits representing a non-negative…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…