• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I retrieve the index of a specific element in an array?  RSS feed

 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm trying to iterate through an array of integers and find the start position of the part of the array containing the most consecutive 1s.

For example given the array {1,3,1,1,1,1,5,3,2,1,1}, the method should return 2 and given {1,4,5,1,1,1,5,1} the method should return 3.

So far, I've managed to retrieve the element where the consecutive 1s begin. However, I'm unsure how to get the index of this element and this implementation doesn't work if there is more than one set of consecutive 1s.
I hope my question is clear enough and any tips would be greatly appreciated. Thanks.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your algorithm is not doing the right thing. Have you tired to do this manually? If not, try it with pen and paper first so you can lay out the procedure.

Variables help you remember things as you go along. You need to remember where you started counting 1s as you iterate through the array. You also need to remember what the longest series of ones is that you have found so far. Good variable names are critical in writing good programs. Good variable names help you think through a problem properly. Poorly chosen names can confuse you. See the Stroop Effect.

In your code, index and mostOnes are pretty good names. However, tracker is not so good. count might be a better name. You probably should have a boolean variable, called trackingOnes perhaps, that remembers whether or not you're counting 1s at a given moment. Then you'll need to do something when that state changes, that is, when you change from counting ones to not counting ones and vice versa.

Then you have to make sure that you use these variables in the way their names imply. For example, the index variable implies that it remembers the start index of the longest series of ones you have found so far, which is represented by the variable mostOnes. However, in your code, line 16 doesn't do what you'd think it would do. Also, you never assign a value to the variable mostOnes.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's your code which returns the array entry which starts the run of 1's. The entry, of course, is always 1 so you're right to consider this value useless.



you should be able to look at that code and find where you are using an array index to return that value, I think. But now that I look at it, your choice of variable names makes it significantly harder to do that. We both know that "index" isn't what you are returning, so let's call it something else:



Do you see the index now?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a variable you haven't used yet: maxOnes
and if your variable 'index' isn't storing an index, then what IS it storing? Either rename it, or change what gets stored in it so that the variable name reflects its actual purpose.

I would suggestion you also maintain a startOfMaxSequence variable, or similar.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:We both know that "index" isn't what you are returning, so let's call it something else:



If I understood OP correctly, he is wanting to return the index, which represents where the longest series of 1s in the array starts. It's the right hand side that's not correct.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul is making the same point you are.
The OP has clearly identified that the variable "index" isn't actually holding the value he wants.
However the index IS present in that line of code. Removing the work 'index' might help it stand out a little more...
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but the whole algorithm isn't even close to being correct. If we're just dealing with issues like being able to recognize the use of an array index in Java code, let's clear that up before getting into more complicated things.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
I would suggestion you also maintain a startOfMaxSequence variable, or similar.

index and startOfMaxSequence have the same purpose. If you really want to be specific, I think startOfMostOnes would be more symmetrical with the other variable name OP already has.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And my point was that I'm not so sure OP realized the he was actually assigning the value in the array rather than the index. The variable name index wasn't terribly misleading and actually conveyed OP's intent quite clearly; he just didn't have the correct expression on the right hand side of the assignment statement.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Stefan Evans wrote:
I would suggestion you also maintain a startOfMaxSequence variable, or similar.

index and startOfMaxSequence have the same purpose.


I disagree.
You need two variables to track things in the algorithm I have in mind.
Perhaps renaming 'index" to "startOfCurrentSequence" would make my point better.

See how even a reasonably named variable like 'index' can be confusing when you have to track more than one?
 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies & I do agree that the statement on the right hand side of index made the code confusing. I am trying to get the index but I forgot to rename it before posting. Junilu's answer helped make the problem more clearer to me and I will of course name variables with more care in future. I'll keep you posted on my progress.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do it this way. Have an int variable idx for the index of the start of the biggest run of 1s and an int variable runlen for the length of that run and initialize them both to some negative number. Next start an i at 0 and cruise through the array until you find a 1. Store that i temporarily in tmpidx. Start incrementing i through the array and counting ones. When you reach the end of the run of ones see if this length is a bigger number than the last saved runlen and if so overwrite the latest idx and runlen. Proceed thusly and forthwith until you reach the end of the array.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:I would do it this way. Have an int variable idx for the index of the start of the biggest run of 1s and an int variable runlen for the length of that run and initialize them both to some negative number. Next start an i at 0 and cruise through the array until you find a 1. Store that i temporarily in tmpidx. Start incrementing i through the array and counting ones. When you reach the end of the run of ones see if this length is a bigger number than the last saved runlen and if so overwrite the latest idx and runlen. Proceed thusly and forthwith until you reach the end of the array.

The algorithm sounds reasonable although your choice of variable names could be better. I recommend using variable names that you can actually pronounce. mostOnes is a much better name than runlen and idx is just, frankly, a poor alternative to the much more readable index. As for initializing to negative one, that is a good idea but you only need to do it for the index. It makes sense and is in line with the behavior of the String.indexOf() method. If you haven't found any 1s yet, it makes sense that the maximum length of 1s you have found is zero.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
I disagree.
You need two variables to track things in the algorithm I have in mind.
Perhaps renaming 'index" to "startOfCurrentSequence" would make my point better.

Yes, I agree that startOfCurrentSequence is going to be needed.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
runlen tells you what it is - a run length. mostOnes could be anything. idx is a particular kind of index, like i. unlike "index"

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guillermo Ishi wrote:runlen tells you what it is - a run length. mostOnes could be anything. idx is a particular kind of index, like i. unlike "index"

We'll just have to disagree then. At least the OP and I understand that mostOnes specifically means "the most number of 1s found." I personally don't understand the need to abbreviate words -- program code is for people reading it, the computer could care less.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Guillermo's algorithm and it actually worked out well. I first tried using a for-loop but the code just got uglier and uglier as I added more test cases. I was able to keep the code relatively clean by having a high-level method with a while-loop that called two helper methods, each having a while-loop as well. One helper method to find the start of the next series of 1s, and other helper method to count the length of a series of 1s given its starting index. By using some calculations in the right spots, I was able to get by with just 4 variables in the top-level method. Here's how they were initialized:

After trying this myself, I think this one could be a bit challenging for beginners.

Here's my JUnit test, in case the OP or anyone following this thread is interested:
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gentlemen,
Jake Thomas wrote:(...) I'll keep you posted on my progress.

Why not wait for OP to see what he comes up with?

(And why not let a regex and matcher do all the hard work?)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see nothing wrong with discussing different approaches while we wait Everyone will have their own preferences and opinions and that's fine.
 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I ended up doing the following:



This seems to do the job I wanted and it was actually a lot simpler that I thought it would be. Thanks for all advice and suggestions .
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Thomas wrote:This seems to do the job I wanted and it was actually a lot simpler that I thought it would be.

Sorry to break it to you, Jake, but it's not quite as simple as you think. I hooked up your solution to my test harness (see my previous reply) and it fails the following tests:

should_return_negative_1_when_empty_array
should_return_negative_1_if_no_1s
should_find_first_long_series_multiple_same_length
should_find_longest_at_beginning_with_multiple_series
should_find_longest_at_end_with_multiple_series

This is what JUnit reported:

should_return_negative_1_when_empty_array(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <-1>
but: was <0>

should_find_first_long_series_multiple_same_length(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <1>
but: was <4>

should_find_longest_at_beginning_with_multiple_series(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <0>
but: was <4>

should_return_negative_1_if_no_1s(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <-1>
but: was <0>

should_find_longest_at_end_with_multiple_series(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <7>
but: was <4>


I added your test case to my test harness and it does indeed pass that case but your case is similar to my should_find_longest_in_middle_with_multiple_series test case.

Based on the above test failures, your code does not handle the following scenarios correctly:
- array is empty, with 0 elements [returns 0, which is incorrect because there is no 0th element]
- array has no 1s [returns 0, which is incorrect because the first element is not a 1]
- array has multiple series of 1s with the same length [returns the position of the second series, even when it's not the longest]
- array has its longest series of 1s at the front of the array [returns the second series found instead, which isn't the longest]
- array has its longest series of 1s at the back of the array [returns the second series found instead, which isn't the longest]

I modified the should_find_first_long_series_multiple_same_length as follows:

to include a shorter series of 1s along with the 3 other series that are the same length. When I comment out line 3 in the test above, this is the AssertionError message:

should_find_first_long_series_multiple_same_length(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <1>
but: was <5>

A correct solution should return the position of the first of the long series but your solution returns the position of the second series of 1s, even though it isn't the longest.

I suggest you look at the data passed in the test cases that failed and try them out yourself. Then go back and look at Guillermo's suggested algorithm again. There are other hints in my previous reply as well.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Thomas wrote:
I'm trying to iterate through an array of integers and find the start position of the part of the array containing the most consecutive 1s.

For example given the array {1,3,1,1,1,1,5,3,2,1,1}, the method should return 2 and given {1,4,5,1,1,1,5,1} the method should return 3.

I tried these out too, and your code does work correctly with these inputs but only because they are testing basically the same scenario where the longest series just happens to be the second series of 1s in the array.
If you move the longest series to the front or back of the array, like so:

your code fails with the following AssertionErrors:

jake_first(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <0>
but: was <5>

jake_second(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <5>
but: was <3>

This is consistent with the other failures I already mentioned. This shows you the importance of testing with different scenarios and testing for different "corner or edge cases" - it's not enough to just use a variety of data to test, you also have to think about how the variations are actually different from each other. Your variations are all the same test scenario: the longest series is in the middle of the array and, in particular, it just happens to be the second among other series of 1s in your test data. Your code fails this test which has the longest series as the third of multiple series of 1s:

java.lang.AssertionError:
Expected: is <6>
but: was <3>
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more hint before I go back to bed (Been dealing with an upset stomach, which is why I'm up at this ungodly hour)

Your code is almost correct. I had to add one variable declaration (what happened to the variable mostOnes that you had earlier?), add another line of code and change two lines to get your solution to pass all the tests I have. Look at lines 13 - 15 of your solution and understand what that's really doing.

I actually kind of like how your way turns out vs what I came up with.

Good luck.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With these sort of algorithams youneed to break your code down, into three segments

The first time you find a number


what to do id its a consecutive

what to do if its not

similar to this, i am sure you can refactor this but on this template

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Evans wrote:With these sort of algorithams youneed to break your code down, into three segments
...
similar to this, i am sure you can refactor this but on this template

Sorry, no cigar for you either, Tony. Your solution fails to find the longest series when it's at the end of the array. Looks like you also fell victim to a missed edge case.

Here are the failures when I hook your code into my test harness:

should_find_first_long_series_multiple_same_length(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <0>
but: was <7>

should_find_single_1_at_end(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <4>
but: was <-1>

should_find_single_long_series_at_end(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <2>
but: was <-1>

should_find_longest_at_end_with_multiple_series(com.coderanch.examples.beginning.MostOnesTest)
java.lang.AssertionError:
Expected: is <7>
but: was <0>

It's quite interesting to see the different solutions to this seemingly simple problem. The OP's most recent solution is actually very close to being correct, he just needs to change a couple of things and it'll be good. The working version of OP's code is also cleaner than the template that Tony offers up for use, IMO.

This is why I love practicing with white belts in the dojo in real life and working with developers who have less experience that I do. You never know what kind of things they can teach you as a seasoned practitioner. I hesitate to say "ignorance" but their lack of bias about what's "right" sometimes enables them to do some pretty nice things. When that happens, you just say to yourself, "Huh, well I'll be a monkey's uncle..."
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at Tony's solution, it's actually very similar to what I had come up with based on my initial recommendations. My first attempt also failed the same edge case where the longest series was at the end of the array. In trying to fix it, it got really ugly so I abandoned it, opting to try out Guillermo's suggested algorithm instead. That got me a correct solution with cleaner code but looking at the OP's fixed solution, I have to admit that there's a certain elegance to how he just ignores optimization and stays in the "here and now". Out of curiosity, I iterated one of my test cases 15000 times and interestingly enough, the OPs "inefficient" solution actually appears to run consistently faster than my seemingly "cleaner" solution. Another reminder of how premature optimization is the root of all evil.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Why not wait for OP to see what he comes up with?

(And why not let a regex and matcher do all the hard work?)

Piet, now that the wait for OP is kind of over, what does your regex/matcher-based solution look like? Is it cleaner than the OP's (fixed) solution? OP's latest code doesn't work as posted but it does with a few minor tweaks and it's even surprisingly elegant in a way, IMO. I've only given your approach a bit of thought but the more I mull it over, the messier it gets in my head.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Junilu,

I hope you're feeling better.

Well, the use of a regex with a matcher is nothing special. If you look at the Orcle regex tutorial,
(http://docs.oracle.com/javase/tutorial/essential/regex/)
you will see examples that are directly applicable.

True, it only works for things that are one character long (like digits and letters),
but apart from that it really is nice.

So, here's the code, mostly taken from said Oracle tutorial:
(of course, it only works if the array is first turned into a string)


And no, it is certainly not better that what OP, you and the others produced.
It's just a bit of fun.

And I leavie it to Campbell to turn it into a decent java 8 version!

Greetz,
Piet
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Junilu,

I hope you're feeling better.
...

Thanks, I'm getting there. Fortunately, I work from home so I can still get some work done without offending my co-workers


And no, it is certainly not better that what OP, you and the others produced.
It's just a bit of fun.

Yeah, the approach has its limitations but it's an interesting solution. I haven't tried to hook it up to the test harness to see how it holds up but thanks for sharing.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because I could, here is an alternative solution.
I won't say it is better than Jake's (which I found quite elegant) but it does show a slightly different way of structuring it.

@Junilu: Nice batch of unit tests

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:@Junilu: Nice batch of unit tests

Thanks

It's probably not surprising that we came with almost the exact same code. I refactored mine a bit more though, extracting two methods from the main body of code.

I could have saved one iteration through the outer while-loop by making line 11 check for going past the end of the array but I opted for the simpler conditional expression after I saw that all the tests still passed without the extra test. I still can't help but smile when I look at the fixed up version of Jake's code and kind of wonder at the elegance that comes with ignoring optimizations. Gotta love the simplicity of it.
 
Jake Thomas
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

So I carried on trying to work on the problem. I ended up with this. This works for most cases that I could think of. Thanks for the help.


 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing that you'll are posting different implementations to this problem I decided to join the playpen... But I am going to play differently by using another strategy...
Viewing the problem from a recurring pattern perspective I came up with the following:

Probably is going to run slower than what you'll have placed in the bucket... but I'm sure it can be improved using multi-threading...

And before Junilu Lacar tries to hit me with the ball the following are the tests:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:
I decided to join the playpen...
And before Junilu Lacar tries to hit me with the ball the following are the tests:

By all means, join in the fun, the more the merrier. It's not dodgeball but I will hit you with some code review comments if that's what you're looking for
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:By all means, join in the fun, the more the merrier. It's not dodgeball but I will hit you with some code review comments if that's what you're looking for

Hit me with your best shot cowboy!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Hit me with your best shot cowboy!

Ok, I'll make this quick because I have to get ready for a road trip tomorrow...

1. You are using @Test annotation so you really don't need to start those method names with "test" - it's redundant. The @Test annotation was introduced to eliminate the need to start method names with "test".

2. Too many parameters. That boolean parameter name, decision, could be better. The JavaDoc for that parameter isn't clear whether true=index/false=frequency or vice versa. I don't really think a boolean is appropriate for this. I would actually prefer two different methods if they were really necessary. Or have the method return a representation of a Tuple.

3. Why a static method?

Have to go now. Will check back this weekend.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jake Thomas wrote:
So I carried on trying to work on the problem. I ended up with this. This works for most cases that I could think of. Thanks for the help.


Jake, this version actually fails more of the tests that I ran it through before. You added a bunch of code that really isn't necessary.

Go back to my comments about your previous version. Go back to the version you had before -- it was very close to being correct. This latest version you posted is not as close and actually is a regression.

Hints:
In this latest version, Lines 13, 14, and 15 are correct; you fixed the bug in the previous version. However, you also introduced a regression: you broke something that was working before. Look at Line 8 of this latest version and compare it to the same line in you previous version. This latest version is incorrect.

And finally, if you haven't found anything yet, why do you initialize index to 0? That would mean you found the longest series of 1s at the very first position of the array, right? What value should you use to indicate that you haven't found anything?

The correct logic should work like this with the edge case where there's no data: Assume you don't find anything. Now try to search for that thing. Since there's no data to search through, then the initial assumption is correct and that's what you report.

This logic should also work when there is data but there are no matches: Assume you don't find anything. Now search for that thing. If you go through all the data available and still don't find anything, then the initial assumption is confirmed and that's what you report.

EDIT: If you just fix the bug on Line 8 of this latest version, it will pass all tests. But like I said, you added a bunch of code that isn't necessary. The while-loop is not necessary. The additional if statement after the while loop is not necessary. The return -1 is not necessary.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you may prefer the following:



But I opt to keeping the functionality static since the class doesn't contain any state information...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!