I am solving this question which requires some optimized techniques to solve it. I can think of the brute force method only which requires combinatorics.

Given an array A consisting of n integers. We call an integer "good" if it lies in the range [L,R] (i.e. L≤x≤R). We need to make sure if we pick up any K integers from the array at least one of them should be a good integer.

For achieving this, in a single operation, we are allowed to increase/decrease any element of the array by one.

What will be the minimum number of operations we will need for a fixed k?"

i.e k=1 to n.

```
input:
L R
1 2
A=[ 1 3 3 ]
output:
for k=1 : 2
for k=2 : 1
for k=3 : 0
```

For k=1, you have to convert both the 3s into 2s to make sure that if you select any one of the 3 integers, the selected integer is good.

For k=2, one of the possible ways is to convert one of the 3s into 2.

For k=3, no operation is needed as 1 is a good integer.

# Best How To :

As burnpanck has explained in his answer, to make sure that when you pick any k elements in the array, and at least one of them is in range [L,R], we need to make sure that there are at least `n - k + 1`

numbers in range [L,R] in the array.

So, first , for each element, we calculate the cost to make this element be a `valid`

element (which is in range [L,R]) and store those cost in an array `cost`

.

We notice that:

For k = 1, the minimum cost is the sum of array `cost`

.

For k = 2, the minimum cost is the sum of `cost`

, minus the largest element.

For k = 3, the minimum cost is the sum of `cost`

, minus two largest elements.

...

So, we need to have a `prefixSum`

array, which ith position is the sum of **sorted** `cost`

array from 0 to ith.

After calculate prefixSum, we can answer result for each k in O(1)

So here is the algo in Java, notice the time complexity is **O(n logn)**:

```
int[]cost = new int[n];
for(int i = 0; i < n; i++)
cost[i] = //Calculate min cost for element i
Arrays.sort(cost);
int[]prefix = new int[n];
for(int i = 0; i < n; i++)
prefix[i] = cost[i] + (i > 0 ? prefix[i - 1] : 0);
for(int i = n - 1; i >= 0; i--)
System.out.println("Result for k = " + (n - i) + " is " + prefix[i]);
```