4 kyu

Sum of Intervals

11,533 of 40,548xDranik

Description:

Write a function called sumIntervals/sum_intervals that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once.

Intervals

Intervals are represented by a pair of integers in the form of an array. The first value of the interval will always be less than the second value. Interval example: [1, 5] is an interval from 1 to 5. The length of this interval is 4.

Overlapping Intervals

List containing overlapping intervals:

[
   [1, 4],
   [7, 10],
   [3, 5]
]

The sum of the lengths of these intervals is 7. Since [1, 4] and [3, 5] overlap, we can treat the interval as [1, 5], which has a length of 4.

Examples:

sumIntervals( [
   [1, 2],
   [6, 10],
   [11, 15]
] ) => 9

sumIntervals( [
   [1, 4],
   [7, 10],
   [3, 5]
] ) => 7

sumIntervals( [
   [1, 5],
   [10, 20],
   [1, 6],
   [16, 19],
   [5, 11]
] ) => 19

sumIntervals( [
   [0, 20],
   [-100000000, 10],
   [30, 40]
] ) => 100000030

Tests with large intervals

Your algorithm should be able to handle large intervals. All tested intervals are subsets of the range [-1000000000, 1000000000].

Algorithms
Performance

Stats:

CreatedDec 23, 2013
PublishedDec 23, 2013
Warriors Trained99357
Total Skips16911
Total Code Submissions324476
Total Times Completed40548
JavaScript Completions11533
CoffeeScript Completions106
PHP Completions894
Python Completions16452
Ruby Completions778
Java Completions2843
C++ Completions2541
Clojure Completions142
C# Completions2079
Elixir Completions118
Dart Completions451
Crystal Completions16
Julia Completions68
Haskell Completions434
TypeScript Completions1155
Racket Completions26
C Completions930
NASM Completions11
COBOL Completions12
Rust Completions472
Go Completions439
Scala Completions86
λ Calculus Completions6
Lua Completions48
Total Stars3069
% of votes with a positive feedback rating92% of 3942
Total "Very Satisfied" Votes3358
Total "Somewhat Satisfied" Votes516
Total "Not Satisfied" Votes68
Total Rank Assessments10
Average Assessed Rank
4 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
5 kyu
Ad
Contributors
  • xDranik Avatar
  • jhoffner Avatar
  • Unnamed Avatar
  • CodeVortex Avatar
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • JohanWiltink Avatar
  • KataSideKick Avatar
  • FArekkusu Avatar
  • monadius Avatar
  • Awesome A.D. Avatar
  • hobovsky Avatar
  • uniapi Avatar
  • user8436785 Avatar
  • gargamel Avatar
  • dolamroth Avatar
  • akar-0 Avatar
  • fcr-- Avatar
  • KayleighWasTaken Avatar
Ad