Given an array of n elements, find the maximum number of elements to select from the array such that the absolute difference between any two of the chosen elements is less than or equal to 1.

**Examples**:

Input: arr[] = {1, 2, 3}Output: 2 We can either take 1, 2 or 2, 3. Both will have the count 2 so maximum count is 2Input: arr[] = {2, 2, 3, 4, 5}Output: 3 The sequence with maximum count is 2, 2, 3.

The absolute difference of 0 or 1 means that the numbers chosen can be of type x and x+1. Therefore the idea is to store frequencies of array elements. So, the task now reduces to find the maximum sum of any two consecutive elements.

Below is the implementation of the above approach:

## C++

`// CPP program to find maximum number of ` `// elements such that their absolute ` `// difference is less than or equal to 1 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return maximum number of elements ` `int` `maxCount(` `int` `n,` `int` `a[]) ` `{ ` ` ` `// Counting frequencies of elements ` ` ` `map<` `int` `,` `int` `> freq; ` ` ` ` ` `for` `(` `int` `i=0;i<n;++i){ ` ` ` `if` `(freq[a[i]]) ` ` ` `freq[a[i]] += 1; ` ` ` `else` ` ` `freq[a[i]] = 1; ` ` ` `} ` ` ` ` ` `// Finding max sum of adjacent indices ` ` ` `int` `ans = 0, key; ` ` ` ` ` `map<` `int` `,` `int` `>:: iterator it=freq.begin(); ` ` ` ` ` `while` `(it!=freq.end()) ` ` ` `{ ` ` ` `key = it->first; ` ` ` ` ` `// increment the iterator ` ` ` `++it; ` ` ` ` ` `if` `(freq[key+1]!=0) ` ` ` `ans=max(ans,freq[key]+freq[key+1]); ` ` ` ` ` `} ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main(){ ` ` ` `int` `n = 5; ` ` ` `int` `arr[] = {2, 2, 3, 4, 5}; ` ` ` ` ` `// function call to print required answer ` ` ` `cout<<maxCount(n,arr); ` ` ` ` ` `return` `0; ` `} ` ` ` ` ` `// This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the maximum number ` `// of elements such that their absolute ` `// difference is less than or equal to 1 ` `import` `java.util.HashMap; ` `import` `java.util.Map; ` `import` `java.lang.Math; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// function to return the maximum number of elements ` ` ` `static` `int` `maxCount(` `int` `n,` `int` `a[]) ` ` ` `{ ` ` ` `// Counting frequencies of elements ` ` ` `HashMap<Integer, Integer> freq = ` `new` `HashMap<>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) ` ` ` `{ ` ` ` `if` `(freq.containsKey(a[i])) ` ` ` `freq.put(a[i], freq.get(a[i]) + ` `1` `); ` ` ` `else` ` ` `freq.put(a[i], ` `1` `); ` ` ` `} ` ` ` ` ` `// Finding max sum of adjacent indices ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `for` `(Integer key : freq.keySet()) ` ` ` `{ ` ` ` `if` `(freq.containsKey(key+` `1` `)) ` ` ` `ans = Math.max(ans, freq.get(key) + freq.get(key+` `1` `)); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` ` ` `int` `n = ` `5` `; ` ` ` `int` `arr[] = {` `2` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `}; ` ` ` ` ` `// function call to print required answer ` ` ` `System.out.println(maxCount(n,arr)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find maximum number of ` `# elements such that their absolute ` `# difference is less than or equal to 1 ` ` ` `def` `maxCount(a): ` ` ` ` ` `# Counting frequencies of elements ` ` ` `freq ` `=` `{} ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(a[i] ` `in` `freq): ` ` ` `freq[a[i]] ` `+` `=` `1` ` ` `else` `: ` ` ` `freq[a[i]] ` `=` `1` ` ` ` ` ` ` `# Finding max sum of adjacent indices ` ` ` `ans ` `=` `0` ` ` `for` `key, value ` `in` `freq.items(): ` ` ` `if` `(key` `+` `1` `in` `freq) : ` ` ` `ans ` `=` `max` `(ans, freq[key] ` `+` `freq[key ` `+` `1` `]) ` ` ` ` ` `return` `ans ` ` ` `# Driver Code ` `n ` `=` `5` `arr ` `=` `[` `2` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `] ` ` ` `print` `(maxCount(arr)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the maximum number ` `// of elements such that their absolute ` `// difference is less than or equal to 1 ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GfG ` `{ ` ` ` ` ` `// function to return the maximum number of elements ` ` ` `static` `int` `maxCount(` `int` `n,` `int` `[]a) ` ` ` `{ ` ` ` `// Counting frequencies of elements ` ` ` `Dictionary<` `int` `,` `int` `> mp = ` `new` `Dictionary<` `int` `,` `int` `>(); ` ` ` ` ` `// Increase the frequency of elements ` ` ` `for` `(` `int` `i = 0 ; i < n; i++) ` ` ` `{ ` ` ` `if` `(mp.ContainsKey(a[i])) ` ` ` `{ ` ` ` `var` `val = mp[a[i]]; ` ` ` `mp.Remove(a[i]); ` ` ` `mp.Add(a[i], val + 1); ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `mp.Add(a[i], 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Finding max sum of adjacent indices ` ` ` `int` `ans = 0; ` ` ` ` ` `foreach` `(KeyValuePair<` `int` `, ` `int` `> e ` `in` `mp) ` ` ` `{ ` ` ` `if` `(mp.ContainsKey(e.Key+1)) ` ` ` `ans = Math.Max(ans, mp[e.Key] + mp[e.Key+1]); ` ` ` `} ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` ` ` `int` `n = 5; ` ` ` `int` `[]arr = {2, 2, 3, 4, 5}; ` ` ` ` ` `// function call to print required answer ` ` ` `Console.WriteLine(maxCount(n,arr)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

3

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:

- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Find all matrix elements which are minimum in their row and maximum in their column
- Triplets in array with absolute difference less than k
- Number of elements less than or equal to a number in a subarray : MO's Algorithm
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Find maximum sum array of length less than or equal to m
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Sum of all array elements less than X and greater than Y for Q queries
- Find all powers of 2 less than or equal to a given number

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.