Find maximum number of elements such that their absolute difference is less than or equal to 1
Last Updated :
06 Sep, 2022
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 2
Input : 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++
#include <bits/stdc++.h>
using namespace std;
int maxCount( int n, int a[])
{
map< int , int > freq;
for ( int i=0;i<n;++i){
if (freq[a[i]])
freq[a[i]] += 1;
else
freq[a[i]] = 1;
}
int ans = 0, key;
map< int , int >:: iterator it=freq.begin();
while (it!=freq.end())
{
key = it->first;
++it;
if (freq[key+1]!=0)
ans=max(ans,freq[key]+freq[key+1]);
}
return ans;
}
int main(){
int n = 5;
int arr[] = {2, 2, 3, 4, 5};
cout<<maxCount(n,arr);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.lang.Math;
class GfG
{
static int maxCount( int n, int a[])
{
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 );
}
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;
}
public static void main(String []args)
{
int n = 5 ;
int arr[] = { 2 , 2 , 3 , 4 , 5 };
System.out.println(maxCount(n,arr));
}
}
|
Python3
def maxCount(a):
freq = {}
for i in range (n):
if (a[i] in freq):
freq[a[i]] + = 1
else :
freq[a[i]] = 1
ans = 0
for key, value in freq.items():
if (key + 1 in freq) :
ans = max (ans, freq[key] + freq[key + 1 ])
return ans
n = 5
arr = [ 2 , 2 , 3 , 4 , 5 ]
print (maxCount(arr))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int maxCount( int n, int []a)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
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);
}
}
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;
}
public static void Main(String []args)
{
int n = 5;
int []arr = {2, 2, 3, 4, 5};
Console.WriteLine(maxCount(n,arr));
}
}
|
Javascript
<script>
function maxCount(n,a)
{
var freq = new Map();
for ( var i=0;i<n;++i){
if (freq.has(a[i]))
freq.set(a[i], freq.get(a[i])+1)
else
freq.set(a[i], 1)
}
var ans = 0, key;
freq.forEach((value, key) => {
if (freq.has(key+1))
ans=Math.max(ans,freq.get(key)+freq.get(key+1));
});
return ans;
}
var n = 5;
var arr = [2, 2, 3, 4, 5];
document.write( maxCount(n,arr));
</script>
|
Complexity Analysis:
- Time Complexity: O(n * log(n))
- Auxiliary Space: O(n)
Share your thoughts in the comments
Please Login to comment...