Exercism problem. How can I do this?


I have this challenge :

Your task is to find the longest contiguous subslice of a slice of integers for which each member is 1 higher than its predecessor. For example, given the slice [1, 2, 4, 4, 5, 6, 7, 3, 2, 7, 8, 9, 1], the longest subslice where each member is 1 higher than its predecessor is [4, 5, 6, 7], from indices 3 through 6 inclusive.

We provide a function stub:

pub fn longest_incrementing_subslice(s: &[u8]) -> &[u8]

Note the signature: you should return a subsection of the input slice, not a newly-allocated vector.

It is safe to assume that every input contains exactly one longest subslice.

anyone who can help me figure out how I can do that. Please no code only a sort of plan

The usual approach to this kind of problem is to keep track of 3 numbers as you scan through the input (in addition to your current position):

  • The start index of the best result you’ve seen so far,
  • The end index of the best result you’ve seen so far, and
  • The start index of the current run.

If you keep these correct in every iteration of the loop, then the first two will represent the final result once you’re done scanning. You can then use them to make the subslice to return.

So make three variables. one holding the start index of the best result, the end index and the index of the iterator ?


You need startOfCurrent so that if you do find one that's better than the best you'll need something to update startOfBest with. whether you need the iterator's index will depend on how you iterate, there's many ways to do so.

oke,like that

Time to experiment with it

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.