long - MATLAB Cody - MATLAB Central

long

30710
Rank
2
Badges
145
Score
1 – 15 of 15

long submitted a Comment to Problem 672. Longest run of consecutive numbers

function val=longrun(a) % Given vector % a = [1 2 2 2 1 3 2 1 4 5 1]; maxn = 1; skip = 2; i = 1; sz = size(a); if sz(1)==1 a = [a,max(a)+1]; elseif sz(2) == 1 a = [a;max(a)+1]; end max1 = -(max(a)+1); while i < length(a) for j = skip:length(a) if a(j) ~= a(i) skip = j+1; % maxn = max(maxn, j-i); if (j-i>maxn) max1 = a(i); elseif (j-i==maxn) if (i~=1) max1 = [max1, a(i)]; else max1 = a(i); end end maxn = max(maxn, j-i); i = j; break; end end end val = max1; if sz(2) == 1 val = val' end end

on 26 Apr 2024

long received Solver badge for Solution 13692371

on 26 Apr 2024

1 – 15 of 15
Go to top of page