Saturday, 23 Oct 2021

# Check if all array elements can be converted to pronic numbers by rotating digits

Check if all array elements can be converted to pronic numbers by rotating digits
Given an array arr[] of size N, the task is to check if it is possible to convert all of array elements to a pronic number by rotating the digits of array elements any number of times.
Examples:

Input: {321, 402, 246, 299} Output: TrueExplanation:arr → Right rotation once modifes arr to 132 (= 11 × 12).arr → Right rotation once modifes arr to 240 (= 15 × 16).arr → Right rotation twice modifies arr to 462 (= 21 × 22).arr → Right rotation twice modifies arr to 992 (= 31 × 32).
Input: {433, 653, 402, 186}Output: False

Approach: Follow the steps below to solve the problem:Traverse the array and check for each array element, whether it is possible to convert it to a pronic number.
For each array element, apply all the possible rotations and check after each rotation, whether the generated number is pronic or not.
If it is not possible to convert any array element to a pronic number, print “False”.
Otherwise, print “True”.
Below is the implementation of the above approach:

Python3

def isPronic(n):

for i in range(int(n**(1 / 2)) + 1):
if i * (i + 1) == n:
return True

return False

def checkRot(n):

temp = str(n)

for i in range(len(temp)):

if isPronic(int(temp)):
return True

temp = temp[1:]+temp

return False

def check(arr):

for i in arr:

if not checkRot(i):
return False
return True

arr = [ 321, 402, 246, 299 ]
print(check(arr))

Output:

True

Time Complexity: O(N3/2)Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.