# Only integer with positive value in positive negative value in array

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// A hashing based solution to find the only ` `// element that doesn't have negative value. ` `#include ` `using` `namespace` `std; ` ` `  `// Return the integer with have no negative value. ` `int` `findInteger(``int` `arr[], ``int` `n) ` `{ ` `    ``unordered_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` `arr[i]; ` ` `  `    ``return` `-1; ` `} ` ` `  `// Driven Program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 8, -6, -1, 6, 8 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << findInteger(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// A hashing based solution to find the only ` `// element that doesn't have negative value. ` `import` `java.util.*; ` `class` `GFG ` `{ ` ` `  `// Return the integer with have no negative value. ` `static` `int` `findInteger(``int` `arr[], ``int` `n) ` `{ ` `    ``HashMap hash = ``new` `HashMap<>(); ` `    ``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.put(Math.abs(arr[i]), ` `           ``(hash.get(Math.abs(arr[i])) == ``null` `? ``0` `:  ` `            ``hash.get(Math.abs(arr[i]))) - ``1``); ` ` `  `        ``// Else add 1 in hash array. ` `        ``else` `            ``hash.put(Math.abs(arr[i]),  ` `           ``(hash.get(Math.abs(arr[i])) == ``null` `? ``0` `:  ` `            ``hash.get(Math.abs(arr[i]))) + ``1``); ` `    ``} ` ` `  `    ``// Traverse the hash array. ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``if` `(hash.get(arr[i]) > ``0``) ` `            ``return` `arr[i]; ` ` `  `    ``return` `-``1``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``1``, ``8``, -``6``, -``1``, ``6``, ``8` `}; ` `    ``int` `n = arr.length; ` `    ``System.out.println( findInteger(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# 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:

## C++

 `// An efficient solution to find the only ` `// element that doesn't have negative value. ` `#include ` `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); ` `    ``cout << findInteger(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// 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. `

## Python 3

 `# 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. `

## C#

 `// 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

 ` `

Output:

```8
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.