# Largest subset with maximum difference as 1

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 c_{1}, c_{2}, c_{3}, …., c_{j}, …., c_{maximum element in arr}. Then our answer will be maximal value of c_{i} + c_{i+1}.

Below is the implementation of this approach:

## C++

`// CPP Program to find the size of ` `// the subset formed from the elements ` `// of the given array such that the ` `// maximum difference is 1 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the maximum size of subset with ` `// absolute difference between any element ` `// is less than 1. ` `int` `maxsizeSubset(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// Inserting elements and their ` ` ` `// frequencies in a hash table. ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[arr[i]]++; ` ` ` ` ` `// Traverse through map, for every element ` ` ` `// x in map, find if x+1 also exists in map. ` ` ` `// If exists, see if sum of their frequencies ` ` ` `// is more than current result. ` ` ` `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; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `arr[] = {1, 2, 2, 3, 1, 2}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << maxsizeSubset(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5

Time Complexity : O(n)

Auxiliary Space : O(n)

## Recommended Posts:

- Find the sum of maximum difference possible from all subset of a given array.
- Largest subset with sum of every pair as prime
- Largest divisible pairs subset
- Largest divisible subset in array
- Largest subset whose all elements are Fibonacci numbers
- Find the size of largest subset of anagram words
- Find the maximum subset XOR of a given set
- Maximum size subset with given sum
- Maximum product subset of an array
- Maximum subset sum such that no two elements in set have same digit in them
- Size of the smallest subset with maximum Bitwise OR
- Difference between the largest and the smallest primes in an array
- Maximum Weight Difference
- Minimize the maximum difference between the heights
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.