Statistics

Problem Statement for "StraightArray"

Problem Statement

An array of ints is said to be a straight if it contains five elements that are five consecutive numbers. For example, the array { 6, 1, 9, 5, 7, 15, 8 } is a straight because it contains 5, 6, 7, 8, and 9.

Given an array of ints, nums, return the minimum number of ints that must be added to that array, so that the augmented array is a straight.

Definition

Class:
StraightArray
Method:
howManyMore
Parameters:
int[]
Returns:
int
Method signature:
int howManyMore(int[] nums)
(be sure your method is public)

Constraints

  • nums contains between 1 and 50 elements, inclusive.
  • Each element in nums is between 0 and 1,000,000,000, inclusive.
  • All elements in nums are distinct.

Examples

  1. { 5, 6, 7 }

    Returns: 2

    We can make a straight if we add the two numbers 8 and 9, so we return 2. (Note that we cannot make a straight by adding fewer than two numbers.)

  2. { 5, 7, 9, 8492, 8493, 192398 }

    Returns: 2

    We can add the two numbers 6 and 8 to get a straight.

  3. { 1000000000, 999999999, 2345, 1800, 654321, 0, 3 }

    Returns: 3

    Testing if they can handle a billion

  4. { 1000, 2000, 3000, 4000 }

    Returns: 4

    For example, we can add 1997, 1998, 1999, and 2001.

  5. { 166606272, 795015165, 974922007, 511003430, 250034850, 922003483, 852533014, 142135207, 935053829, 133348166, 687573216, 510577365, 959121378, 643901600, 709418941, 847551862, 418965247, 465225135, 470925805, 657726035, 266079053, 906056923, 799539654, 367497791, 424545728, 476332623, 66977775, 962696103, 700285385, 577170506, 113081915, 994511293, 22896377, 944605340, 234640348, 727977905, 589577809, 140492223, 881421665, 9089897, 165192277, 157383020, 89357461, 634637039, 133348170, 490311269, 224757620, 623107865, 890450914, 220804058 }

    Returns: 3

    Testing fifty values. We get a straight if we add 133348167, 133348168, and 133348169.

  6. { 399710788, 908814160, 716153223, 970654133, 506644631, 217544361, 260957226, 132981872, 483739151, 595795986, 824140293, 362514681, 716153227, 336131843, 406529057, 147725164 }

    Returns: 3

    We get a straight if we add 716153224, 716153225, and 716153226.

  7. { 1000, 1005, 1010, 1015, 995, 990, 985 }

    Returns: 4

    Testing numbers that are five apart.

  8. { 1000, 1002, 1004, 2000, 2001 }

    Returns: 2

  9. { 1000, 1002, 1003, 1007, 1009, 1013 }

    Returns: 2

  10. { 3000, 3001, 3002 }

    Returns: 2

  11. { 6, 1, 9, 5, 7, 15, 8 }

    Returns: 0

    From the problem statement. It already is a straight.

  12. { 118, 120, 123, 124, 127, 126, 131, 132 }

    Returns: 1

    Just add 125.

  13. { 0 }

    Returns: 4

  14. { 1000000000 }

    Returns: 4

  15. {1, 3, 5, 7, 9 }

    Returns: 2

  16. {5, 7, 9, 8492, 8494, 192398 }

    Returns: 2

  17. {1, 2, 3, 4, 5, 6, 7, 8 }

    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: