- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

## 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.

1 is the product of 1 and 2.

6 is the product of 2 and 3.

-> 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, etc.

### Examples

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.

## 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.

### Example

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."); }

### Output

Array: [21,65,227,204,2] The elements of array can be converted to pronic numbers.

### Time and Space Complexity

The time complexity of the above code is O(N), where N is the size of the array. Here we are getting an extra factor of log of size of number for both traversing over the array and getting its square root but as the maximum length of the given integer is very less, makes no effect over the linear time complexity.

The space complexity of the above code is constant or O(1), as we are not using any extra space here.

## Conclusion

In this tutorial, we have implemented a JavaScript program to find weather we can convert each element of the array to the pronic number by just rotating its digits in the left or right direction. We have defined certain functions to rotate the numbers, check whether they are pronic or not, and get the number of digits of the number. The time complexity of the above code is O(N) and the space complexity of the above code is O(1).