Problem Statement
An array of
Given an array of
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
{ 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.)
{ 5, 7, 9, 8492, 8493, 192398 }
Returns: 2
We can add the two numbers 6 and 8 to get a straight.
{ 1000000000, 999999999, 2345, 1800, 654321, 0, 3 }
Returns: 3
Testing if they can handle a billion
{ 1000, 2000, 3000, 4000 }
Returns: 4
For example, we can add 1997, 1998, 1999, and 2001.
{ 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.
{ 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.
{ 1000, 1005, 1010, 1015, 995, 990, 985 }
Returns: 4
Testing numbers that are five apart.
{ 1000, 1002, 1004, 2000, 2001 }
Returns: 2
{ 1000, 1002, 1003, 1007, 1009, 1013 }
Returns: 2
{ 3000, 3001, 3002 }
Returns: 2
{ 6, 1, 9, 5, 7, 15, 8 }
Returns: 0
From the problem statement. It already is a straight.
{ 118, 120, 123, 124, 127, 126, 131, 132 }
Returns: 1
Just add 125.
{ 0 }
Returns: 4
{ 1000000000 }
Returns: 4
{1, 3, 5, 7, 9 }
Returns: 2
{5, 7, 9, 8492, 8494, 192398 }
Returns: 2
{1, 2, 3, 4, 5, 6, 7, 8 }
Returns: 0