Given an array of N integers. In the given, for each positive element ‘x’ there exist a negative value ‘-x’, except one integer whose negative value is not present. That integer may occur multiple number of time. The task is find that integer.
Examples:
Input : arr[] = { 1, 8, -6, -1, 6, 8, 8 } Output : 8 All positive elements have an equal negative value except 8. Input : arr[] = { 15, 6, -9, 4, 15, 9, -6, -4, 15, 15 } Output : 15
Method 1: (hashing) The idea is to create a hash table, initialize with zero value. Whenever we encounter a positive value, we add +1 on corresponding index position in hash. And whenever we encounter negative value we add -1. Finally we traverse the whole hash. After traversing, the index with non-zero value is the only integer with only value without negative pair.
Below is the implementation of this approach:
// A hashing based solution to find the only // element that doesn't have negative value. #include <bits/stdc++.h> using namespace std;
// Return the integer with have no negative value. int findInteger( int arr[], int n)
{ unorder_map< int , int > hash;
int maximum = 0;
// Traversing the array.
for ( int i = 0; i < n; i++) {
// If negative, then subtract 1 in hash array.
if (arr[i] < 0)
hash[ abs (arr[i])] -= 1;
// Else add 1 in hash array.
else
hash[arr[i]] += 1;
}
// Traverse the hash array.
for ( int i = 0; i < n; i++)
if (hash[arr[i]] == 0)
return i;
return -1;
} // Driven Program int main()
{ int arr[] = { 1, 8, -6, -1, 6, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findInteger(arr, n) << endl;
return 0;
} |
# A hashing based solution to find the only # element that doesn't have negative value. # Return the integer with have no negative value. def findInteger(arr, n):
hash = dict ()
maximum = 0
# Traversing the array.
for i in arr:
# If negative, then subtract 1 in hash array.
if (i < 0 ):
if abs (i) not in hash .keys():
hash [ abs (i)] = - 1
else :
hash [ abs (i)] - = 1
# Else add 1 in hash array.
else :
hash [i] = hash .get(i, 0 ) + 1
# Traverse the hash array.
for i in arr:
if i in hash .keys() and hash [i] > 0 :
return i
return - 1
# Driver Code arr = [ 1 , 8 , - 6 , - 1 , 6 , 8 ]
n = len (arr)
print (findInteger(arr, n))
# This code is contributed by Mohit Kumar |
Output:
8
Method 2: (efficient) The idea is to find the sum of each element of array and also count the number of positive and negative numbers in the array. Finally, divide the sum by absolute difference of number of positive elements and negative elements.
Below is the implementation of this approach:
// An efficient solution to find the only // element that doesn't have negative value. #include <bits/stdc++.h> using namespace std;
// Return the integer with have no negative value. int findInteger( int arr[], int n)
{ int neg = 0, pos = 0;
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
// If negative, then increment neg count.
if (arr[i] < 0)
neg++;
// Else increment pos count..
else pos++;
}
return (sum / abs (neg - pos));
} // Driven Program int main()
{ int arr[] = { 1, 8, -6, -1, 6, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findInteger(arr, n) << endl;
return 0;
} |
// An efficient solution to find the // only element that doesn't have // negative value. import java.lang.*;
class GFG {
// Return the integer with have
// no negative value.
static int findInteger( int arr[], int n)
{
int neg = 0 , pos = 0 ;
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
// If negative, then increment
// neg count.
if (arr[i] < 0 )
neg++;
// Else increment pos count..
else
pos++;
}
return (sum / Math.abs(neg - pos));
}
// Driven Program
public static void main(String[] args)
{
int arr[] = new int []{ 1 , 8 , - 6 , - 1 , 6 , 8 };
int n = arr.length;
System.out.println(findInteger(arr, n));
}
} // This code is contributed by Smitha. |
# An efficient solution to find the # only element that doesn't have # negative value. # Return the integer with have no # negative value. def findInteger(arr, n):
neg = 0
pos = 0
sum = 0
for i in range ( 0 , n):
sum + = arr[i]
# If negative, then increment
# neg count.
if (arr[i] < 0 ):
neg + = 1
# Else increment pos count..
else :
pos + = 1
return ( sum / abs (neg - pos))
# Driven Program arr = [ 1 , 8 , - 6 , - 1 , 6 , 8 ]
n = len (arr)
print ( int (findInteger(arr, n)))
# This code is contributed by Smitha. |
// An efficient solution to find the // only element that doesn't have // negative value. using System;
class GFG {
// Return the integer with have
// no negative value.
static int findInteger( int [] arr, int n)
{
int neg = 0, pos = 0;
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
// If negative, then increment
// neg count.
if (arr[i] < 0)
neg++;
// Else increment pos count..
else
pos++;
}
return (sum / Math.Abs(neg - pos));
}
// Driven Program
public static void Main()
{
int [] arr = new int []{ 1, 8, -6,
-1, 6, 8 };
int n = arr.Length;
Console.Write(findInteger(arr, n));
}
} // This code is contributed by Smitha. |
<?php // An efficient solution to find the only // element that doesn't have negative value. // Return the integer with // have no negative value. function findInteger( $arr , $n )
{ $neg = 0; $pos = 0;
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$sum += $arr [ $i ];
// If negative, then
// increment neg count.
if ( $arr [ $i ] < 0)
$neg ++;
// Else increment
// pos count..
else
$pos ++;
}
return ( $sum / abs ( $neg - $pos ));
} // Driver Code
$arr = array (1, 8, -6, -1, 6, 8);
$n = sizeof( $arr );
echo findInteger( $arr , $n ) , "\n" ;
// This code is contributed by aj_36 ?> |
Output:
8
Recommended Posts:
- Pairs of Positive Negative values in an array
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Minimum positive integer required to split the array equally
- Partition negative and positive without comparison with 0
- Minimum number of changes such that elements are first Negative and then Positive
- Print all the pairs that contains the positive and negative values of an element
- Segregating negative and positive maintaining order and O(1) space
- Make three non-empty sets with negative, positive and 0 products
- Rearrange positive and negative numbers using inbuilt sort function
- Lambda expression in Python to rearrange positive and negative numbers
- Longest alternating (positive and negative) subarray starting at every index
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.