📌  相关文章
📜  通过将数组元素增加 K 来最大化唯一数组元素的计数

📅  最后修改于: 2021-09-05 11:34:41             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] 任务是通过将任何数组元素仅增加一次K来找到可能的唯一元素的最大数量。

例子:

方法:解决这个问题的思路是将数组元素的出现频率存储在一个Map中,并相应地改变数组元素,在任意值递增K后得到数组中唯一的元素。请按照以下步骤解决问题:

  • 遍历数组并将每个数组元素的频率存储在 Map 中。
  • 遍历 Map 并执行以下步骤:
    • 如果当前元素的计数为1 ,则不要更改数组元素。
    • 如果当前元素的计数大于 1 ,则将当前元素增加K并在Map中将(current_element + K)的计数增加1
  • 完成上述步骤后,将 Map大小打印为数组中唯一元素的最大数量。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
void maxDifferent(int arr[], int N, int K)
{
    // Stores the count of element
    // in array
    map M;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Increase the counter of
        // the array element by 1
        M[arr[i]]++;
    }
 
    // Traverse the map
    for (auto it = M.begin();
         it != M.end(); it++) {
 
        // Extract the current element
        int current_element = it->first;
 
        // Number of times the current
        // element is present in array
        int count = it->second;
 
        // If element is present only
        // once, then do not change it
        if (count == 1)
            continue;
 
        // If the count > 1 then change
        // one of the same current
        // elements to (current_element + K)
        // and increase its count by 1
        M[current_element + K]++;
    }
 
    // The size of the map is the
    // required answer
    cout << M.size();
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 2, 4, 5, 5, 7, 4 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maxDifferent(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
static void maxDifferent(int arr[], int N, int K)
{
     
    // Stores the count of element
    // in array
    HashMap M = new HashMap();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Increase the counter of
        // the array element by 1
        Integer count = M.get(arr[i]);
         
        if (count == null)
        {
            M.put(arr[i], 1);
        }
        else
        {
            M.put(arr[i], count + 1);
        }
    }
 
    // Iterator itr = M.entrySet().iterator();
    Iterator> itr = M.entrySet().iterator();
 
    int[] ar1 = new int[N];
 
    // Traverse the map
    while (itr.hasNext())
    {
        Map.Entry Element = itr.next();
 
        // Extract the current element
        int current_element = (int)Element.getKey();
 
        // Number of times the current
        // element is present in array
        int count = (int)Element.getValue();
 
        // If element is present only
        // once, then do not change it
        if (count == 1)
            continue;
 
        // If the count > 1 then change
        // one of the same current
        // elements to (current_element + K)
        // and increase its count by 1
        ar1[current_element + K]++;
    }
 
    for(int i = 0; i < N; i++)
    {
        if (ar1[i] >= 0)
        {
            Integer count = M.get(ar1[i]);
             
              if (count == null)
              {
                  M.put(ar1[i], 1);
            }
            else
            {
                M.put(ar1[i], count + 1);
            }
        }
    }
 
    // The size of the map is the
    // required answer
    System.out.println(M.size());
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 3, 2, 4, 5, 5, 7, 4 };
    int K = 2;
    int N = arr.length;
     
    // Function Call
    maxDifferent(arr, N, K);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python3 program for the above approach
 
# Function to find the maximum unique
# elements in array after incrementing
# any element by K
def maxDifferent(arr, N, K):
     
    # Stores the count of element
    # in array
    M = {}
 
    # Traverse the array
    for i in range(N):
 
        # Increase the counter of
        # the array element by 1
        M[arr[i]] = M.get(arr[i], 0) + 1
 
    # Traverse the map
    for it in list(M.keys()):
 
        # Extract the current element
        current_element = it
 
        # Number of times the current
        # element is present in array
        count = M[it]
 
        # If element is present only
        # once, then do not change it
        if (count == 1):
            continue
 
        # If the count > 1 then change
        # one of the same current
        # elements to (current_element + K)
        # and increase its count by 1
        M[current_element + K] = M.get(current_element, 0) + 1
 
    # The size of the map is the
    # required answer
    print(len(M))
 
# Driver Code
if __name__ == '__main__':
    arr=[2, 3, 2, 4, 5, 5, 7, 4]
    K = 2
    N = len(arr)
 
    # Function Call
    maxDifferent(arr, N, K)
 
    # This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find the maximum unique
// elements in array after incrementing
// any element by K
static void maxDifferent(int []arr, int N, int K)
{
     
    // Stores the count of element
    // in array
    Dictionary M = new Dictionary();
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Increase the counter of
        // the array element by 1
        int count = M.ContainsKey(arr[i]) ? M[arr[i]] : 0;
         
        if (count == 0)
        {
            M.Add(arr[i], 1);
        }
        else
        {
            M[arr[i]] = count + 1;
        }
    }
 
    int[] ar1 = new int[N];
 
    // Traverse the map
    foreach(KeyValuePair Element in M)
    {
 
        // Extract the current element
        int current_element = (int)Element.Key;
 
        // Number of times the current
        // element is present in array
        int count = (int)Element.Value;
 
        // If element is present only
        // once, then do not change it
        if (count == 1)
            continue;
 
        // If the count > 1 then change
        // one of the same current
        // elements to (current_element + K)
        // and increase its count by 1
        ar1[current_element + K]++;
    }
 
    for(int i = 0; i < N; i++)
    {
        if (ar1[i] >= 0)
        {
            int count = M.ContainsKey(ar1[i]) ? M[ar1[i]] : 0;           
              if (count == 0)
              {
                  M.Add(ar1[i], 1);
            }
            else
            {
                M[ar1[i]] = count + 1;
            }
        }
    }
 
    // The size of the map is the
    // required answer
    Console.WriteLine(M.Count);
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 2, 3, 2, 4, 5, 5, 7, 4 };
    int K = 2;
    int N = arr.Length;
     
    // Function Call
    maxDifferent(arr, N, K);
}
}
 
// This code contributed by shikhasingrajput


Javascript


输出:
7

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live