Problem Statement
John and Brus have some integers.
You are given these integers in a int[] numbers.
In one operation they can pick a number and increase/decrease it by 1.
They want to have at least k consecutive integers.
The order in which those integers appear in numbers does not matter.
Return the minimal number of operations required to achieve the goal.
Definition
- Class:
- TheConsecutiveIntegersDivOne
- Method:
- find
- Parameters:
- int[], int
- Returns:
- int
- Method signature:
- int find(int[] numbers, int k)
- (be sure your method is public)
Constraints
- numbers will contain between 2 and 47 elements, inclusive.
- Each element of numbers will be between -10,000,000 and 10,000,000, inclusive.
- All elements of numbers will be distinct.
- k will be between 1 and the number of elements in numbers, inclusive.
Examples
{4, 7, 47}2
Returns: 2
There are three optimal strategies: Increase 4 two times to obtain {6,7,47}. Decrease 7 two times to obtain {4,5,47}. Increase 4 and decrease 7 to obtain {5,6,47}.{1, 100}1
Returns: 0
No operation is needed.{-96, -53, 82, -24, 6, -75}2
Returns: 20
{64, -31, -56}2
Returns: 24
{-96, -53, 82, -24, 6, -75}4
Returns: 90