Given a set of integers, the task is to create a list of closed intervals where each interval includes its endpoints. For example, the interval 1 to 5 includes each value 'x' that satisfies the condition 1 ≤ x ≤ 5.
Intervals can contain consecutive values if the difference between a value and the next smaller value in the set equals one. If this condition is not met, a new interval begins. The start value of an interval is excluded from this rule. A single value that does not fit into an existing interval becomes the start and endpoint of a new interval.
Input: An array of integers.
Output: A matrix with two columns; each row containing two integers indicating the endpoints of the interval. The matrix should be sorted by the start point of each interval.
Examples:
create_intervals([1, 2, 3, 4, 5, 7, 8, 12]) returns [ 1, 5; 7, 8 ; 12, 12] create_intervals([1, 2, 3, 6, 7, 8, 4, 5]) returns [1, 8]
Solution Stats
Problem Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers14
Suggested Problems
-
11376 Solvers
-
Circular Primes (based on Project Euler, problem 35)
658 Solvers
-
Edges of a n-dimensional Hypercube
90 Solvers
-
Integer Sequence - II : New Fibonacci
700 Solvers
-
Factorions: Numbers that equal the sum of the factorials of their digits
84 Solvers
More from this Author53
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!