Open in App
Not now

# Maximize count of unique elements in Array by changing elements to negative

• Last Updated : 01 Feb, 2022

Given array arr containing N integers. the task is to find the maximum number of unique elements in the array if each element in the array can be changed to its negative i.e. X can be changed to -X in the array.

Example:

Input: arr[] = {-1, 3, 2, 3, 2}
Output: 5
Explanation: Change one 2 to -2 and another 3 to -3 to get the arr[]={-1, 3, 2, -3, -2}, having 5 unique values.

Input: arr[] = {1, 2, 2, 2, 3, 3, 3}
Output: 5

Approach: A set can be used in this problem. Follow the below steps to solve:

• Traverse the array from i=0 to i<N, and for each element arr[i]:
• Check if abs(arr[i]) is present in the set or not.
• If it’s not present then insert it in the set.
• Else insert the original value in the set.
• As the set contains only the original values, so the answer will be the size of the set.

Below is the implementation of the above approach:

## 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<``int``> 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<``int``> s = ``new` `HashSet<``int``>();` `    ``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

 ``

Output

`5`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles