Problem 672. Longest run of consecutive numbers
Solution Stats
Problem Comments

29 Comments
I don't understand why for test suite 4 :
a=[0 1 1 1 0 2 2 0 1 1 1 0];
y_correct = [1 1];
I expect y_correct = [1 1 1]
The idea seems to be to return the element that occurs in the longest run, or all such elements in case of a tie. In case 4 there are two longest runs, both with element 1.
Thanks Tim for the explanation . So as the vector [1 1 1] appears twice in test 4 with the unique value 1 , the result must be twice the unqiue value > [ 1 1]. ok ok thanks again
Nice
I have this array as I counted the times it repeats. However, can someone give me a command to pull out the max values with the number to the left? Any advise? The first value in column on the right top doesn't matter as it should always be 1. I try the max command it only shows the max value of 3...:
1 0
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Nice
Great problem. Thank you.
Good one
Nice problem
Is there a way to test output without submitting?
I really liked this one
This was fun.
The problem should specify that if the same number repeats the longest run of consecutive numbers more than once, it must be repeated in the output.
Great stuff!
I like this problem, it very fun
Should be more specific about the expected output, the solution checkers expect the output of numbers to be listed in the order of when they occurred in the sequence and also if the same number repeats in the sequence (with the largest run again) it needs to be repeated in the output. Both of these criteria broke my solution.
that was the first matlab problem i solved by myself, i'm so excited:)
Nice problem!
to be honest, though I solved the problem ,I didn't quite understand how to improve my code. I mean, the top of the solutions seemed quite strange to me .why by doing that the size would be smaller?
Hi 昱树 何,
In Matlab Cody, size of a solution is calculated by various factors, one of them being the number of variables used. However, the top solution in this problem (and in many others as well) exploit the use of regexp to gain a lower solution size. This was done because earlier the scoring was based on your solution size, which has now been changed to a fixed 10 points.
Hope this helps.
took a while doing this but overall good question. I got stuck in the last test case.
Good problem
hm, I don't think the assertion in test 4 is intuitively correct. The problem asks for the number(s) that have the longest consecutive repetitions in the input vector. So there can be multiple numbers having the longest runs (of equal size), but if all those numbers are the same, as in [1 1 2 1 1], surely the answer is "1", not "1 and 1".
I can only imagine that's what the implementation turned out to produce and instead of fixing the implementation to fit the problem, the tests was introduced to check for exactly that output ;)
Which is fine, you'll always have clients in real life that say they want something and when you roll it out they meant something different, it just bothers me that my nice solution doesn't work because of this 😂
The problem specifically states that column and row vectors must be supported, but then doesn't have a test for this. I'd suggest changing at least one of the tests to use a column vector.
Or maybe add one test where it is asserted that `longrun(a) == transpose(longrun(transpose(a)))`
Nice one
nice
Tricky!
Repeated consecutively makes the problem more difficult otherwise, we can use unique and histcounts to solve this quickly.
Not as difficult as it seems.
Solution Comments

1 Comment
Great solution. I never see this shortest one.

1 Comment
i'm still amazed on how some solutions are of dimension 10. I'd love to be able to see the one for this problem

1 Comment
Not solved by myself

1 Comment
Good

1 Comment
Ended up using ifs for tests 4 and 6 if I remember correctly.
Test 4: wanted [1,1], I outputted [1]
As the question asks for the repeated number, I would argue mine is more correct.
test 6: wanted [3, 2]'. since unique sorts the array, mine was [2 3]'
Bad fix, but also a nasty bug. I would like to see more ambiguity in solutions.

2 Comments
This is the best source to learn an practice coding problems and improving algorithmic proficiency!!
Luckily stumbled upon your code.
Impressed with your coding prowess.

1 Comment
Caomparatively trickier if solved without functions

1 Comment
这道题的测试非常不科学。。。

1 Comment
difficult

1 Comment
Took a while for doing this, was stuck on the last test case.

1 Comment
I was struggling to solve this problem and then saw your solution. It was very elegent.

1 Comment
want to know an easy way

1 Comment
interesting

1 Comment
I really like your solution!

1 Comment
hello, i have problem understanding the code,
in:
for c=1:l
for r=c:l
if a(c)==a(r)
freq(c)=freq(c)+1
else
break
end
end
end
why didn't the c=1,r=5 satisfy the "a(c)==a(r)",in a(c)==a(r)
thanks a lot

3 Comments
hy,is there a problem with the above mentioned question
l=length(a);
freq=zeros(1,l);
for c=1:l
for r=c:l
if a(c)==a(r)
freq(c)=freq(c)+1;
else
break
end
end
end
M=max(freq);
idx=find(freq==M);
val=a(idx);
function val=longrun(a)
b = zeros(size(a));
old = a(1);
cnt = 0;
for idx = 1:length(a)
if a(idx) == old
cnt = cnt +1;
else
old = a(idx);
cnt = 1;
end
b(idx)=cnt;
end
bmax = max(b);
val = a(b==bmax);
end

1 Comment
function val=longrun(a)
a_len=length(a); % Calculate the length of a
c=ones(a_len,1); % Initialize the consecutive times of the corresponding position
for num_a=1:(a_len1)
n_val=1;
for next_a=(num_a+1):a_len
if a(num_a)==a(next_a) % If this number is equal to the following number, then n_val+1, and the next cycle
n_val=n_val+1;
c(num_a)=n_val;
else
c(num_a)=n_val; % If this number is not equal to the following number, assign the current n_val to the corresponding number of consecutive times, and jump out of the inner for loop
break
end
end
end
a_conti=max(c); %Find the largest number of consecutive times in the c array
max_location=find(c==a_conti); % Corresponds to the position of the maximum consecutive times
val=a(max_location); % Find out the corresponding position in a
end

1 Comment
Test 1 is error！
y_correct =[1 2];

2 Comments

1 Comment
Why is test 4 like that tho

1 Comment
Tricky one, but enjoyed solving it.

1 Comment
Tricky one.. but enjoyed it.

1 Comment
function val=longrun(a)
a_len=length(a); %计算a的长度
c=ones(a_len,1); %初始化对应位置的连续次数
for num_a=1:(a_len1)
n_val=1;
for next_a=(num_a+1):a_len
if a(num_a)==a(next_a) %如果这个数字和后面的数字相等，则n_val+1，并进行下一次循环
n_val=n_val+1;
c(num_a)=n_val;
else
c(num_a)=n_val; %如果这个数字和后面数字不等，则把当前的n_val赋值给对应数字的连续次数，并跳出内层for循环
break
end
end
end
a_conti=max(c); %找出c数组中最大的连续次数
max_location=find(c==a_conti); %对应最大连续次数的位置
val=a(max_location); %找出对应在a中的位置
end

1 Comment
Test 1 and 4 are wrong

3 Comments
@Piero Cimule
Could you please comment on this method? It is very unlikely for me as a beginner in Matlab to have such a concise code. You are more than welcome to elaborate here or via my email address zasevzasev42@gmail.com
Really succinct code. One of the best I saw so far in the contest. @Piero Cimule
@Jovan Krunic:
%
% find(vertcat(1,diff(a(:)),1))
% val=a(ans(diff(ans)==max(diff(ans))))
%
% the approach is:
% find the differences between the elements of the vector
% consecutive zero's mean there is a run of the same values
% get the indices of none zero elements of this vector
% get the differences of these indices (which are a measure for the length the run of consecutive zero's, the 'gap' resulting from removing the zeros)
% note that to make this work for vectors that start or end with the longest run we add a 1 to the beginning and the end of the first difference vector
% get the max value from this last result which is the max sequence length
% get the indices of these max values, that is, the first indices of the runs that have this max length
% note that this last result is always a column vector
% get the values of the first elements of the runs with max length from the original vector
% note that if the original vector was a row vector, the result will be a row vector and when it was a column vector the result is a column vector,
% as required in the problem description
function val=longrun(a)
val = [];
if length(a) > 0
% a(:) will convert a to a column vector
a_as_column_vector = a(:);
% differences between elements of a
diff_a = diff(a_as_column_vector);
% differences between elements of a enclosed in ones (to make sure first and last element are non zero)
vertcat_1_diff_a_1 = vertcat(1, diff_a, 1);
% non zero elements of vertcat_1_diff_a_1
find_vertcat_1_diff_a_1 = find(vertcat_1_diff_a_1);
% the difference between the non zero indices is equal to the length of the run of consecutive numbers
lengts_of_runs_of_consecutive_numbers = diff(find_vertcat_1_diff_a_1);
% set the elements that have the max length to one and the others to 0  note that shift of indices as a result of diff is compensated by adding 1 at the front of the vector
indices_of_elements_that_have_max_length_run = lengts_of_runs_of_consecutive_numbers == max(lengts_of_runs_of_consecutive_numbers);
indices_of_first_elements_of_longest_run_of_consecutive_numbers = find_vertcat_1_diff_a_1(indices_of_elements_that_have_max_length_run);
values_of_first_elements_of_longest_run_of_consecutive_numbers = a(indices_of_first_elements_of_longest_run_of_consecutive_numbers);
val = values_of_first_elements_of_longest_run_of_consecutive_numbers;
end
end

1 Comment
Could you check this for me, please?
[ra,ca]=size(a);
if ra>ca
a=a';
end
j=1;
V=[a(1,1);1];
for i=2:size(a,2)
X=a(1,i);
if V(1,j)==X
V(2,j)=V(2,j)+1;
else
j=j+1;
V(1,j)=X;
V(2,j)=V(2,j)+1;
end
end
val=V(V(2,:)==max(V(2,:)))
if ra>ca
val=val';
end

1 Comment
How else could you solve in any other traditional programming language?

1 Comment
Its good,

1 Comment
It was not so easy...

1 Comment
very clever!

1 Comment
i cannot understand
Problem Recent Solvers4250
Suggested Problems

1282 Solvers

It dseon't mettar waht oedrr the lrettes in a wrod are.
1255 Solvers

224 Solvers

409 Solvers

Find the next Fibonacci number
415 Solvers
More from this Author80
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!