Given an integer array 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. Note that all coordinates should be distinct and elements of array are in increasing order.
Examples:
Input : arr[] = {1, 2, 3, 4, 10, 11, 14, 15},
k = 4
Output : 8
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 .
Input : arr[] = {7, 8, 12, 13, 15, 18}
k = 5
Output : 10
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 .
Brute force Approach ( Time Complexity – O(N2) ):
A brute force consists of checking all the possible (l, r) pairs for the condition ((arr[r]-arr[l])-(r-l)) <= k. In order to find out if a pair (l, r) is valid, we should check if the number of points that need to be placed between these two initial ones is not greater than K. Since arr[i] is the coordinate of the i-th point in the input array (arr), then we need to check if (arr[r] – arr[l]) – (r – l ) <= k.
This solution has a complexity of O(N2).
C++
/* C++ program to find the maximum size of subset of points that can have consecutive values using brute force */#include <bits/stdc++.h> using namespace std; int maximiseSubset(int arr[], int n, int k) { // Since we can always enforce the solution // to contain all the K added points int ans = k; for (int l = 0; l < n - 1; l++) for (int r = l; r < n; r++) // check if the number of points that // need to be placed between these two // initial ones is not greater than k if ((arr[r] - arr[l]) - (r - l) <= k) ans = max(ans, r - l + k + 1); return (ans); } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 10, 11, 14, 15 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; printf("%dn", maximiseSubset(arr, n, k)); return 0; } |
Java
/* Java program to find the maximum size of subset of points that can have consecutive values using brute force */import java.util.*; class GFG { static int maximiseSubset(int[] arr, int n, int k) { // Since we can always enforce the solution // to contain all the K added points int ans = k; for (int l = 0; l < n - 1; l++) for (int r = l; r < n; r++) // check if the number of points that // need to be placed between these two // initial ones is not greater than k if ((arr[r] - arr[l]) - (r - l) <= k) ans = Math.max(ans, r - l + k + 1); return (ans); } // Driver code public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 10, 11, 14, 15 }; int n = arr.length; int k = 4; System.out.println(maximiseSubset(arr, n, k)); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python 3
# Python 3 program to find the maximum size # of subset of points that can have consecutive # values using brute force def maximiseSubset(arr , n, k): # Since we can always enforce the solution # to contain all the K added points ans = k for l in range(n - 1): for r in range(l, n): # check if the number of points that # need to be placed between these two # initial ones is not greater than k if ((arr[r] - arr[l]) - (r - l) <= k) : ans = max(ans, r - l + k + 1) return (ans) # Driver code if __name__ == "__main__": arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ] n = len(arr) k = 4 print(maximiseSubset(arr, n, k)) # This code is contributed by ita_c |
C#
/* C# program to find the maximum size of subset of points that can have consecutive values using brute force */using System; class GFG { static int maximiseSubset(int[] arr, int n, int k) { // Since we can always enforce // the solution to contain all // the K added points int ans = k; for (int l = 0; l < n - 1; l++) for (int r = l; r < n; r++) // check if the number of // points that need to be // placed between these // two initial ones is not // greater than k if ((arr[r] - arr[l]) - (r - l) <= k) ans = Math.Max(ans, r - l + k + 1); return (ans); } // Driver code public static void Main() { int[] arr = {1, 2, 3, 4, 10, 11, 14, 15}; int n = arr.Length; int k = 4; Console.WriteLine(maximiseSubset(arr, n, k)); } } // This code is contributed by anuj_67. |
PHP
<?php // PHP program to find the maximum size // of subset of points that can have // consecutive values using brute force function maximiseSubset($arr, $n, $k) { // Since we can always enforce // the solution to contain all // the K added points $ans = $k; for ($l = 0; $l < $n - 1; $l++) for ($r = $l; $r < $n; $r++) // check if the number of points that // need to be placed between these two // initial ones is not greater than k if (($arr[$r] - $arr[$l]) - ($r - $l) <= $k) $ans = max($ans, $r - $l + $k + 1); return ($ans); } // Driver code $arr = array(1, 2, 3, 4, 10, 11, 14, 15 ); $n = sizeof($arr); $k = 4; echo (maximiseSubset($arr, $n, $k)); // This code is contributed // by Sach_Code ?> |
Output:
8
Efficient Approach (Time complexity – O(N))
In order to optimize the brute force, notice that if r increases, then l also increases (or at least stays the same). We can maintain two indexes. Initialise l and r both as 0. Then we start incrementing r. As we do this, at each step we increment l until the condition used in brute force approach becomes true. When r reaches last index, we stop.
C++
/* C++ program to find the maximum size of subset of points that can have consecutive values using efficient approach */#include <bits/stdc++.h> using namespace std; int maximiseSubset(int arr[], int n, int k) { // Since we can always enforce the // solution to contain all the K added // points int ans = k; int l = 0, r = 0; while (r < n) { // increment l until the number of points // that need to be placed between index l // and index r is not greater than k while ((arr[r] - arr[l]) - (r - l) > k) l++; // update the solution as below ans = max(ans, r - l + k + 1); r++; } return (ans); } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 10, 11, 14, 15 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; printf("%d", maximiseSubset(arr, n, k)); return 0; } |
Java
/* Java program to find the maximum size of subset of points that can have consecutive values using efficient approach */import java.util.*; class GFG { static int maximiseSubset(int[] arr, int n, int k) { // Since we can always enforce the // solution to contain all the K added // points int ans = k; int l = 0, r = 0; while (r < n) { // increment l until the number of points // that need to be placed between index l // and index r is not greater than k while ((arr[r] - arr[l]) - (r - l) > k) l++; // update the solution as below ans = Math.max(ans, r - l + k + 1); r++; } return (ans); } // Driver code public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 10, 11, 14, 15 }; int n = arr.length; int k = 4; System.out.println(maximiseSubset(arr, n, k)); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python3
# Python 3 program to find the maximum size # of subset of points that can have consecutive # values using efficient approach def maximiseSubset(arr, n, k): # Since we can always enforce the solution # to contain all the K added points ans = k; l = 0; r = 0; while (r < n): # increment l until the number of points # that need to be placed between index l # and index r is not greater than k while ((arr[r] - arr[l]) - (r - l) > k): l = l + 1; # update the solution as below ans = max(ans, r - l + k + 1); r = r + 1; return (ans); # Driver code arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ]; n = len(arr); k = 4; print(maximiseSubset(arr, n, k)); # This code is contributed # by Akanksha Rai |
C#
/* C# program to find the maximum size of subset of points that can have consecutive values using efficient approach */using System; class GFG { static int maximiseSubset(int[] arr, int n, int k) { // Since we can always enforce // the solution to contain all // the K added points int ans = k; int l = 0, r = 0; while (r < n) { // increment l until the // number of points that // need to be placed // between index l and // index r is not greater // than k while ((arr[r] - arr[l]) - (r - l) > k) l++; // update the // solution as below ans = Math.Max(ans, r - l + k + 1); r++; } return (ans); } // Driver code public static void Main() { int[] arr = {1, 2, 3, 4, 10, 11, 14, 15}; int n = arr.Length; int k = 4; Console.WriteLine(maximiseSubset(arr, n, k)); } } // This code is contributed // by anuj_67. |
PHP
<?php // PHP program to find the maximum size // of subset of points that can have // consecutive values using efficient approach function maximiseSubset($arr, $n, $k) { // Since we can always enforce the // solution to contain all the K // added points $ans = $k; $l = 0; $r = 0; while ($r < $n) { // increment l until the number of points // that need to be placed between index l // and index r is not greater than k while (($arr[$r] - $arr[$l]) - ($r - $l) > $k) $l++; // update the solution as below $ans = max($ans, $r - $l + $k + 1); $r++; } return ($ans); } // Driver code $arr = array(1, 2, 3, 4, 10, 11, 14, 15 ); $n = sizeof($arr); $k = 4; echo(maximiseSubset($arr, $n, $k)); // This code is contributed by Mukul Singh ?> |
Output:
8
Time Complexity: O(N)
This article is contributed by Divyanshu_Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximise array sum after taking non-overlapping sub-arrays of length K
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Sum of subsets of all the subsets of an array | O(3^N)
- Sum of subsets of all the subsets of an array | O(2^N)
- Sum of subsets of all the subsets of an array | O(N)
- Divide array in two Subsets such that sum of square of sum of both subsets is maximum
- Maximise the number of toys that can be purchased with amount K
- Maximise the number of toys that can be purchased with amount K using min Heap
- Maximise the sum of two Numbers using at most one swap between them
- Maximize count of subsets having product of smallest element and size of the subset at least X
- Check if an array can be split into subsets of K consecutive elements
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Count minimum number of subsets (or subsequences) with consecutive numbers
- Minimum difference between max and min of all K-size subsets
- Print all subsets of given size of a set
- k size subsets with maximum difference d between max and min
- Product of all sorted subsets of size K using elements whose index divide K completely
- Subsets of size K with product equal to difference of two perfect squares
- Merge an array of size n into another array of size m+n

