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++
#include <bits/stdc++.h>
using namespace std;
int CountUniqueElements( int arr[], int N)
{
unordered_set< int > s;
int i = 0;
while (i < N) {
int val1 = abs (arr[i]);
int val2 = -val1;
if (s.count(val1)) {
s.insert(val2);
}
else {
s.insert(val1);
}
i++;
}
return s.size();
}
int main()
{
int arr[] = { 1, 2, 2, 2, 3, 3, 3 };
int N = sizeof (arr) / sizeof ( int );
cout << CountUniqueElements(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static int CountUniqueElements( int arr[], int N)
{
Set<Integer> s = new HashSet<Integer>();
int i = 0 ;
while (i < N)
{
int val1 = Math.abs(arr[i]);
int val2 = -val1;
if (s.contains(val1)) {
s.add(val2);
}
else {
s.add(val1);
}
i++;
}
return s.size();
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 2 , 2 , 3 , 3 , 3 };
int N =arr.length;
System.out.print(CountUniqueElements(arr, N));
}
}
|
Python3
def CountUniqueElements(arr, N):
s = set ([])
i = 0
while (i < N):
val1 = abs (arr[i])
val2 = - val1
if ( list (s).count(val1)):
s.add(val2)
else :
s.add(val1)
i + = 1
return len (s)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 , 2 , 3 , 3 , 3 ]
N = len (arr)
print (CountUniqueElements(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int CountUniqueElements( int [] arr, int N)
{
HashSet< int > s = new HashSet< int >();
int i = 0;
while (i < N)
{
int val1 = Math.Abs(arr[i]);
int val2 = -val1;
if (s.Contains(val1))
{
s.Add(val2);
}
else
{
s.Add(val1);
}
i++;
}
return s.Count;
}
public static void Main()
{
int [] arr = { 1, 2, 2, 2, 3, 3, 3 };
int N = arr.Length;
Console.Write(CountUniqueElements(arr, N));
}
}
|
Javascript
<script>
function CountUniqueElements(arr, N)
{
let s = new Set();
let i = 0;
while (i < N)
{
let val1 = Math.abs(arr[i]);
let val2 = -val1;
if (s.has(val1)) {
s.add(val2);
}
else {
s.add(val1);
}
i++;
}
return s.size;
}
let arr = [1, 2, 2, 2, 3, 3, 3];
let N = arr.length;
document.write(CountUniqueElements(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...