- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Find maximum number of elements such that their absolute difference is less than or equal to 1 in C++

Suppose we have an array of n elements. We have to 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. So if the array is like [2, 2, 3, 4, 5], then the element will be 3, so the sequence with maximum count is 2, 2, 3.

The absolute difference of 0 and 1 means, that the number can be of type x and x + 1. So the idea is to store the frequencies of array elements. So if we find the maximum sum of any two consecutive elements, it will be the solution.

## Example

#include <iostream> #include <map> using namespace std; int maxElem(int arr[], int n) { map<int,int> occurrence; for(int i=0;i<n;++i){ if(occurrence[arr[i]]) occurrence[arr[i]] += 1; else occurrence[arr[i]] = 1; } int ans = 0, key; map<int,int>:: iterator it=occurrence.begin(); while(it!=occurrence.end()) { key = it->first; ++it; if(occurrence[key+1]!=0) ans=max(ans,occurrence[key]+occurrence[key+1]); } return ans; } int main(){ int arr[] = {2, 2, 3, 4, 5}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"Result is: " << maxElem(arr, n); }

## Output

Result is: 3

- Related Articles
- Find three integers less than or equal to N such that their LCM is maximum - C++
- Find three integers less than or equal to N such that their LCM is maximum in C++
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k in C++
- Find unique pairs such that each element is less than or equal to N in C++
- C++ program to find unique pairs such that each element is less than or equal to N
- Linear magnification produced by a concave mirror may be:(a) less than 1 or equal to 1 (b) more than 1 or equal to 1(c) less than 1, more than 1 or equal to 1 (d) less than 1 or more than 1
- Linear magnification produced by a convex lens can be:(a) less than 1 or more than 1.(b) less than 1 or equal to 1.(c) more than 1 or equal to 1.(d) less than 1, equal to 1 or more than 1.
- Find maximum sum array of length less than or equal to m in C++
- Number of elements less than or equal to a given number in a given subarray in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit in C++
- Find Largest Special Prime which is less than or equal to a given number in C++
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1?
- Magnification produced by a concave lens is always:(a) more than 1 (b) equal to 1 (c) less than 1 (d) more than 1 or less than 1
- The numerical ratio of displacement to distance for a moving object is$(a).$ always less than 1$(b).$ always equal to 1$(c).$ always more than 1$(d).$ equal or less than 1

Advertisements