# Can someone tell me the time complexity of this code?

#### sscrock1234

Java:
``````class Solution {
public void moveZeroes(int[] nums) {
int left=0;
int right=0;

for(int i=0;i<nums.length;i++){
if(nums[i]==0){
left=i;
right=i+1;
break;
}
}

while(right<nums.length){
if(nums[right]!=0 && nums[left]==0){
int temp=nums[right];
nums[right]=nums[left];
nums[left]=temp;
left++;
right++;
}
else{
right++;
}
}
}
}``````

#### FResher

Java:
``````public void moveZeroes(int[] nums) {

int temp[]; // use a temp array
int i = 0; // iterator index
int j=0; // iterator index for temp array

while(i<nums.length){ // fetching the Array with values

if(nums[i] == 0){ // if the value is 0 : skip the indexes by incrementing
i++;
continue; // jump to the next lap of the loop, without executing the lines after
}

temp[j] = nums[i]; // allocating the value in nums at index i.
i++; // incrementing i for the next value
j++;
}

nums[] = temp[] ; // old array is replace by new array without 0.

}``````

It's a solution too, maybe the syntax and statement are to be fix.

Last edited:

#### WhiteCube

Java:
``````class Solution {
public void moveZeroes(int[] nums) {
int left=0;
int right=0;

for(int i=0;i<nums.length;i++){
if(nums[i]==0){
left=i;
right=i+1;
break;
}
}

while(right<nums.length){
if(nums[right]!=0 && nums[left]==0){
int temp=nums[right];
nums[right]=nums[left];
nums[left]=temp;
left++;
right++;
}
else{
right++;
}
}
}
}``````
The number of iterations is equal to the length of the array, so that would be linear time. Double the array, double the time, triple the array, triple the time etc.
I don't know the official "big O" notation for this case, I do not study computer science.

#### raymbs

Java:
``````class Solution {
public void moveZeroes(int[] nums) {
int left=0;
int right=0;

for(int i=0;i<nums.length;i++){
if(nums[i]==0){
left=i;
right=i+1;
break;
}
}

while(right<nums.length){
if(nums[right]!=0 && nums[left]==0){
int temp=nums[right];
nums[right]=nums[left];
nums[left]=temp;
left++;
right++;
}
else{
right++;
}
}
}
}``````
O(n) linear

#### raymbs

The number of iterations is equal to the length of the array, so that would be linear time. Double the array, double the time, triple the array, triple the time etc.
I don't know the official "big O" notation for this case, I do not study computer science.i

#### raymbs

it's Big O notation will be O(n) linear as you mentioned

#### alexcray

Java:
``````class Solution {
public void moveZeroes(int[] nums) {
int left=0;
int right=0;

for(int i=0;i<nums.length;i++){
if(nums[i]==0){
left=i;
right=i+1;
break;
}
}

while(right<nums.length){
if(nums[right]!=0 && nums[left]==0){
int temp=nums[right];
nums[right]=nums[left];
nums[left]=temp;
left++;
right++;
}
else{
right++;
}
}
}
}``````
If you don't have a vector version of the logo, use a raster format like PNG. PNGs support transparency, which can be useful for logos, and they generally compress well for web use. Avoid using JPEGs for logos as they compress by sacrificing image quality, leading to blurriness.

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.