Hai Liu
Hai Liu

Reputation: 43

Can someone explain the difference between these two loops?

   while(n>= 0 && nums[n] == value){  
        n--;
    }

   while(n>=0 && nums[n--] == value){
   }

I think these two loops should works exactly the same. What happens is that I run the test case input[4,5] value=4. and get an array index out of boundary. But if I can to another loop, it works.

public int removeElement(int[] nums, int val) {
    if(nums.length == 0) return 0;
    // use two pointers, n for the right most of the item, which value != val. 
    // i for the start pointer. 
    int n = nums.length-1;
    int i = 0;
    while(n>= 0 && nums[n--] == val){ 
    }

    while(i <= n){
        // as long as the curr items is equal to the valu
        // replace it by the right most of the value, decrement the n
        // pointer, until find another item which is not equal to val. 
        if(nums[i] == val){
            nums[i] = nums[n]; //
            while(n >= 0 && nums[--n] == val){
            }
        }
        i++;   
    }
    return n+1;
}

Upvotes: 1

Views: 357

Answers (3)

logee
logee

Reputation: 5067

They are not the same.

In the first loop, n gets decremented only if n >= 0 and nums[n] = value.

public void loop1() {
    int n = 0;
    int[] nums = new int[]{1};
    int value = 2;

    // n >= 0 is true
    // nums[n] == value is false
    // Therefore while condition is false and n is NOT decremented
    while(n>= 0 && nums[n] == value){  
        n--;
    }
    // n = 0
    System.out.println("n=" + n);
}

In the second loop, n gets decremented if n >= 0 regardless of whether nums[n] == value is true or false.

public void loop2() {
    int n = 0;
    int[] nums = new int[]{1};
    int value = 2;

    // n >= 0 is true
    // nums[n--] == value executes in 2 steps:
    //  1.  nums[n] == value
    //  2.  n = n - 1 WILL EXECUTE regardless of result being true/false
    while(n>= 0 && nums[n--] == value){  
    }
    // n = -1
    System.out.println("n=" + n);
}

Upvotes: 8

nickle_nine
nickle_nine

Reputation: 71

while(n>= 0 && nums[n] == value){  
    n--;
}

That says, while n is greater than or equal to zero AND the value stored in the nums array at index n equals the variable named value, decrease n by one. If n equals 0 it will break the loop at the next iteration when n gets decreased to -1 because of the first condition where n >= 0.

while(n>=0 && nums[n--] == value){
}

This is saying while n is greater than or equal to zero AND the value stored in the nums array at index minus one, keep looping while decreasing the value of n by one. This problem here is that when n equals 0, the first condition will be true but the second condition will fail. That is, nums[-1] will throw the index out of bounds exception.

Upvotes: 0

gihansalith
gihansalith

Reputation: 1920

this one decrease value of n by one before execute inside statements of the loop

 while(n>=0 && nums[n--] == value){

}

this one decrease value of n by one at inside of the loop(it can be last statement or first inside statement of the loop)

while(n>= 0 && nums[n] == value){  
    n--;
}

Upvotes: 0

Related Questions