Pairs of Positive Negative values in an array
Given an array of distinct integers, print all the pairs having positive value and negative value of a number that exists in the array. We need to print pairs in order of their occurrences. A pair whose any element appears first should be printed first.
Examples:
Input : arr[] = { 1, -3, 2, 3, 6, -1 }
Output : -1 1 -3 3
Input : arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }
Output : -1 1 -4 4 -8 8 -9 9
Method 1 (Simple : O(n2))
The idea is to use two nested loop. For each element arr[i], find negative of arr[i] from index i + 1 to n – 1 and store it in another array. For output, sort the stored element and print negative positive value of the stored element.
Below is the implementation of this approach:
C++
// Simple CPP program to find pairs of positive // and negative values present in an array. #include <bits/stdc++.h> using namespace std; // Print pair with negative and positive value void printPairs(int arr[], int n) { vector<int> v; // For each element of array. for (int i = 0; i < n; i++) // Try to find the negative value of // arr[i] from i + 1 to n for (int j = i + 1; j < n; j++) // If absolute values are equal print pair. if (abs(arr[i]) == abs(arr[j])) v.push_back(abs(arr[i])); // If size of vector is 0, therefore there is no // element with positive negative value, print "0" if (v.size() == 0) return; // Sort the vector sort(v.begin(), v.end()); // Print the pair with negative positive value. for (int i = 0; i < v.size(); i++) cout << -v[i] << " " << v[i]; } // Driven Program int main() { int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = sizeof(arr) / sizeof(arr[0]); printPairs(arr, n); return 0; } |
Java
// Java program to find pairs of positive // and negative values present in an array. import java.util.*; import java.lang.*; class GFG { // Print pair with negative and positive value public static void printPairs(int arr[] , int n) { Vector<Integer> v = new Vector<Integer>(); // For each element of array. for (int i = 0; i < n; i++) // Try to find the negative value of // arr[i] from i + 1 to n for (int j = i + 1; j < n; j++) // If absolute values are equal // print pair. if (Math.abs(arr[i]) == Math.abs(arr[j])) v.add(Math.abs(arr[i])); // If size of vector is 0, therefore there // is no element with positive negative // value, print "0" if (v.size() == 0) return; // Sort the vector Collections.sort(v); // Print the pair with negative positive // value. for (int i = 0; i < v.size(); i++) System.out.print(-v.get(i) + " " + v.get(i)); } // Driven Program public static void main(String[] args) { int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = arr.length; printPairs(arr, n); } } // This code is contributed by Prasad Kshirsagar. |
Python 3
# Simple Python 3 program to find # pairs of positive and negative # values present in an array. # Print pair with negative and # positive value def printPairs(arr, n): v = [] # For each element of array. for i in range(n): # Try to find the negative value # of arr[i] from i + 1 to n for j in range( i + 1,n) : # If absolute values are # equal print pair. if (abs(arr[i]) == abs(arr[j])) : v.append(abs(arr[i])) # If size of vector is 0, therefore # there is no element with positive # negative value, print "0" if (len(v) == 0): return; # Sort the vector v.sort() # Print the pair with negative # positive value. for i in range(len( v)): print(-v[i], "" , v[i], end = " ") # Driver Code if __name__ == "__main__": arr = [ 4, 8, 9, -4, 1, -1, -8, -9 ] n = len(arr) printPairs(arr, n) # This code is contributed # by ChitraNayal |
C#
// C# program to find pairs of positive // and negative values present in an array. using System; using System.Collections.Generic; class GFG { // Print pair with negative and positive value public static void printPairs(int []arr , int n) { List<int> v = new List<int>(); // For each element of array. for (int i = 0; i < n; i++) // Try to find the negative value of // arr[i] from i + 1 to n for (int j = i + 1; j < n; j++) // If absolute values are equal // print pair. if (Math.Abs(arr[i]) == Math.Abs(arr[j])) v.Add(Math.Abs(arr[i])); // If size of vector is 0, therefore there // is no element with positive negative // value, print "0" if (v.Count == 0) return; // Sort the vector v.Sort(); // Print the pair with negative positive // value. for (int i = 0; i < v.Count; i++) Console.Write(-v[i] + " " + v[i]); } // Driver code public static void Main(String[] args) { int []arr = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = arr.Length; printPairs(arr, n); } } // This code has been contributed by 29AjayKumar |
-1 1-4 4-8 8-9 9
Method 2 (Hahing)
The idea is to use hashing. Traverse the given array, increase the count at absolute value of hash table. If count becomes 2, store its absolute value in another vector. And finally sort the vector. If the size of the vector is 0, print “0”, else for each term in vector print first its negative value and the the positive value.
Below is the implementation of this approach:
C++
// Efficient CPP program to find pairs of // positive and negative values present in // an array. #include <bits/stdc++.h> using namespace std; // Print pair with negative and positive value void printPairs(int arr[], int n) { vector<int> v; unordered_map<int, bool> cnt; // For each element of array. for (int i = 0; i < n; i++) { // If element has not encounter early, // mark it on cnt array. if (cnt[abs(arr[i])] == 0) cnt[abs(arr[i])] = 1; // If seen before, push it in vector ( // given that elements are distinct) else { v.push_back(abs(arr[i])); cnt[abs(arr[i])] = 0; } } if (v.size() == 0) return; sort(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) cout << -v[i] << " " << v[i] << " "; } // Driven Program int main() { int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = sizeof(arr) / sizeof(arr[0]); printPairs(arr, n); return 0; } |
Java
// Efficient Java program to find pairs of // positive and negative values present in // an array. import java.util.*; class GFG { // Print pair with negative // and positive value static void printPairs(int arr[], int n) { ArrayList<Integer> v = new ArrayList<Integer> (); HashMap<Integer, Integer> cnt = new HashMap<Integer, Integer>(); // For each element of array. for (int i = 0; i < n; i++) { // If element has not encounter early, // mark it on cnt array. if (cnt.containsKey(Math.abs(arr[i]))) cnt.put(Math.abs(arr[i]), 1); // If seen before, push it in vector // (given that elements are distinct) else { v.add(Math.abs(arr[i])); cnt.put(Math.abs(arr[i]), 0); } } if (v.size() == 0) return; Collections.sort(v); for (int i = 0; i < v.size(); i++) System.out.print("-" + v.get(i) + " " + v.get(i) + " "); } // Driver Code public static void main(String[] args) { int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = arr.length; printPairs(arr, n); } } // This code is contributed by Prerna Saini |
-1 1 -4 4 -8 8 -9 9
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Print all the pairs that contains the positive and negative values of an element
- Only integer with positive value in positive negative value in array
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Partition negative and positive without comparison with 0
- Minimum number of changes such that elements are first Negative and then Positive
- 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
- Longest alternating (positive and negative) subarray starting at every index
- Lambda expression in Python to rearrange positive and negative numbers
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Move all negative numbers to beginning and positive to end with constant extra space



