Largest subset with maximum difference as 1
Last Updated :
11 Oct, 2022
Given an array arr[] of n positive integers. The task is to find the size of the subset formed from the elements of the given array and the absolute difference between any two elements of the set is less than equal to 1.
Examples :
Input : arr[] = {8, 9, 8, 7, 8, 9, 10, 11}
Output : 5
If we make subset with elements {8, 9, 8, 8, 9}.
Each pair in the subset has an absolute
difference <= 1
Input : arr[] = {4, 5, 2, 4, 4, 4}
Output : 5
Subset is {4, 5, 4, 4, 4}
Observe, since we want the absolute difference between any two elements to be less than equal to 1, then there can be a maximum of two distinct numbers. So, the subset we choose will be in the form {a, a, a, ….., b, b, b} or {a, a, a, a, …..}.
Now, to find the size of such subset we will find the frequency of each element say c1, c2, c3, …., cj, …., cmaximum element in arr. Then our answer will be the maximal value of ci + ci+1.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxsizeSubset( int arr[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[arr[i]]++;
int res = 0;
for ( auto x : mp)
if (mp.find(x.first + 1) != mp.end())
{
res = max(res, mp[x.first] + mp[x.first+1]);
}
else
{
res=max(res,mp[x.first]);
}
return res;
}
int main()
{
int arr[] = {1, 2, 2, 3, 1, 2};
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxsizeSubset(arr, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxsizeSubset( int arr[], int n)
{
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(arr[i]))
{
mp.put(arr[i], mp.get(arr[i]) + 1 );
}
else
{
mp.put(arr[i], 1 );
}
}
int res = 0 ;
for (Map.Entry<Integer, Integer> x : mp.entrySet())
{
if (mp.containsKey(x.getKey() + 1 ))
{
res = Math.max(res, mp.get(x.getKey()) + mp.get(x.getKey() + 1 ));
}
else
{
res = Math.max(res, mp.get(x.getKey()));
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 , 3 , 1 , 2 };
int n = arr.length;
System.out.println(maxsizeSubset(arr, n));
}
}
|
Python3
def maxsizeSubset(arr, n):
mp = {}
for i in range (n):
if (arr[i] in mp):
mp[arr[i]] = mp[arr[i]] + 1
else :
mp[arr[i]] = 1
res = 0
for x,y in mp.items():
if ((x + 1 ) in mp):
res = max (res, mp[x] + mp[x + 1 ])
else :
res = max (res, mp[x])
return res
arr = [ 1 , 2 , 2 , 3 , 1 , 2 ]
n = len (arr)
print (maxsizeSubset(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxsizeSubset( int []arr, int n)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
var val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else
{
mp.Add(arr[i], 1);
}
}
int res = 0;
foreach (KeyValuePair< int , int > x in mp)
{
if (mp.ContainsKey(x.Key + 1))
{
res = Math.Max(res, mp[x.Key] + mp[x.Key + 1]);
}
else
{
res = Math.Max(res, mp[x.Key]);
}
}
return res;
}
public static void Main(String[] args)
{
int []arr = {1, 2, 2, 3, 1, 2};
int n = arr.Length;
Console.WriteLine(maxsizeSubset(arr, n));
}
}
|
Javascript
<script>
function maxsizeSubset(arr, n) {
let mp = new Map();
for (let i = 0; i < n; i++) {
if (mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1)
} else {
mp.set(arr[i], 1)
}
}
let res = 0;
for (let x of mp)
if (mp.has(x[0] + 1)) {
res = Math.max(res, mp.get(x[0]) + mp.get(x[0] + 1));
}
else {
res = Math.max(res, mp.get(x[0]));
}
return res;
}
let arr = [1, 2, 2, 3, 1, 2];
let n = arr.length;
document.write(maxsizeSubset(arr, n) + "<br>" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space : O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...