### Leetcode - Maximum Non Negative Product in a Matrix

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

September 03, 2020

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]
```

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.

```
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;
}
}
```

Its `O(n)`

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.

```
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 ++;
}
}
}
```

Its `O(n)`

But, its better since we are not using another loop to copy zero.

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

Problem Statement You are given a string text of words that are placed among…

Problem Statement Given a string s, return the maximum number of unique…

Problem Statement Given an array of integers, return indices of the two numbers…

Its a kind of incremental insertion technique, where the algorithm build up…

Problem Statement Given a string, find the first non-repeating character in it…

Problem Statement You are given a string text of words that are placed among…

Problem Statement You are given a rows x cols matrix grid. Initially, you are…

Problem Statement Given a string s, return the maximum number of unique…

Problem The Leetcode file system keeps a log each time some user performs a…

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

Problem Implement an algorithm to determine if a string has all the characters…