Geeksforgeeks

It's live! The biggest programming festival has begun! Check out Geek Week now!

Error

Given a sorted array **arr** of **n** distinct elements and an integer **k**. The array elements denote positions of points on 1-D number line, find the maximum size of subset of points that can have consecutive values of points which can be formed by placing another **k** points on the number line.

**Example 1:**

**Input:
**n = 8, k = 4
arr[] = {1, 2, 3, 4, 10, 11, 14, 15}
**Output:** 8
**Explanation:
**For maximum size subset, it is optimal to
choose the points on number line at
coordinates 12, 13, 16 and 17, so that the
size of the consecutive valued subset will
become 8 which will be maximum.

**Example 2:**

**Input:
**n = 6, k = 5
arr[] = {7, 8, 12, 13, 15, 18}
**Output:** 10
**Explanation:
**For maximum size subset, it is optimal to choose
the points on number line at coordinates 9, 10,
11, 14 and 16, so that the size of the consecutive
valued subset will become 10 which will be maximum .

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **maximiseSubset()** which takes the array of integers **arr, n** and **k **as parameters and returns an integer denoting the answer.

**Expected Time Complexity:** O(n)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= n <= 10^{6}

1 <= k <= 10^{5}

1 <= arr[i] <= 10^{6}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Maximum size of consecutives

...