Maximum possible size of the group so that each pair differ by at most 5
Last Updated :
17 Apr, 2023
Given an array arr[] of size n, the task is to find the maximum possible size x of a group such that the difference between any pair formed in the group differ by at most 5.
Examples:
Input: arr[] = {1, 10, 17, 12, 15, 2}
Output: 3
Explanation: You can create a group with [12, 17, 15]. [10, 12, 15] is also the valid group. Hence, the maximum possible size is 3
Input: arr[] = {13, 13, 13, 13, 13, 13, 13, 13, 13, 13}
Output: 10
Explanation: The difference between each pair of elements is 0 always. You can make the group with all the elements. Hence, the maximum possible size is 10
Approach: Implement the idea below to solve the problem:
This problem can be solved using binary search. In order to have the maximum possible size such that no pair differ more than 5, We will sort out the array first and start iterating and calculate the range upto which the each pair has a difference less than or equal to 5. In order to calculate the range we can use the upper bound. Also, comparing the previous maximum possible size with the current size in each iteration.
Follow the steps mentioned below to implement the idea:
- Sort the array.
- Initialized a variable maxi to 1 that stores the maximum possible size
- Start iterating the array from starting
- Calculate the maximum possible range up to which the difference between each pair is less than or equal to 5.
- We will be using upper_bound(arr.begin(), arr.end(), arr[i] + 5) – arr.begin() in order to calculate the maximum possible range.
- The current size will be calculated as idx – current index.
- Store the maximum possible size in each iteration by comparing the current size with the previous maximum size.
- Return maxi.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximum_possible(vector< int >& arr, int n)
{
sort(arr.begin(), arr.end());
int maxi = 1;
for ( int i = 0; i < n; i++) {
int idx = upper_bound(arr.begin(), arr.end(),
arr[i] + 5)
- arr.begin();
maxi = max(maxi, idx - i);
}
return maxi;
}
int main()
{
vector< int > arr = { 1, 10, 17, 12, 15, 2 };
int n = arr.size();
cout << maximum_possible(arr, n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int maximum_possible(List<Integer> arr,
int n)
{
Collections.sort(arr);
int maxi = 1 ;
for ( int i = 0 ; i < n; i++) {
int idx = Collections.binarySearch(
arr, arr.get(i) + 5 );
if (idx < 0 ) {
idx = -(idx + 1 ) - 1 ;
}
maxi = Math.max(maxi, idx - i + 1 );
}
return maxi;
}
public static void main(String[] args)
{
List<Integer> arr
= Arrays.asList( 1 , 10 , 17 , 12 , 15 , 2 );
int n = arr.size();
System.out.println(maximum_possible(arr, n));
}
}
|
Python3
import bisect
def maximum_possible(arr, n):
arr.sort()
maxi = 1
for i in range (n):
idx = bisect.bisect_right(arr, arr[i] + 5 )
if idx = = 0 :
idx = 1
else :
idx - = 1
maxi = max (maxi, idx - i + 1 )
return maxi
arr = [ 1 , 10 , 17 , 12 , 15 , 2 ]
n = len (arr)
print (maximum_possible(arr, n))
|
C#
using System;
using System.Linq;
public class Program {
public static int MaximumPossible( int [] arr, int n)
{
Array.Sort(arr);
int maxi = 1;
for ( int i = 0; i < n; i++) {
int idx = Array.BinarySearch(
arr, i + 1, n - i - 1, arr[i] + 5);
if (idx < 0) {
idx = ~idx - 1;
}
maxi = Math.Max(maxi, idx - i + 1);
}
return maxi;
}
public static void Main()
{
int [] arr = new int [] { 1, 10, 17, 12, 15, 2 };
int n = arr.Length;
Console.WriteLine(MaximumPossible(arr, n));
}
}
|
Javascript
<script>
function maximum_possible(arr, n) {
arr.sort((a, b) => a - b);
function upper_bound(arr, key) {
let left = 0;
let right = arr.length;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] <= key) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
let maxi = 1;
for (let i = 0; i < n; i++) {
let idx = upper_bound(arr, arr[i] + 5);
maxi = Math.max(maxi, idx - i);
}
return maxi;
}
let arr = [1, 10, 17, 12, 15, 2];
let n = arr.length;
console.log(maximum_possible(arr, n));
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Alternative Approach:
Implement the idea below to solve the problem:
Use a sliding window to maintain a subarray where the difference between any two elements is less than or equal to 5. Start with a subarray consisting of only the first element, and then expand the window by incrementing j until the difference between arr[j] and arr[i] is less than or equal to 5. Once the window is expanded as much as possible, calculate its size and update the maxLength accordingly. Then increment i until the difference between arr[j] and arr[i] is greater than 5, which means need to contract the window. Continue this process until j reaches the end of the array.
Follow the steps mentioned below to implement the idea:
- Sort the array.
- Initialize two pointers i and j, both pointing to the first element in the array.
- Initialize a variable ‘maxLength’ to 1 to keep track of the maximum possible size.
- Iterate over the array using pointers i and j.
- Increment j until the difference between arr[j] and arr[i] is less than or equal to 5.
- Calculate the current size of the subarray, i.e., j – i + 1.
- If the current size is greater than the previous maximum size, update the maxLength to the current size.
- Increment i until the difference between arr[j] and arr[i] is greater than 5.
- Repeat steps 5-8 until j reaches the end of the array.
- Return maxLength.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {1, 10, 17, 12, 15, 2};
int n = sizeof (arr) / sizeof (arr[0]);
sort(arr, arr + n);
int i = 0, j = 0;
int maxLength = 1;
while (j < n) {
if (arr[j] - arr[i] <= 5) {
maxLength = max(maxLength, j - i + 1);
j++;
} else {
i++;
}
}
cout << maxLength << endl;
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
public static void main(String[] args) {
int [] arr = { 1 , 10 , 17 , 12 , 15 , 2 };
int n = arr.length;
Arrays.sort(arr);
int i = 0 , j = 0 ;
int maxLength = 1 ;
while (j < n) {
if (arr[j] - arr[i] <= 5 ) {
maxLength = Math.max(maxLength, j - i + 1 );
j++;
}
else {
i++;
}
}
System.out.println(maxLength);
}
}
|
Python3
if __name__ = = '__main__' :
arr = [ 1 , 10 , 17 , 12 , 15 , 2 ]
n = len (arr)
arr.sort()
i = 0
j = 0
maxLength = 1
while j < n:
if arr[j] - arr[i] < = 5 :
maxLength = max (maxLength, j - i + 1 )
j + = 1
else :
i + = 1
print (maxLength)
|
C#
using System;
using System.Linq;
class GFG {
static public void Main()
{
int [] arr = { 1, 10, 17, 12, 15, 2 };
int n = arr.Length;
Array.Sort(arr);
int i = 0, j = 0;
int maxLength = 1;
while (j < n) {
if (arr[j] - arr[i] <= 5) {
maxLength = Math.Max(maxLength, j - i + 1);
j++;
}
else {
i++;
}
}
Console.WriteLine(maxLength);
}
}
|
Javascript
const arr = [1, 10, 17, 12, 15, 2];
const n = arr.length;
arr.sort((a, b) => a - b);
let i = 0, j = 0;
let maxLength = 1;
while (j < n) {
if (arr[j] - arr[i] <= 5) {
maxLength = Math.max(maxLength, j - i + 1);
j++;
} else {
i++;
}
}
console.log(maxLength);
|
Time Complexity: O(nlog(n)).
Auxiliary Space: O(1).
Share your thoughts in the comments
Please Login to comment...