# Move Zeroes - Leet Code Solution

September 03, 2020

## 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.

## Similar Posts

### Maximum Length of Subarray With Positive Product - Leet Code Solution

Problem Statement Maximum Length of Subarray With Positive Product. Given an…

### Integer to Roman conversion - Leet Code Solution

Problem Statement Roman numerals are represented by seven different symbols: I…

### List of Sorting Algorithms

This topic is one of the most common studied. When somebody started preparation…

### Rotate Array - Leet Code Solution

Problem Statement Given an array, rotate the array to the right by k steps…

### Index for Coding Problems

Sorting Problems Merge Sort Quick Sort Heap Sort Bubble Sort Selection Sort…

### Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…