 experchange > java Subtraction question. Yes, apparently I can't subtract

 cdalten (09-26-19, 03:54 PM)
Given the following

"Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."

I came up with the following...

public boolean has77(int[] nums) {
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] == 7 && nums[i+1] == 7) return true; //
if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
if (nums[i] == 7 && nums[i+2] == 7) return true; //
}

return false;
}

The question revolves around the following case...

has77([1, 7, 7])

How does the whatever know to check [1,7,7] when 2 is subtracted from the length?

Wouldn't the loop or the computer just see something like  when it traverses the list? if so, then how can it do something like...

if (nums[i] == 7 && nums[i+1] == 7) return true;
On Thursday, September 26, 2019 at 6:54:59 AM UTC-7, chad altenburg wrote:
[..]
> How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
> Wouldn't the loop or the computer just see something like  when it traverses the list? if so, then how can it do something like...
> if (nums[i] == 7 && nums[i+1] == 7) return true;

Actually, more to the point, how can it do something like...

if (nums[i+1] == 7 && nums[i+2] == 7) return true;

Because when I comment out this line, it returns false (which is incorrect).
On Thursday, September 26, 2019 at 6:59:33 AM UTC-7, chad altenburg wrote:
> On Thursday, September 26, 2019 at 6:54:59 AM UTC-7, chad altenburg wrote:
> Actually, more to the point, how can it do something like...
> if (nums[i+1] == 7 && nums[i+2] == 7) return true;
> Because when I comment out this line, it returns false (which is incorrect).

Wait, this is cool. I found that I could google subtraction!

 Eric Douglas (09-26-19, 04:57 PM)
On Thursday, September 26, 2019 at 9:54:59 AM UTC-4, chad altenburg wrote:
[..]
> How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
> Wouldn't the loop or the computer just see something like  when it traverses the list? if so, then how can it do something like...
> if (nums[i] == 7 && nums[i+1] == 7) return true;

1. I'm not sure what this has to do with 'subtraction'.