Given an integer array and an integer k. The array elements denote positions of points on a 1-D number line, find the maximum size of the 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 the 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 .
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.
Implementation:
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 */ |
Python3
# Python3 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 ?> |
Javascript
<script> // Javascript 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 let ans = k; for (let l = 0; l < n - 1; l++) for (let 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 let arr = [ 1, 2, 3, 4, 10, 11, 14, 15 ]; let n = arr.length; let k = 4; document.write(maximiseSubset(arr, n, k)); // This code is contributed by avanitrachhadiya2155 </script> |
8n
Time complexity: O(N2).
Auxiliary Space: O(1)
Efficient Approach:
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. Initialize 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 the brute force approach becomes true. When r reaches the last index, we stop.
Implementation:
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 ?> |
Javascript
<script> /* Javascript 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 let ans = k; let 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 let arr=[1, 2, 3, 4, 10, 11, 14, 15 ]; let n = arr.length; let k = 4; document.write(maximiseSubset(arr, n, k)); // This code is contributed by rag2127 </script> |
8
Time Complexity: O(N)
Auxiliary Space: O(1)
If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.uk or mail your article to review-team@neveropen.co.uk. See your article appearing on the neveropen main page and help other Geeks.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!