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
JavaScript Program for Pairs such that one is a power multiple of other
JavaScript Program for pairs such that one is a power multiple of the other is a problem that involves finding pairs of numbers where one number can be expressed as a power of another number. In this tutorial, we will explore how to write a JavaScript program to solve this problem using different approaches and algorithms.
A power multiple relationship exists when one number is equal to another number raised to some integer power. For example, 8 is a power multiple of 2 because 2³ = 8.
Problem Statement
Given an array of integers, we need to find all pairs of numbers where one number is a power multiple of the other. Let's understand this with clear examples.
Examples
Example 1:
Input: [2, 4, 8, 16] Output: [[2, 4], [2, 8], [2, 16], [4, 8], [4, 16], [8, 16]]
Explanation: The power relationships are:
- 2² = 4, so (2, 4) is valid
- 2³ = 8, so (2, 8) is valid
- 2? = 16, so (2, 16) is valid
- 4² = 16, so (4, 16) is valid
Example 2:
Input: [3, 9, 27, 5] Output: [[3, 9], [3, 27], [9, 27]]
Explanation: 3² = 9, 3³ = 27, and 9 × 3 = 27, creating valid power relationships.
Approach: Brute Force with Power Check
The most straightforward approach is to check every pair of numbers to determine if one is a power of the other. We'll use logarithms to verify the power relationship efficiently.
Algorithm Steps
- Initialize an empty array to store valid pairs
- Use nested loops to check all possible pairs
- For each pair, check if one number is a power of the other
- Use logarithms to verify the power relationship
- Add valid pairs to the result array
Implementation
function isPowerOf(base, number) {
if (base === 1) {
return number === 1;
}
if (base === 0 || number <= 0) {
return false;
}
// Calculate log_base(number)
const logResult = Math.log(number) / Math.log(base);
// Check if the result is close to an integer
return Math.abs(logResult - Math.round(logResult)) < 1e-10;
}
function findPowerMultiplePairs(arr) {
const pairs = [];
const n = arr.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
// Check if arr[i] is a power of arr[j] or vice versa
if (isPowerOf(arr[i], arr[j]) || isPowerOf(arr[j], arr[i])) {
pairs.push([arr[i], arr[j]]);
}
}
}
return pairs;
}
// Example usage
const inputArray = [2, 4, 8, 16, 3];
const result = findPowerMultiplePairs(inputArray);
console.log("Input:", inputArray);
console.log("Power multiple pairs:", result);
Input: [ 2, 4, 8, 16, 3 ] Power multiple pairs: [ [ 2, 4 ], [ 2, 8 ], [ 2, 16 ], [ 4, 8 ], [ 4, 16 ], [ 8, 16 ] ]
How It Works
The isPowerOf(base, number) function determines if number is a power of base by:
- Calculating the logarithm: logbase(number) = ln(number) / ln(base)
- Checking if the result is very close to an integer value
- Using a small epsilon (1e-10) to handle floating-point precision issues
Alternative Example with Different Base
// Testing with powers of 3
const testArray = [1, 3, 9, 27, 81, 5];
const testResult = findPowerMultiplePairs(testArray);
console.log("Input:", testArray);
console.log("Power pairs:", testResult);
Input: [ 1, 3, 9, 27, 81, 5 ] Power pairs: [ [ 1, 3 ], [ 1, 9 ], [ 1, 27 ], [ 1, 81 ], [ 1, 5 ], [ 3, 9 ], [ 3, 27 ], [ 3, 81 ], [ 9, 27 ], [ 9, 81 ], [ 27, 81 ] ]
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time Complexity | O(n²) | Nested loops check all pairs |
| Space Complexity | O(k) | k is the number of valid pairs found |
Key Points
- The algorithm handles edge cases like base = 1 and negative numbers
- Floating-point precision is managed using an epsilon value
- The solution works for any positive integer bases and powers
- Time complexity is O(n²) due to the brute force approach
Conclusion
This JavaScript program effectively finds pairs where one number is a power multiple of another using logarithmic calculations. The brute force approach ensures all valid pairs are found, making it suitable for moderate-sized arrays where correctness is prioritized over optimization.
