Can someone tell me the time complexity of this code?

Joined
Mar 9, 2023
Messages
3
Reaction score
0
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++;   
            }
        }
    }
}
 
Joined
Sep 4, 2022
Messages
128
Reaction score
16
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:
Joined
Sep 21, 2022
Messages
121
Reaction score
15
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.
 
Joined
Dec 18, 2023
Messages
3
Reaction score
0
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
 
Joined
Dec 18, 2023
Messages
3
Reaction score
0
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
 
Joined
Nov 23, 2023
Messages
50
Reaction score
2
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

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.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top