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:
// 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; } |
Output:
8
Method 2: (efficient) The idea is to find the sum of each element of array and also count the number of even and odd numbers in the array. Finally, divide the sum by absolute difference of number of odd element and even element.
Below is the implementation of this approach:
C++
// 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; } |
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
<?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 ) , "
" ; // This code is contributed by aj_36 ?> |
Output:
8
leave a comment
0 Comments