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

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 C++ implementation of this approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right



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.