Task · algorithms easy JavaScript ·
Summary ranges
Given a sorted list of distinct integers, summarize it as consecutive ranges. A range of several numbers is written "a->b", a single number as "a". Return the ranges in order.
Signature
/**
* @param {number[]} nums
* @returns {string[]}
*/
function summary_ranges(nums)Examples
summary_ranges([0, 1, 2, 4, 5, 7]) // -> ["0->2", "4->5", "7"]
summary_ranges([0, 2, 3, 4, 6, 8, 9]) // -> ["0", "2->4", "6", "8->9"]
summary_ranges([]) // -> []Plus 5 hidden tests and 2 large-input tests under a time limit; their inputs are not shown. Tasks are given to the models in English.
#1DeepSeek V4 Pro 0423not solved · 9 of 10 testsanswer in 8.9 s · runtime 2.6 s · $0.0007 ✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 2 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] 1 ms ✓ 1 ms✓ 1 ms✓ <1 ms✓ 1 ms✓ 1 ms⏱ time limit exceeded⏱ 95 msCode
/** * @param {number[]} nums * @returns {string[]} */ function summary_ranges(nums) { const result = []; const n = nums.length; if (n === 0) return result; let start = nums[0]; let prev = start; for (let i = 1; i < n; i++) { if (nums[i] === prev + 1) { prev = nums[i]; } else { result.push(start === prev ? `${start}` : `${start}->${prev}`); start = nums[i]; prev = start; } } result.push(start === prev ? `${start}` : `${start}->${prev}`); return result; }Second try
not solved · 9 of 10 testsanswer in 5.6 s · $0.0009The model saw how many tests failed, the failing examples with the expected and returned values, and only why hidden tests failed, not their inputs. Doesn't count for places or the rating.
✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 2 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] <1 ms ✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ time limit exceeded⏱ 130 msCode
/** * @param {number[]} nums * @returns {string[]} */ function summary_ranges(nums) { const result = []; const n = nums.length; if (n === 0) return result; let i = 0; while (i < n) { let start = nums[i]; let j = i + 1; while (j < n && nums[j] === nums[j-1] + 1) { j++; } let end = nums[j-1]; if (start === end) { result.push(start.toString()); } else { result.push(start + "->" + end); } i = j; } return result; }#1OpenAI GPT-6.1 Terranot solved · 9 of 10 testsanswer in 2.2 s · runtime 2.6 s · $0.0009 ✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 3 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] <1 ms ✓ <1 ms✓ 1 ms✓ <1 ms✓ 6 ms✓ 1 ms⏱ time limit exceeded⏱ 105 msCode
function summary_ranges(nums) { const result = []; const n = nums.length; let i = 0; while (i < n) { const start = nums[i]; let end = start; i++; while (i < n && nums[i] - end === 1) { end = nums[i++]; } result.push(start === end ? String(start) : `${start}->${end}`); } return result; }Second try
not solved · 9 of 10 testsanswer in 2.8 s · $0.0012The model saw how many tests failed, the failing examples with the expected and returned values, and only why hidden tests failed, not their inputs. Doesn't count for places or the rating.
✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 1 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] <1 ms ✓ <1 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 1 ms⏱ time limit exceeded⏱ 127 msCode
function summary_ranges(nums) { const n = nums.length; if (n === 0) return []; const result = []; let start = nums[0]; let prev = start; for (let i = 1; i < n; i++) { const value = nums[i]; if (value - prev !== 1) { result.push(start === prev ? String(start) : start + "->" + prev); start = value; } prev = value; } result.push(start === prev ? String(start) : start + "->" + prev); return result; }#1Qwen3.8 Max (0902)not solved · 9 of 10 testsanswer in 7.6 s · runtime 2.6 s · $0.0026 ✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 1 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] 1 ms ✓ 3 ms✓ 1 ms✓ 1 ms✓ 26 ms✓ 1 ms⏱ time limit exceeded⏱ 97 msCode
function summary_ranges(nums) { if (nums.length === 0) return []; const result = []; let start = nums[0]; let prev = nums[0]; for (let i = 1; i <= nums.length; i++) { if (i === nums.length || nums[i] !== prev + 1) { if (start === prev) { result.push(String(start)); } else { result.push(start + "->" + prev); } if (i < nums.length) { start = nums[i]; prev = nums[i]; } } else { prev = nums[i]; } } return result; }Second try
not solved · 9 of 10 testsanswer in 9.5 s · $0.0039The model saw how many tests failed, the failing examples with the expected and returned values, and only why hidden tests failed, not their inputs. Doesn't count for places or the rating.
✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 3 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] 1 ms ✓ summary_ranges([])expected [] <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ 4 ms✓ 1 ms⏱ time limit exceeded⏱ 118 msCode
function summary_ranges(nums) { const n = nums.length; if (n === 0) return []; const result = []; let i = 0; while (i < n) { let j = i; while (j + 1 < n && nums[j + 1] === nums[j] + 1) { j++; } if (i === j) { result.push(String(nums[i])); } else { result.push(nums[i] + "->" + nums[j]); } i = j + 1; } return result; }#1Mistral Devstral 2 2512not solved · 9 of 10 testsanswer in 1.2 s · runtime 2.7 s · $0.0005 ✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 2 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] 2 ms ✓ summary_ranges([])expected [] <1 ms ✓ <1 ms✓ 1 ms✓ 35 ms✓ <1 ms✓ <1 ms⏱ time limit exceeded⏱ 134 msCode
function summary_ranges(nums) { if (nums.length === 0) return []; const ranges = []; let start = nums[0]; for (let i = 1; i < nums.length; i++) { if (nums[i] !== nums[i - 1] + 1) { if (start === nums[i - 1]) { ranges.push(start.toString()); } else { ranges.push(start + '->' + nums[i - 1]); } start = nums[i]; } } if (start === nums[nums.length - 1]) { ranges.push(start.toString()); } else { ranges.push(start + '->' + nums[nums.length - 1]); } return ranges; }Second try
not solved · 9 of 10 testsanswer in 1.1 s · $0.0005The model saw how many tests failed, the failing examples with the expected and returned values, and only why hidden tests failed, not their inputs. Doesn't count for places or the rating.
✓ summary_ranges([0, 1, 2, 4, 5, 7])expected ["0->2", "4->5", "7"] 2 ms ✓ summary_ranges([0, 2, 3, 4, 6, 8, 9])expected ["0", "2->4", "6", "8->9"] <1 ms ✓ summary_ranges([])expected [] <1 ms ✓ 5 ms✓ 31 ms✓ 1 ms✓ 2 ms✓ 4 ms⏱ time limit exceeded⏱ 134 msCode
function summary_ranges(nums) { const ranges = []; let start = nums[0]; for (let i = 1; i <= nums.length; i++) { if (i === nums.length || nums[i] !== nums[i - 1] + 1) { ranges.push(start === nums[i - 1] ? start.toString() : start + '->' + nums[i - 1]); if (i < nums.length) start = nums[i]; } } return ranges; }
Tokens cost: $0.01 · How rounds are run and scored →