Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Given an array of integers return positives, whose equivalent negatives present in it in JavaScript
In this problem, we need to find positive integers from an array that have their corresponding negative counterparts present in the same array. For example, if we have [1, -1, 2, -3, 4, -4], we should return [1, 4] since both 1 and 4 have their negatives (-1 and -4) in the array.
Understanding the Problem
Given an array of integers containing both positive and negative values, we need to identify positive numbers whose negative equivalents also exist in the array. For instance, in the array [7, -7, 8, -8, 9, -9], all positive numbers (7, 8, 9) have their corresponding negatives present, so our result would be [7, 8, 9].
Algorithm Approach
We'll use a two-pass approach with a Set data structure for efficient lookup:
- First pass: Store all positive numbers in a Set
- Second pass: For each negative number, check if its absolute value exists in the positive Set
- If found, add the positive value to our result array
Implementation
function findPositivesWithNegatives(arr) {
const positiveSet = new Set();
const result = [];
// First pass: collect all positive numbers
for (let num of arr) {
if (num > 0) {
positiveSet.add(num);
}
}
// Second pass: find positives with corresponding negatives
for (let num of arr) {
if (num < 0 && positiveSet.has(Math.abs(num))) {
result.push(Math.abs(num));
}
}
return result;
}
// Test with example array
const array = [1, -1, 2, -2, 3, -4, 4, 5, -5];
const positives = findPositivesWithNegatives(array);
console.log("Original array:", array);
console.log("Positives with negatives:", positives);
Original array: [1, -1, 2, -2, 3, -4, 4, 5, -5] Positives with negatives: [1, 2, 5]
Alternative Single-Pass Solution
We can optimize this to a single pass by checking conditions during iteration:
function findPositivesSinglePass(arr) {
const positiveSet = new Set();
const result = [];
for (let num of arr) {
if (num > 0) {
positiveSet.add(num);
} else if (num < 0 && positiveSet.has(Math.abs(num))) {
result.push(Math.abs(num));
}
}
return result;
}
// Test the single-pass version
const testArray = [7, -7, 8, -8, 9, -9, 10];
console.log("Result:", findPositivesSinglePass(testArray));
Result: [7, 8, 9]
Handling Edge Cases
// Test edge cases
console.log("Empty array:", findPositivesWithNegatives([]));
console.log("Only positives:", findPositivesWithNegatives([1, 2, 3]));
console.log("Only negatives:", findPositivesWithNegatives([-1, -2, -3]));
console.log("With zero:", findPositivesWithNegatives([0, 1, -1, 2]));
Empty array: [] Only positives: [] Only negatives: [] With zero: [1]
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Two-pass solution | O(n) | O(n) |
| Single-pass solution | O(n) | O(n) |
Both approaches have linear time complexity since we traverse the array once (or twice), and Set operations (add, has) are O(1) on average. The space complexity is O(n) for storing positive numbers in the Set.
Conclusion
This problem efficiently demonstrates the use of Set data structure for fast lookups. The single-pass solution is slightly more efficient as it processes the array only once, making it ideal for large datasets where performance matters.
