给定一个包含N 个元素的数组arr[] ,任务是通过对数组的每个元素的给定操作之一来最大化数组中不同元素的计数:
- 要么将元素增加 1
- 或将元素减少 1
- 或保持元素原样。
注意:没有元素可以小于或等于 0。
例子:
Input: arr = [4, 4, 5, 5, 5, 5, 6, 6]
Output: 5
Explanation: After modification of each element of the array in any of the three possible ways, arr[] = [3, 4, 5, 5, 5, 5, 6, 7]. Here distinct elements are 5.
Input: arr = [1, 1, 1, 8, 8, 8, 9, 9]
Output: 6
Explanation: After modification of each element of the array in any of the three possible ways, arr[] = [1, 1, 2, 7, 8, 8, 9, 10]. Here distinct elements are 6.
方法:这个想法是先对给定的数组进行排序,这样可以通过与相邻元素进行比较来轻松检查元素是否不同。
- 首先,对数组的所有元素进行排序。
- 将变量count和prev初始化为 0。(分别存储不同元素和前一个元素的计数。)
- 之后,使用prev变量跟踪前一个元素。
- 迭代已排序的数组。
- 将当前元素的值减 1,并检查前一个元素是否小于减小的值。如果它较小,则增加计数并将当前值分配给prev 。
- 如果当前元素的减少值不大于前一个元素,则保持当前元素不变并检查前一个元素是否小于当前元素。如果它较小,则增加计数并将当前值分配给prev 。
- 如果当前值不大于前一个元素,则将当前值增加 1 并检查前一个元素是否小于增加的当前元素。如果它较小,则增加计数并将当前值分配给prev 。
- 如果当前元素的增量值不小于前一个值,则跳过该元素。
下面是上述方法的实现:
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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live