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 maximal value of ci + ci+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; } |
Java
// Java Program to find the size of // the subset formed from the elements // of the given array such that the // maximum difference is 1 import java.util.*; class GFG { // Return the maximum size of subset with // absolute difference between any element // is less than 1. static int maxsizeSubset(int arr[], int n) { // Inserting elements and their // frequencies in a hash table. 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); } } // 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 (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; } // Driver code public static void main(String[] args) { int arr[] = {1, 2, 2, 3, 1, 2}; int n = arr.length; System.out.println(maxsizeSubset(arr, n)); } } /* This code is contributed by PrinciRaj1992 */ |
C#
// C# Program to find the size of // the subset formed from the elements // of the given array such that the // maximum difference is 1 using System; using System.Collections.Generic; class GFG { // Return the maximum size of subset with // absolute difference between any element // is less than 1. static int maxsizeSubset(int []arr, int n) { // Inserting elements and their // frequencies in a hash table. 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); } } // 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; 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; } // Driver code public static void Main(String[] args) { int []arr = {1, 2, 2, 3, 1, 2}; int n = arr.Length; Console.WriteLine(maxsizeSubset(arr, n)); } } // This code is contributed by 29AjayKumar |
5
Time Complexity : O(n)
Auxiliary Space : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Find the size of largest subset of anagram words
- Largest subset with M as smallest missing number
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Largest increasing subsequence of consecutive integers
- Largest rectangular sub-matrix having sum divisible by k
- Find largest d in array such that a + b + c = d
- Largest element in the array that is repeated exactly k times
- Lexicographically largest string formed from the characters in range L and R
- Find the k largest numbers after deleting the given elements
- Python | Element with largest frequency in list
- Largest even number that can be formed by any number of swaps
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Largest sub-string where all the characters appear at least K times
- Area of the largest square that can be formed from the given length sticks using Hashing
- Largest index for each distinct character in given string with frequency K
- Largest number in the Array having frequency same as value
- Largest subarray with frequency of all elements same
- Largest substring where all characters appear at least K times | Set 2
- Largest number made up of X and Y with count of X divisible by Y and of Y by X
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.

