
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Pairs of Positive Negative values in an Array using C++
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.
- Related Articles
- Print all the pairs that contains the positive and negative values of an element in C++
- Find Count of Positive, Negative and Zero Elements in an Array in Java
- Test array values for positive or negative infinity in Numpy
- 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?
- Find count of positive and negative array elements in Java
- Python - Sum negative and positive values using GroupBy in Pandas
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- How to convert negative values in an R data frame to positive values?
- Finding array number that have no matching positive or negative number in the array using JavaScript
- Increment Negative and Decrement Positive Numbers by 1 in an Array in Java
- How to find the number of positive values in an R vector?
- Converting boolean values to positive or negative sign in MySQL?
- How to print positive and negative infinity values in JavaScript?
