📌  相关文章
📜  找到最大数量的元素,使得它们的绝对差小于或等于 1

📅  最后修改于: 2021-10-27 16:45:57             🧑  作者: Mango

给定一个包含 n 个元素的数组,找出要从该数组中选择的最大元素数,使得任意两个所选元素之间的绝对差小于或等于 1。
例子

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.

0 或 1 的绝对差异意味着所选数字可以是 x 和 x+1 类型。因此,想法是存储数组元素的频率。因此,任务现在减少到找到任何两个连续元素的最大和。
下面是上述方法的实现:

C++
// CPP program to find maximum number of
// elements such that their absolute
// difference is less than or equal to 1
#include 
using namespace std;
 
// function to return maximum number of elements
int maxCount(int n,int a[])
{
    // Counting frequencies of elements
    map freq;
 
    for(int i=0;i:: 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<


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 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


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))


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 mp = new Dictionary();
         
        // 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 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 */


Javascript


输出:
3

时间复杂度: O(n * log(n))

辅助空间: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程