Maximise the size of consecutive element subsets in an array
Last Updated :
17 Oct, 2022
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>
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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands
SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read