Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Subtraction question. Yes, apparently I can't subtract Date: Thu, 26 Sep 2019 11:51:18 -0400 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: <9884f2a0-2655-4ca4-bec6-06501e96129c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 26 Sep 2019 15:51:21 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="2c8e13289615024399243acc7af5a53d"; logging-data="1707"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/7OkWvW0KIPCK5BiCH5/XS" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 Cancel-Lock: sha1:w2kIKZOsuf2cV+ix8SWYAXAnlvE= In-Reply-To: <9884f2a0-2655-4ca4-bec6-06501e96129c@googlegroups.com> Content-Language: en-US Xref: csiph.com comp.lang.java.programmer:39159 On 9/26/2019 9:54 AM, cdalten@gmail.com wrote: > 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; > } This should *almost* work, but not quite. Try has77(new int[] {7, 7}) and see if you can uncover the problem. (Aside: There's more than one way to skin a cat, and some of the other ways may prove less cumbersome. Let's dress up the scenario a little, and see if you can invent a different approach. So, imagine people walking up to you one by one, each announcing what day of the month he was born on. Because you're a big fan of astrology you believe being born on the seventh of Anywhen is important, and if you encounter two sevenses in a row or separated by only one non-seven you will jump for joy. But each person just walks up to you, announces, and walks away; you get no opportunity to check his birth day a second time. How might *you* decide when to jump? Can you get the computer to use a similar method?) > 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 [1] when it traverses the list? if so, then how can it do something like... > > if (nums[i] == 7 && nums[i+1] == 7) return true; The loop will run for each `i' value from 0 through 0 and no further; that much you've seen. But when `i' is 0, what is `i+1'? And what is `i+2'? (Extra credit: What is `i+3', if you were to compute it, and what trouble might `nums[i+3]' encounter?) -- esosman@comcast-dot-net.invalid Four hundred eighty-two days to go.