JSFiddle - React, Tailwind, and code Playground
JavaScript
/*
**[--SCROLL DOWN FOR CODE--]**
http://codegolf.stackexchange.com/questions/4534/determine-longest-group-of-consecutive-numbers
INPUT:
10 random numbers
each number is greater than 0 and less than 100
list of numbers is presorted, lowest to highest
the list will not contain any duplicate numbers
the solution should be in perl or php ideally, but can also be a general psudocode/algorithm description
efficiency is key!
CHALLENGE:
Write a function that would take in the numbers, and return the count of the longest group of consecutive numbers.
EXAMPLE OUTPUT:
1 2 33 44 55 66 77 88 98 => return of the function would be 2
1 3 23 24 30 48 49 70 80 => return of the function would be 2
6 9 50 51 52 72 81 83 92 => return of the function would be 3
*/
/*
// -------------------------
// Ungolfed Simple Algorithm
var input = prompt();
var numbers = input.split(' ');
var consec = maxConsec = 1;
for (var i = 1; i < numbers.length; i++)
if (numbers[i] - numbers[i - 1] == 1)
consec++;
else
{
maxConsec = Math.max(consec, maxConsec);
consec = 1;
}
alert(maxConsec);
*/
/*
// -----------------
// Complex Algorithm
n=prompt(i=c=m=1).split(' ');
for(a=n[0];i<n.length;i++)
{
b=n[i];
c*=!(b-a-1)|0;
m=Math.max(++c,m);
a=b;
}
alert(m);
*/
/*
// ------------------------------------
// Fully golfed solution #1 - 107 chars
n=prompt(i=c=m=1).split(' ');for(a=n[0];i<n.length;i++)b=n[i],c*=!(b-a-1)|0,m=Math.max(++c,m),a=b;alert(m);
//*/
/*
// -----------------------------------
// Fully golfed solution #2 - 93 chars
// Converted a,b references to n[#], saved a few chars
// Converted Math.max to if statement, which was actually shorter
for(n=prompt(i=c=m=1).split(' ');i<n.length;i++)c*=!(n[i]-n[i-1]-1)|0,m=(++c>m)?c:m;alert(m);
//*/
/*
// -----------------------------------
// Fully golfed solution #3 - 80 chars
// Started from 9 -> 1 so i could use --i and remove i++.
// Use i as condition by starting from top down
//...