- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we have an array containing distinct elements. We need to print the pairs of positive-negative values in the array with the same absolute value and print them in sorted order for examples −

Input : arr[] = { 1, -1, 11, 12, 56, 77, -56, -12, -88} Output : -1 1 -12 12 -56 56 Input : arr[] = {30, 40, 50, 77, -51, -50, -40} Output : -40 40 -50 50

The first approach that comes to our mind is the Brute Force approach, and we make up another one called the Efficient approach. We are going to discuss both of the approaches.

In this approach, we will traverse the array with one index in hand and find the same absolute value but a different index.

#include<bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, -1, 11, 12, 56, 77, -56, -12, -88 }; int n = sizeof(arr)/sizeof(int); // size of our array. vector<int> nums; // the present pairs. for(int i = 0; i < n; i++) { for(int j = i+1; j < n; j++) { if(abs(arr[j]) == abs(arr[i])) { // finding the pairs. nums.push_back(abs(arr[i])); break; // if we found the pair then we can just break as there are distinct elements in the array. } } } sort(nums.begin(), nums.end()); for(auto x : nums) // printing the pairs. cout << -x << " " << x << " "; }

-1 1 -12 12 -56 56

In this approach, we use two loops for traversing the array and finding the other element now; if we find the other element, we break out from the inner loop to run the code faster. Now that we are using two for-loop, our overall time complexity is O(N*N). N is the size of a given array that is good for lower constraints but not good for higher constraints, so now we will discuss the other approach.

In this approach, we will use a hashmap, which will help in a substantial decrease of our time complexity.

#include<bits/stdc++.h> using namespace std; int main() { int arr[] = { 4, 8, 9, -4, 1, -1, -8, -9 }; int n = sizeof(arr)/sizeof(int); // size of our array. map<int, int> found; // going to store the count of numbers found. vector<int> nums; // the present pairs. for(int i = 0; i < n; i++) found[abs(arr[i])]++; // increasing the frequency of abs(arr[i]). for(auto x : found) { // traversing the map. if(x.second == 2) // if any numbers frequency is two then push it to nums. nums.push_back(x.first); } for(auto x : nums) // printing the pairs. cout << -x << " " << x << " "; }

-1 1 -4 4 -8 8 -9 9

In this approach, we are using a hashmap to store the frequency of a number; as we traverse through the array, we are updating the frequency of the absolute value of the current element now as you know that all the pairs will have the value of two so then we are traversing the map.

If the frequency of any number is 2, then we are storing it in nums, and finally, we are printing the values in sorted order. (as the map contains the number in sorted order, we don’t need to sort the numbers vector).

In this article, we solve a problem to find the Pairs of Positive Negative values in an array using Hashing Technique. We also learned the C++ program for this problem and the complete approach ( Normal and efficient ) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.

- Related Questions & Answers
- Find the Pairs of Positive Negative values in an Array using C++\n
- Print all the pairs that contains the positive and negative values of an element in C++
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Positive, negative and zeroes contribution of an array in JavaScript
- How to find the groupwise number of positive and negative values in an R data frame?
- Python - Sum negative and positive values using GroupBy in Pandas
- How to convert negative values in an R data frame to positive values?
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- How to find the number of positive values in an R vector?
- Finding array number that have no matching positive or negative number in the array using JavaScript
- Converting boolean values to positive or negative sign in MySQL?
- How to print positive and negative infinity values in JavaScript?
- Java Program to convert positive int to negative and negative to positive
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Find number of pairs in an array such that their XOR is 0 using C++.

Advertisements