## Uber Interview Question

Software Engineer / Developers**Country:**India

**Interview Type:**Written Test

They are basically calculating the gaps between the presentations in the arr array. Now you can notice that the longest time period you can get by at most k reschedules is k+1 consecutive gaps.

For example, if you have gap0, event1, gap1, event2, gap2, event3, gap3, event4, gap4. If k=2, in two reschedules you can switch event[i] with either gap[i] or gap[i-1] to get a series of 3 consecutive gaps. Now, we take the maximum size of k consecutive gaps to get the answer

There is a small mistake in the last for loop, instead of arr[i - k] and arr[k] it will be arr[i - k - 1] and arr[i] respectively.

Previous code:

```
for (int i = k; i <= len; i++) {
sum = sum - arr[i-k] + arr[k];
max = Math.max(max, sum);
}
```

Correct code:

```
for (int i = k; i <= len; i++) {
sum = sum - arr[i-k-1] + arr[i];
max = Math.max(max, sum);
}
```

There is a small mistake in the last for loop, instead of arr[i - k] and arr[k] it will be arr[i - k - 1] and arr[i] respectively.

Previous code:

```
for (int i = k; i <= len; i++) {
sum = sum - arr[i-k] + arr[k];
max = Math.max(max, sum);
}
```

Correct code:

```
for (int i = k; i <= len; i++) {
sum = sum - arr[i-k-1] + arr[i];
max = Math.max(max, sum);
}
```

- vishal.iitr February 18, 2020