📌  相关文章
📜  执行给定操作后数组中唯一值的最大数量

📅  最后修改于: 2021-05-17 21:29:46             🧑  作者: Mango

给定的尺寸为N []在阵列ARR阵列ARR和每个元素[]是在范围[1,N]和该阵列可以包含重复。任务是找到可以获取的唯一值的最大数量,以使任意索引i上的值可以是:

  • 增加1。
  • 减少1。
  • 保持原样。

注意:每个索引只能对该操作执行一次,并且必须对数组arr []中的所有索引执行一次。

例子:

方法:

  • 对于存在于索引i处的数组中的任意元素X ,我们通过考虑以下事项来决定对其执行哪些操作:
    1. 如果数组中不存在值( X – 1 )并且数组中存在一个或多个其他X且索引不同,则将值X1。
    2. 如果值X在数组中仅出现一次,则我们不会更改X。
    3. 我们递增1的值X,如果值(X + 1)不存在于阵列中的和存在于不同的索引的阵列中的一个或多个其它的X的存在。
  • 通过对每个元素做出上述决定,我们可以确保获得的唯一元素的最终计数是最大的。
  • 但是,要对每个索引执行上述步骤并计算元素X的出现并连续更新数组arr [],花费的时间将是二次的,这对于大型数组是不可行的。
  • 减少时间复杂度的一种替代方法是首先对数组进行排序。通过排序,将数组中的所有元素分组,并且所有重复的值合并在一起。
  • 在对数组进行排序之后,由于已经给出了数字范围并且它是固定的,因此可以使用哈希图,其中哈希键是[1,N]范围内的数字,并且每个键的值都是布尔值用于确定键是否存在于数组中。
  • 在这个问题中,由于索引本身是哈希的键,因此使用大小为( N + 2 )的数组freq []来实现哈希。

下面是上述方法的实现:

C++
// C++ program to find the maximum number of
// unique values in the array
  
#include 
using namespace std;
  
// Function to find the maximum number of
// unique values in the array
int uniqueNumbers(int arr[], int n)
{
    // Sorting the given array
    sort(arr, arr + n);
  
    // This array will store the frequency
    // of each number in the array
    // after performing the given operation
    int freq[n + 2];
  
    // Initialising the array with all zeroes
    memset(freq, 0, sizeof(freq));
  
    // Loop to apply operation on
    // each element of the array
    for (int x = 0; x < n; x++) {
  
        // Incrementing the value at index x
        // if the value  arr[x] - 1 is
        // not present in the array
        if (freq[arr[x] - 1] == 0) {
            freq[arr[x] - 1]++;
        }
  
        // If arr[x] itself is not present, then it
        // is left as it is
        else if (freq[arr[x]] == 0) {
            freq[arr[x]]++;
        }
  
        // If both arr[x] - 1 and arr[x] are present
        // then the value is incremented by 1
        else {
            freq[arr[x] + 1]++;
        }
    }
  
    // Variable to store the number of unique values
    int unique = 0;
  
    // Finding the unique values
    for (int x = 0; x <= n + 1; x++) {
        if (freq[x]) {
            unique++;
        }
    }
  
    // Returning the number of unique values
    return unique;
}
  
// Driver Code
int main()
{
    int arr[] = { 3, 3, 3, 3 };
  
    // Size of the array
    int n = 4;
  
    int ans = uniqueNumbers(arr, n);
    cout << ans;
    return 0;
}


Java
// Java program to find the maximum number of 
// unique values in the array 
import java.util.*;
  
class GFG {
      
    // Function to find the maximum number of 
    // unique values in the array 
    static int uniqueNumbers(int arr[], int n) 
    { 
        // Sorting the given array 
        Arrays.sort(arr); 
      
        // This array will store the frequency 
        // of each number in the array 
        // after performing the given operation 
        int freq[] = new int[n + 2]; 
      
        // Initialising the array with all zeroes 
        for(int i = 0; i < n + 2; i++)
            freq[i] = 0;
  
        // Loop to apply operation on 
        // each element of the array 
        for (int x = 0; x < n; x++) { 
      
            // Incrementing the value at index x 
            // if the value arr[x] - 1 is 
            // not present in the array 
            if (freq[arr[x] - 1] == 0) { 
                freq[arr[x] - 1]++; 
            } 
      
            // If arr[x] itself is not present, then it 
            // is left as it is 
            else if (freq[arr[x]] == 0) { 
                freq[arr[x]]++; 
            } 
      
            // If both arr[x] - 1 and arr[x] are present 
            // then the value is incremented by 1 
            else { 
                freq[arr[x] + 1]++; 
            } 
        } 
      
        // Variable to store the number of unique values 
        int unique = 0; 
      
        // Finding the unique values 
        for (int x = 0; x <= n + 1; x++) { 
            if (freq[x] != 0) { 
                unique++; 
            } 
        } 
      
        // Returning the number of unique values 
        return unique; 
    } 
      
    // Driver Code 
    public static void main (String[] args)
    { 
        int []arr = { 3, 3, 3, 3 }; 
      
        // Size of the array 
        int n = 4; 
      
        int ans = uniqueNumbers(arr, n); 
        System.out.println(ans); 
    } 
}
  
// This code is contributed by Yash_R


Python3
# Python program to find the maximum number of
# unique values in the array
  
# Function to find the maximum number of
# unique values in the array
def uniqueNumbers(arr, n):
  
    # Sorting the given array
    arr.sort()
  
    # This array will store the frequency
    # of each number in the array
    # after performing the given operation
    freq =[0]*(n + 2)
  
    # Loop to apply the operation on 
    # each element of the array
    for val in arr:
  
        # Incrementing the value at index x
        # if the value  arr[x] - 1 is
        # not present in the array
        if(freq[val-1]== 0):
            freq[val-1]+= 1
  
        # If arr[x] itself is not present, then it
        # is left as it is
        elif(freq[val]== 0):
            freq[val]+= 1
  
        # If both arr[x] - 1 and arr[x] are present
        # then the value is incremented by 1
        else:
            freq[val + 1]+= 1
      
    # Variable to store the 
    # number of unique values
    unique = 0
  
    # Finding the number of unique values
    for val in freq:
        if(val>0):
            unique+= 1
      
    return unique
  
# Driver code
if __name__ == "__main__":
    arr =[3, 3, 3, 3]
    n = 4
    print(uniqueNumbers(arr, n))


C#
// C# program to find the maximum number of 
// unique values in the array 
using System;
  
class GFG {
      
    // Function to find the maximum number of 
    // unique values in the array 
    static int uniqueNumbers(int []arr, int n) 
    { 
        // Sorting the given array 
        Array.Sort(arr); 
      
        // This array will store the frequency 
        // of each number in the array 
        // after performing the given operation 
        int []freq = new int[n + 2]; 
      
        // Initialising the array with all zeroes 
        for(int i = 0; i < n + 2; i++)
            freq[i] = 0;
  
        // Loop to apply operation on 
        // each element of the array 
        for (int x = 0; x < n; x++) { 
      
            // Incrementing the value at index x 
            // if the value arr[x] - 1 is 
            // not present in the array 
            if (freq[arr[x] - 1] == 0) { 
                freq[arr[x] - 1]++; 
            } 
      
            // If arr[x] itself is not present, then it 
            // is left as it is 
            else if (freq[arr[x]] == 0) { 
                freq[arr[x]]++; 
            } 
      
            // If both arr[x] - 1 and arr[x] are present 
            // then the value is incremented by 1 
            else { 
                freq[arr[x] + 1]++; 
            } 
        } 
      
        // Variable to store the number of unique values 
        int unique = 0; 
      
        // Finding the unique values 
        for (int x = 0; x <= n + 1; x++) { 
            if (freq[x] != 0) { 
                unique++; 
            } 
        } 
      
        // Returning the number of unique values 
        return unique; 
    } 
      
    // Driver Code 
    public static void Main (string[] args)
    { 
        int []arr = { 3, 3, 3, 3 }; 
      
        // Size of the array 
        int n = 4; 
      
        int ans = uniqueNumbers(arr, n); 
        Console.WriteLine(ans); 
    } 
}
  
// This code is contributed by Yash_R


输出:
3

时间复杂度分析:

  • 给定数组排序所需的时间为O(N * log(N)) ,其中N是数组的大小。
  • 在已排序数组上运行循环以执行操作所需的时间为O(N)
  • 在哈希上运行循环以计算唯一值所需的时间为O(N)
  • 因此,总体时间复杂度为O(N * log(N))+ O(N)+ O(N) 。由于N * log(N)较大,因此上述方法的最终时间复杂度为O(N * log(N))

如果您希望与行业专家一起参加实时课程,请参阅Geeks在线课程