We define a *skip sequence* as a regularly-spaced list of integers such as might be generated by MATLAB's colon operator. We will call the inter-element increment the *stride*. So the vector 2:3:17 or [2 5 8 11 14 17] is a six-element skip sequence with stride 3.

Given the vector a, your job is to find the stride associated with the longest skip sequence you can assemble using any of the elements of a in any order. You can assume that stride is positive and unique.

Example:

input a = [1 5 3 11 7 2 4 9] output stride is 2

since from the elements of a we can build the six-element sequence [1 3 5 7 9 11].

Show
1 older comment

@bmtran
on 13 Feb 2012

this problem is multi-valued. it is equally valid to say that, in the case of the example, the stride associated with the longest skip sequence is -2.

Ned Gulley
on 14 Feb 2012

Fair enough. I changed the wording to remove the ambiguity.

Ned Gulley
on 13 Feb 2014

I added another test and rescored. It appears that DIST no longer works. The DIST function is from the Neural Networks Toolbox. It shouldn't have worked in the past, but since it did, it played a part in a lot of answers. Sorry for the discontinuity.

Jean-Marie Sainthillier
on 28 Jan 2015

Tough !
Two years before understand that the sequence must contain only elements of a :)

1 Comment

Alfonso Nieto-Castanon
on 25 Oct 2016

this new implicit expansion functionality is a great excuse to revisit old problems :)

1 player likes this solution

4 Comments

Show
1 older comment

@bmtran
on 14 Feb 2012

what does the dist function do?

Alfonso Nieto-Castanon
on 14 Feb 2012

'dist' just computes the euclidean distance between two lists of vectors (the dimensionality size(dist(a,b)) is the same as size(a*b)). In this case ~dist(a',a+s) is just the same as bsxfun(@eq,a',a+s), the matrix defining a directional graph of the elements of 'a' with distance 's'.

Alfonso Nieto-Castanon
on 14 Feb 2012

just for completion: the matrix above is nilpotent (with degree lower than the length of the vector a), if you sum all of its powers D=sum_n{C^n} (and this is what polyvalm does) you get a matrix where d(i,j)=1 if i is connected to j in this graph; it you then take the largets column sum of the result (this is what norm(D,1) does) you get the maximum length of a sequence with stride s (across all possible starting nodes in a)...

David Young
on 18 Feb 2012

I am impressed.

1 Comment

@bmtran
on 14 Feb 2012

i have no idea what dist or dmperm are/do...

2 Comments

Alfonso Nieto-Castanon
on 14 Feb 2012

wow, very nice double arrayfun

@bmtran
on 14 Feb 2012

they were once for loops

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
3 players like this problem

3 players like this problem