Created
May 27, 2025 14:18
-
-
Save wperron/7c84be2f75edbd57409979c67b53358e to your computer and use it in GitHub Desktop.
Find pairs of numbers that sum to an odd number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @function oddSum | |
* @description | |
* Identifies and returns pairs of numbers (one from array 'a' and one from array 'b') | |
* such that the sum of the two numbers in each pair is an odd number. | |
* | |
* The function iterates through all possible combinations of elements from the two input arrays. | |
* It leverages the mathematical property that the sum of an odd number and an even number | |
* is always odd. The core logic checks if one number in the pair is odd and the other is even. | |
* If this condition is met (meaning their sum would be odd), the pair is added to an | |
* array which is then returned. | |
* | |
* @param {number[]} a - The first array of numbers. Each element is expected to be a number. | |
* @param {number[]} b - The second array of numbers. Each element is expected to be a number. | |
* | |
* @returns {Array<Array<number>>} An array containing pairs of numbers [numFromA, numFromB] | |
* where one number is odd and the other is even, ensuring their sum is odd. | |
* Returns an empty array if no such pairs are found or if input arrays are empty. | |
* | |
* @example | |
* // Example usage: | |
* const arr1 = [1, 2, 3]; // 1 (odd), 2 (even), 3 (odd) | |
* const arr2 = [4, 5, 6]; // 4 (even), 5 (odd), 6 (even) | |
* const result = oddSum(arr1, arr2); | |
* console.log(result); | |
* // Output will be pairs whose sum is odd: | |
* // [ | |
* // [1, 4], // 1 (odd) + 4 (even) = 5 (odd) | |
* // [1, 6], // 1 (odd) + 6 (even) = 7 (odd) | |
* // [2, 5], // 2 (even) + 5 (odd) = 7 (odd) | |
* // [3, 4], // 3 (odd) + 4 (even) = 7 (odd) | |
* // [3, 6] // 3 (odd) + 6 (even) = 9 (odd) | |
* // ] | |
* | |
* // Example with no matching pairs: | |
* const arr3 = [2, 4]; // all even | |
* const arr4 = [6, 8]; // all even | |
* console.log(oddSum(arr3, arr4)); // Output: [] | |
* | |
* @remarks | |
* How the condition identifies pairs that sum to an odd number: | |
* 1. The sum of two integers (i + j) is odd if and only if one integer is odd and the other is even. | |
* - Odd + Even = Odd | |
* - Even + Odd = Odd | |
* - Odd + Odd = Even | |
* - Even + Even = Even | |
* 2. The expression `(num & 1)` is a bitwise AND operation. It results in `1` if `num` is odd | |
* (its least significant bit is 1) and `0` if `num` is even (its least significant bit is 0). | |
* 3. `((i & 1) == 1)` evaluates to `true` if `i` is odd, and `false` if `i` is even. | |
* Similarly for `((j & 1) == 1)`. | |
* 4. The condition `((i & 1) == 1) != ((j & 1) == 1)` checks if the "oddness" of `i` is | |
* different from the "oddness" of `j`. This is true if one is odd and the other is even. | |
* 5. Therefore, this condition directly identifies pairs `(i, j)` whose sum `i + j` will be an odd number. | |
*/ | |
function oddSum(a, b) { | |
let pairs = []; | |
for (const i of a) { | |
for (const j of b) { | |
if (((i & 1) == 1) != ((j & 1) == 1)) { | |
pairs.push([i, j]); | |
} | |
} | |
} | |
return pairs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment