# 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 such that one number is a power multiple of the other. In this tutorial, we will be discussing how to write a JavaScript program to solve this problem. We will be exploring various approaches and algorithms to find these pairs efficiently. Additionally, we will also be discussing the time and space complexity of these algorithms to provide a comprehensive understanding of the topic.

So, let's dive in and learn how to write a JavaScript program to find pairs of integers where one is a power multiple of the other.

## Problem Statement

Given an array of integers, we have to find pairs of numbers where one number is a power multiple of the other. Let’s understand this with some examples.

### Sample Examples

Example 1

Input: const arr = [2, 4, 6, 8, 10];
Output: [ [2, 8], [4, 8], [2, 4] ]


Explanation − In the given input array, the pairs of numbers that satisfy the condition of one number being a power multiple of the other are as follows −

2 and 8: 2 is a power multiple of 8 (2^3 = 8)
4 and 8: 4 is a power multiple of 8 (2^2 = 4 and 2^3 = 8)
2 and 4: 2 is a power multiple of 4 (2^1 = 4)


Hence, the program should return these pairs in an array.

Example 2

Input: const arr = [3, 9, 27, 81, 5];
Output: [ [3, 9], [9, 27], [27, 81] ]


Explanation − In the given input array, the pairs of numbers that satisfy the condition of one number being a power multiple of the other are as follows −

3 and 9: 3 is a power multiple of 9 (3^2 = 9)
9 and 27: 9 is a power multiple of 27 (3^2 = 9 and 3^3 = 27)
27 and 81: 27 is a power multiple of 81 (3^3 = 27 and 3^4 = 81)


Hence, the program should return these pairs in an array.

Now, let's explore various approaches and algorithms to find pairs of numbers where one number is a power multiple of the other efficiently −

## Approach 1: Brute Force

One of the simplest approaches to finding pairs of numbers where one number is a power multiple of the other is to use a nested loop. In this approach, we can iterate through all the possible pairs of numbers in the array and check if one number is a power multiple of the other. This approach has a time complexity of O(n^2) since we are checking all possible pairs.

## Approach 2: Using Hashing

Another approach to solving this problem is to use hashing. In this approach, we can create a hash table to store the numbers in the array. Then, we can iterate through the array and check if each number has a corresponding power multiple in the hash table. This approach has a time complexity of O(n) since we are iterating through the array only once.

## Approach 3: Sorting and Binary Search

We can also solve this problem by sorting the array and using binary search to find the power multiple of each number. In this approach, we first sort the array and then iterate through it to find the power multiple of each number using binary search. This approach has a time complexity of O(n log n) since we are sorting the array and using binary search.

These are some of the approaches that can be used to find pairs of numbers where one number is a power multiple of the other. The choice of approach depends on the size of the input array and the time complexity requirements of the problem. So, in this tutorial, we are going to use the Brute Force algorithm to solve the problem using Javascript.

## Brute Force Algorithm

Input − An array of integers arr of size n.

Output − An array of pairs of numbers where one number is a power multiple of the other.

• Initialize an empty array of pairs to store the resulting pairs.

• Iterate through all possible pairs of numbers in the array arr using two nested loops

• For each pair, check if one number is a power multiple of the other −

• Use the modulo operator % to check if the remainder of the division of one number by the other is zero.

• If the remainder is zero, it means one number is a power multiple of the other.

• Add the pair of numbers to the pairs array.

• Return the pairs array containing all the pairs where one number is a power multiple of the other.

Now, let’s understand the implementation of the above algorithm with the help of an example where we implement this algorithm using Javascript.

## Example

Implementation of the Above Algorithm

The program uses a brute force approach to find pairs of numbers where one number is a power multiple of the other. It iterates through all possible pairs of numbers in the input array using nested loops, and checks if one number is a power multiple of the other using the modulo operator. If a pair satisfies this condition, it is added to the resulting pairs array. However, this approach has a time complexity of O(n^2), which may not be efficient for large input arrays.

Input: [2, 4, 6, 8, 10]

Expected Output: [ [ 2, 4 ], [ 2, 6 ], [ 2, 8 ], [ 2, 10 ], [ 4, 8 ] ]

function findPowerMultiplePairs(arr) {
const pairs = [];
const n = arr.length;

// Iterate through all possible pairs of numbers
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {

// Check if one number is a power multiple of the other
if ((arr[i] % arr[j] === 0) || (arr[j] % arr[i] === 0)) {
pairs.push([arr[i], arr[j]]);
}
}
}
return pairs;
}

// Example usage:
const inputArray = [2, 4, 6, 8, 10];
const result = findPowerMultiplePairs(inputArray);
console.log(result);


## Conclusion

So, we have learned a JavaScript program for finding pairs of numbers where one number is a power multiple of the other. We discussed a brute force approach that involves iterating through all possible pairs of numbers and checking for the power multiple conditions. Overall, understanding the problem and choosing the right approach is essential for solving such programming challenges effectively.

Updated on: 02-May-2023

54 Views