📌  相关文章
📜  通过增加/减少元素或使其保持不变来最大化不同的元素

📅  最后修改于: 2021-05-17 20:24:35             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是通过对数组的每个元素进行以下任一给定操作来最大化数组中不同元素的数量:

  • 将元素增加1
  • 或将元素减少1
  • 或保持原样。

注意:任何元素都不能小于或等于0。

例子:

方法:想法是首先对给定的数组进行排序,以便可以通过与相邻元素进行比较轻松地检查元素(如果有区别的话)。

  1. 首先,对数组的所有元素进行排序。
  2. 将变量countprev初始化为0。(分别存储不同元素和前一个元素的计数。)
  3. 之后,使用prev变量跟踪前一个元素。
  4. 迭代排序后的数组。
  5. 将当前元素的值减小1,然后检查前一个元素是否小于减小的值。如果较小,则增加计数并将当前值分配给prev
  6. 如果当前元素的减小值不大于前一个元素,则保持当前元素不变,并检查前一个元素是否小于当前元素。如果较小,则增加计数并将当前值分配给prev
  7. 如果当前值不大于前一个元素,则将当前值增加1并检查前一个元素是否小于递增的当前元素。如果较小,则增加计数并将当前值分配给prev
  8. 如果当前元素的增量值不小于先前值,则跳过该元素。

下面是上述方法的实现:

C++
// C++ program to Maximize distinct
// elements by incrementing/decrementing
// an element or keeping it same
  
#include 
using namespace std;
  
// Function that Maximize
// the count of distinct
// element
int max_dist_ele(int arr[],
                 int n)
{
  
    // sort thr array
    sort(arr, arr + n);
  
    int ans = 0;
  
    // keeping track of
    // previous change
    int prev = 0;
  
    for (int i = 0;
         i < n; i++) {
  
        // check the
        // decremented value
        if (prev < (arr[i] - 1)) {
  
            ans++;
            prev = arr[i] - 1;
        }
  
        // check the current
        // value
        else if (prev < (arr[i])) {
  
            ans++;
            prev = arr[i];
        }
  
        // check the
        // incremented value
        else if (prev < (arr[i] + 1)) {
  
            ans++;
            prev = arr[i] + 1;
        }
    }
    return ans;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 1, 1, 8,
                  8, 8, 9, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << max_dist_ele(arr, n)
         << endl;
    return 0;
}


Java
// Java program to Maximize
// the count of distinct element
  
import java.util.*;
  
public class GFG {
  
    // Function that Maximize
    // the count of distinct element
    static int max_dist_ele(
        int arr[], int n)
    {
        // sort thr array
        Arrays.sort(arr);
  
        int ans = 0;
  
        // keeping track of
        // previous change
        int prev = 0;
  
        for (int i = 0;
             i < n; i++) {
  
            // decrement is possible
            if (prev < (arr[i] - 1)) {
  
                ans++;
                prev = arr[i] - 1;
            }
  
            // remain as it is
            else if (prev < (arr[i])) {
  
                ans++;
                prev = arr[i];
            }
            // increment is possible
            else if (prev < (arr[i] + 1)) {
                ans++;
                prev = arr[i] + 1;
            }
        }
  
        return ans;
    }
  
    // Driver Code
    public static void main(String args[])
    {
        int arr[] = { 1, 1, 1, 8,
                      8, 8, 9, 9 };
        int n = arr.length;
  
        System.out.println(max_dist_ele(arr, n));
    }
}


Python3
# Python3 program to Maximize 
# the count of distinct element
def max_dist_ele(arr, n):
      
    # Sort thr list
    arr.sort()
      
    ans = 0
      
    # Keeping track of 
    # previous change
    prev = 0
      
    for i in range(n):
          
        # Decrement is possible
        if prev < (arr[i] - 1):
            ans += 1;
            prev = arr[i] - 1
              
        # Remain as it is
        elif prev < (arr[i]):
            ans += 1
            prev = arr[i]
              
        # Increment is possible
        elif prev < (arr[i] + 1):
            ans += 1
            prev = arr[i] + 1
      
    return ans
  
# Driver Code
arr = [ 1, 1, 1, 8, 8, 8, 9, 9 ]
n = len(arr)
  
print(max_dist_ele(arr, n))
  
# This code is contributed by rutvik_56


C#
// C# program to maximize the 
// count of distinct element
using System;
  
class GFG{
  
// Function that maximize the 
// count of distinct element
static int max_dist_ele(int []arr, int n)
{
      
    // Sort thr array
    Array.Sort(arr);
  
    int ans = 0;
  
    // Keeping track of
    // previous change
    int prev = 0;
  
    for(int i = 0; i < n; i++)
    {
         
       // Decrement is possible
       if (prev < (arr[i] - 1))
       {
           ans++;
           prev = arr[i] - 1;
       }
         
       // Remain as it is
       else if (prev < (arr[i]))
       {
           ans++;
           prev = arr[i];
       }
         
       // Increment is possible
       else if (prev < (arr[i] + 1))
       {
           ans++;
           prev = arr[i] + 1;
       }
    }
    return ans;
}
  
// Driver Code
public static void Main(String []args)
{
    int []arr = { 1, 1, 1, 8,
                  8, 8, 9, 9 };
    int n = arr.Length;
  
    Console.WriteLine(max_dist_ele(arr, n));
}
}
  
// This code is contributed by Amit Katiyar


输出:
6


时间复杂度: O(N * logN)