Find the Pairs of Positive Negative values in an Array using C++

C++Server Side ProgrammingProgramming

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

Approach to find The Solution

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.

Brute Force Approach

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

Example

#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 << " ";
}

Output

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

Efficient Approach

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

Example

#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 << " ";
}

Output

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

Explanation of the above code

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

Conclusion

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.

raja
Updated on 26-Nov-2021 07:14:42

Advertisements