Statistics

Problem Statement for "IntervalIntersections"

Problem Statement

For any x ≤ y the interval [x,y] contains all real numbers between x and y, inclusive.
The length of the interval [x,y] is y-x.
Two intervals intersect if they have at least one number in common.


You are given the ints x1, y1, x2, and y2.
These are the endpoints of two intervals: [x1,y1] and [x2,y2].
We are looking for an interval [a,b] that intersects both given intervals.
Return the smallest possible length of the interval [a,b].

Definition

Class:
IntervalIntersections
Method:
minLength
Parameters:
int, int, int, int
Returns:
int
Method signature:
int minLength(int x1, int y1, int x2, int y2)
(be sure your method is public)

Constraints

  • x1,y1,x2,y2 will be between 1 and 10^6, inclusive.
  • x1 will be less than or equal to y1.
  • x2 will be less than or equal to y2.

Examples

  1. 3

    6

    1

    2

    Returns: 1

    The two given intervals are [3,6] and [1,2]. The unique shortest interval that intersects both of them is the interval [2,3]. Its length is 3-2 = 1.

  2. 1

    2

    3

    6

    Returns: 1

    The same two intervals as in Example 0, only in different order. The correct return value is the same.

  3. 1

    10

    2

    5

    Returns: 0

    In this test case the optimal length of the interval [a,b] is 0. Examples of such intervals include [2,2] and [4,4].

  4. 4

    5

    1

    4

    Returns: 0

  5. 1

    1

    1000000

    1000000

    Returns: 999999

  6. 312348

    819554

    281345

    589030

    Returns: 0

  7. 179349

    507166

    358369

    738215

    Returns: 0

  8. 115075

    181266

    5090

    556250

    Returns: 0

  9. 116437

    202335

    534613

    981260

    Returns: 332278

  10. 380565

    789980

    26588

    378986

    Returns: 1579

  11. 240474

    615891

    606139

    945634

    Returns: 0

  12. 221991

    733353

    438905

    441547

    Returns: 0

  13. 414616

    868217

    18462

    557668

    Returns: 0

  14. 219621

    985380

    102454

    212064

    Returns: 7557

  15. 371857

    783935

    41429

    774080

    Returns: 0

  16. 630052

    660576

    870658

    987696

    Returns: 210082

  17. 544929

    863609

    663688

    950181

    Returns: 0

  18. 528777

    755332

    544424

    629407

    Returns: 0

  19. 542308

    830645

    689509

    806142

    Returns: 0

  20. 103397

    385741

    142326

    488476

    Returns: 0

  21. 177787

    322513

    463633

    604708

    Returns: 141120

  22. 620240

    725668

    340787

    959517

    Returns: 0

  23. 369261

    387188

    72523

    231058

    Returns: 138203

  24. 335311

    600705

    12574

    520766

    Returns: 0

  25. 302007

    866098

    319930

    915951

    Returns: 0

  26. 293996

    520155

    707079

    707201

    Returns: 186924

  27. 564356

    932246

    734719

    883266

    Returns: 0

  28. 637941

    755191

    67203

    810774

    Returns: 0

  29. 534290

    981522

    235934

    336049

    Returns: 198241

  30. 189271

    419578

    54307

    725310

    Returns: 0

  31. 127050

    743717

    810708

    964488

    Returns: 66991

  32. 94406

    997267

    77357

    629060

    Returns: 0

  33. 442630

    550870

    23906

    196016

    Returns: 246614

  34. 619446

    951455

    137225

    682966

    Returns: 0

  35. 338957

    713770

    134447

    951653

    Returns: 0

  36. 1

    1000000

    1

    1000000

    Returns: 0

  37. 2

    4

    1

    3

    Returns: 0

  38. 2

    11

    1

    10

    Returns: 0

  39. 5

    6

    5

    7

    Returns: 0

  40. 3

    6

    5

    10

    Returns: 0

  41. 2

    4

    1

    5

    Returns: 0

  42. 3

    5

    4

    7

    Returns: 0

  43. 1

    3

    2

    4

    Returns: 0

  44. 1

    6

    3

    7

    Returns: 0

  45. 10

    12

    1

    2

    Returns: 8

  46. 5

    10

    1

    8

    Returns: 0

  47. 6

    8

    3

    4

    Returns: 2

  48. 1

    4

    3

    5

    Returns: 0

  49. 1

    5

    3

    10

    Returns: 0

  50. 2

    3

    3

    4

    Returns: 0

  51. 2

    3

    2

    3

    Returns: 0

  52. 1

    5

    3

    7

    Returns: 0

  53. 100

    200

    50

    150

    Returns: 0

  54. 1

    10

    2

    15

    Returns: 0

  55. 4

    10

    2

    7

    Returns: 0

  56. 2

    5

    1

    3

    Returns: 0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: