Count Positions on Street Problem

Count Positions on Street Problem — ExecCode Easy DSA Practice

Solve the Count Positions on Street problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

A street has n positions numbered 0 to n-1. Given a list of inclusive ranges [l, r] where workers paint that stretch of the street, return the number of distinct positions that get painted at least once.

Examples

Input n = 6, ranges = [[0, 1], [4, 5]]; Output 4. Input n = 5, ranges = [[0, 4]]; Output 5. Input n = 4, ranges = [[0, 0], [3, 3]]; Output 2

Constraints

1 ≤ n ≤ 10⁵ 0 ≤ ranges.length ≤ 10⁴ 0 ≤ l ≤ r < n

Practice Count Positions on Street free on ExecCode. Browse DSA problems, topic map, and placement guides.