C++ Program to Check if all array elements can be converted to pronic numbers by rotating digits Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given an array arr[] of size N, the task is to check if it is possible to convert all of the array elements to a pronic number by rotating the digits of array elements any number of times. Examples: Input: {321, 402, 246, 299} Output: True Explanation: arr[0] ? Right rotation once modifies arr[0] to 132 (= 11 × 12). arr[1] ? Right rotation once modifies arr[0] to 240 (= 15 × 16). arr[2] ? Right rotation twice modifies arr[2] to 462 (= 21 × 22). arr[3] ? Right rotation twice modifies arr[3] 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: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // function to check Pronic Number bool isPronic(int x) { for (int i = 0; i < (int)(sqrt(x)) + 1; i++) { // Checking Pronic Number // by multiplying consecutive // numbers if (x == i * (i + 1)) { return true; } } return false; } // Function to check if any permutation // of val is a pronic number or not bool checkRot(int val) { string temp = to_string(val); for (int i = 0; i < temp.length(); i++) { if (isPronic(stoi(temp)) == true) { return true; } temp = temp.substr(1, temp.size() - 1) + temp[0]; } return false; } // Function to check if all array // elements can be converted to // a pronic number or not bool check(int arr[], int N) { // Traverse the array for (int i = 0; i < N; i++) { // If current element // cannot be converted // to a pronic number if (checkRot(arr[i]) == false) { return false; } } return true; } // Driven Program int main() { // Given array int arr[] = { 321, 402, 246, 299 }; int N = sizeof(arr) / sizeof(arr[0]); // function call cout << (check(arr, N) ? "True" : "False"); return 0; } // This code is contributed by Kingash. Output: True Time Complexity: O(N3/2)Auxiliary Space: O(1) Please refer complete article on Check if all array elements can be converted to pronic numbers by rotating digits for more details! Comment More infoAdvertise with us Next Article C++ Program to Check if all array elements can be converted to pronic numbers by rotating digits K kartik Follow Improve Article Tags : Mathematical C++ Programs C++ DSA Arrays number-digits rotation array-rearrange Numbers +5 More Practice Tags : CPPArraysMathematicalNumbers Similar Reads C++ Program to Check if it is possible to make array increasing or decreasing by rotating the array Given an array arr[] of N distinct elements, the task is to check if it is possible to make the array increasing or decreasing by rotating the array in any direction.Examples: Input: arr[] = {4, 5, 6, 2, 3} Output: Yes Array can be rotated as {2, 3, 4, 5, 6}Input: arr[] = {1, 2, 4, 3, 5} Output: No 4 min read C++ Program to Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes". Examples: Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation:Â After 2 min read C++ Program to Check if a string can be obtained by rotating another string d places Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right). Examples: Input: str1 = "abcdefg", str2 = "cdefgab", d = 2 Output: Yes Rotate str1 2 places to the left. Input: str1 = "abcdefg", str 4 min read C++ Program to Check if two numbers are bit rotations of each other or not Given two positive integers x and y, check if one integer is obtained by rotating bits of other. Input constraint: 0 < x, y < 2^32 Bit Rotation: A rotation (or circular shift) is an operation similar to shift except that the bits that fall off at one end are put back to the other end.More info 3 min read C++ Program to Check if it is possible to sort the array after rotating it Given an array of size N, the task is to determine whether its possible to sort the array or not by just one shuffle. In one shuffle, we can shift some contiguous elements from the end of the array and place it in the front of the array.For eg: A = {2, 3, 1, 2}, we can shift {1, 2} from the end of t 3 min read Like