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 to Check if all array elements can be converted to pronic numbers by rotating digits
Pronic numbers are also known as rectangular numbers, the pronic numbers are numbers that are multiples of two consecutive numbers. We will be given an array of integers and we can rotate the digits in any direction for a certain number of times to get all combinations. For any combination produced by rotating the digit if each array element can be converted into the pronic number then we will print true otherwise false.
What are Pronic Numbers?
First, let us discuss pronic numbers: pronic numbers are the numbers that are the product of two consecutive numbers.
Mathematically saying, if we have integer x and its next consecutive number will be x+1 and let the number k is the product of both of them, that means: k = (x)*(x+1). A few examples of the Pronic Numbers are:
0 is the product of 0 and 1.
2 is the product of 1 and 2.
6 is the product of 2 and 3.
12 is the product of 3 and 4.
Some more examples: 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, etc.
Problem Example
Let us assume we are given an array:
{ 21, 65, 227, 204, 2}
Output: Yes
Explanation:
For the zeroth index: 21, after one rotation can be converted into 12 which is the multiplication of 3 and 4, and hence a pronic number.
For the first index: 65, after one rotation can be converted into 56 which is the multiplication of the 7 and 8, and hence a pronic number.
For the second index: 227, after one rotation can be converted into 272 which is a pronic number.
Similarly, 204 to 420 and 2 itself is a pronic number.
Solution Approach
We have seen the example for the code, now let us move to the steps:
First, we will define a function to rotate the given number. An integer will be passed as the parameter and that will be converted to the string.
Using the substring method, we will rotate the string to its right and then again convert back in into the number and return it.
We will define the pronic function to check whether the current number is pronic or not.
We will find the floor of the square root of the current number and multiply it with its consecutive number to find that current number is pronic or not.
We will define a function to find the number of digits in the current number by converting it into the string.
In the main function, we will traverse over the array and for each element we will rotate it its length number of times or until we find the pronic number.
If we found any number after all iterations that is not a pronic and we are not able to convert it into pronic number then we will print no other wise yes.
Implementation
In the below example, we check if all array elements can be converted to pronic numbers by rotating digits. The input and expected output are given below.
Input: Array = [21, 65, 227, 204, 2]
Expected Output: Yes
// function to rotate the digits
function rotate(num){
// converting integer to string
var str = num.toString();
// putting first index value to last
str = str.substring(1) + str.substring(0,1);
// converting back string to integer
num = parseInt(str);
return num;
}
// function to check whether current number if pronic number or not
function isPronic(num){
// getting square root of the current number
var cur = Math.sqrt(num);
// taking floor of cur
cur = Math.floor(cur);
if(cur*(cur+1) == num) {
return true;
}
else {
return false;
}
}
// function to find the length of the current integer
function number_length(num){
var str = num.toString()
var len = str.length;
return len;
}
// function to check whether array is pronic or not
function check(arr){
var len = arr.length;
for(var i = 0; i < len; i++){
// getting length of the current number
var cur = number_length(arr[i]);
while(cur--){
if(isPronic(arr[i])){
break;
}
arr[i] = rotate(arr[i]);
}
if(isPronic(arr[i]) == false){
return false;
}
}
return true;
}
var arr = [21, 65, 227, 204, 2]
console.log("Array:", JSON.stringify(arr))
if(check(arr)){
console.log("The elements of array can be converted to pronic numbers.");
}
else{
console.log("The elements of array can't be converted to pronic numbers.");
}
Array: [21,65,227,204,2] The elements of array can be converted to pronic numbers.
Alternative Approach
Here's a more compact version using modern JavaScript features:
function canAllBePronic(arr) {
// Check if a number is pronic
const isPronic = (num) => {
const sqrt = Math.floor(Math.sqrt(num));
return sqrt * (sqrt + 1) === num;
};
// Get all rotations of a number
const getRotations = (num) => {
const str = num.toString();
const rotations = [];
for (let i = 0; i < str.length; i++) {
rotations.push(parseInt(str.substring(i) + str.substring(0, i)));
}
return rotations;
};
// Check if any rotation of the number is pronic
const canBePronic = (num) => {
return getRotations(num).some(rotation => isPronic(rotation));
};
return arr.every(num => canBePronic(num));
}
// Test with sample data
const testArray = [21, 65, 227, 204, 2];
console.log("Input Array:", testArray);
console.log("Can all be converted to pronic?", canAllBePronic(testArray));
// Test with individual numbers to show rotations
testArray.forEach(num => {
const str = num.toString();
const rotations = [];
for (let i = 0; i < str.length; i++) {
rotations.push(parseInt(str.substring(i) + str.substring(0, i)));
}
console.log(`${num} rotations: ${rotations.join(", ")}`);
});
Input Array: [21,65,227,204,2] Can all be converted to pronic? true 21 rotations: 21, 12 65 rotations: 65, 56 227 rotations: 227, 272, 722 204 rotations: 204, 042, 420 2 rotations: 2
Time and Space Complexity
The time complexity of the above code is O(N × M), where N is the size of the array and M is the average number of digits in each number. Here we are getting an extra factor for rotating each number and checking if it's pronic.
The space complexity of the above code is O(M) for storing the rotated string representations, where M is the maximum number of digits in any number.
Conclusion
In this tutorial, we have implemented a JavaScript program to check if all array elements can be converted to pronic numbers by rotating their digits. The solution involves checking each possible rotation of every number to see if any rotation forms a pronic number, returning true only if all elements can be converted.
