TheConsecutiveIntegersDivOne
SRM 646 · 2014-12-30 · by Vasyl[alphacom]
SRM 646 · 2014-12-30 · by Vasyl[alphacom] · Simple Search, Iteration
Problem Statement
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.
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
0)
{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)
{1, 100}
1
Returns: 0
No operation is needed.
2)
{-96, -53, 82, -24, 6, -75}
2
Returns: 20
3)
{64, -31, -56}
2
Returns: 24
4)
{14, 36, 74}
1
Returns: 0
Submissions are judged against all 99 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class TheConsecutiveIntegersDivOne with a public method int find(vector<int> numbers, int k) · 99 test cases · 2 s / 256 MB per case