📌  相关文章
📜  通过将元素更改为负数来最大化 Array 中唯一元素的数量

📅  最后修改于: 2022-05-13 01:56:07.746000             🧑  作者: Mango

通过将元素更改为负数来最大化 Array 中唯一元素的数量

给定包含N个整数的数组arr 。任务是找到数组中唯一元素的最大数量,如果数组中的每个元素都可以更改为负数,即可以将数组中的X更改为-X

例子:

方法:可以在这个问题中使用一个集合。请按照以下步骤解决:

  • i=0i遍历数组,并且对于每个元素arr[i]
    • 检查集合中是否存在abs(arr[i])
    • 如果它不存在,则将其插入集合中。
    • 否则在集合中插入原始值。
  • 由于集合仅包含原始值,因此答案将是集合的大小。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the number of unique elements
int CountUniqueElements(int arr[], int N)
{
    // Set to hold unique values
    unordered_set s;
    int i = 0;
 
    // Loop to determine and
    // store all unique values
    while (i < N) {
        // Positive value of arr[i]
        int val1 = abs(arr[i]);
 
        // Negative value of arr[i]
        int val2 = -val1;
 
        // Checking if val1 is present or not
        // If not then insert val1 in the set
        // Insert val2 in the set
        if (s.count(val1)) {
            s.insert(val2);
        }
 
        // Else inserting the original value
        else {
            s.insert(val1);
        }
        i++;
    }
 
    // Return the count of unique
    // values in the Array
    return s.size();
}
 
// Driver Code
int main()
{
    // Declaring Array of size 7
    int arr[] = { 1, 2, 2, 2, 3, 3, 3 };
    int N = sizeof(arr) / sizeof(int);
 
    cout << CountUniqueElements(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find the number of unique elements
  static int CountUniqueElements(int arr[], int N)
  {
     
    // Set to hold unique values
    Set s = new HashSet();
    int i = 0;
 
    // Loop to determine and
    // store all unique values
    while (i < N)
    {
       
      // Positive value of arr[i]
      int val1 = Math.abs(arr[i]);
 
      // Negative value of arr[i]
      int val2 = -val1;
 
      // Checking if val1 is present or not
      // If not then insert val1 in the set
      // Insert val2 in the set
      if (s.contains(val1)) {
        s.add(val2);
      }
 
      // Else inserting the original value
      else {
        s.add(val1);
      }
      i++;
    }
 
    // Return the count of unique
    // values in the Array
    return s.size();
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    // Declaring Array of size 7
    int arr[] = { 1, 2, 2, 2, 3, 3, 3 };
    int N =arr.length;
    System.out.print(CountUniqueElements(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python 3 program for the above approach
 
# Function to find the number of unique elements
def CountUniqueElements(arr, N):
 
    # Set to hold unique values
    s = set([])
    i = 0
 
    # Loop to determine and
    # store all unique values
    while (i < N):
        # Positive value of arr[i]
        val1 = abs(arr[i])
 
        # Negative value of arr[i]
        val2 = -val1
 
        # Checking if val1 is present or not
        # If not then insert val1 in the set
        # Insert val2 in the set
        if (list(s).count(val1)):
            s.add(val2)
 
        # Else inserting the original value
        else:
            s.add(val1)
 
        i += 1
 
    # Return the count of unique
    # values in the Array
    return len(s)
 
# Driver Code
if __name__ == "__main__":
 
    # Declaring Array of size 7
    arr = [1, 2, 2, 2, 3, 3, 3]
    N = len(arr)
 
    print(CountUniqueElements(arr, N))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to find the number of unique elements
  static int CountUniqueElements(int[] arr, int N)
  {
 
    // Set to hold unique values
    HashSet s = new HashSet();
    int i = 0;
 
    // Loop to determine and
    // store all unique values
    while (i < N)
    {
 
      // Positive value of arr[i]
      int val1 = Math.Abs(arr[i]);
 
      // Negative value of arr[i]
      int val2 = -val1;
 
      // Checking if val1 is present or not
      // If not then insert val1 in the set
      // Insert val2 in the set
      if (s.Contains(val1))
      {
        s.Add(val2);
      }
 
      // Else inserting the original value
      else
      {
        s.Add(val1);
      }
      i++;
    }
 
    // Return the count of unique
    // values in the Array
    return s.Count;
  }
 
  // Driver Code
  public static void Main()
  {
 
    // Declaring Array of size 7
    int[] arr = { 1, 2, 2, 2, 3, 3, 3 };
    int N = arr.Length;
    Console.Write(CountUniqueElements(arr, N));
  }
}
 
// This code is contributed by gfgking


Javascript



输出
5

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