Java 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: Java // Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // function to check Pronic Number static boolean isPronic(int x) { for (int i = 0; i < (int)(Math.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 static boolean checkRot(int val) { String temp = Integer.toString(val); for (int i = 0; i < temp.length(); i++) { if (isPronic(Integer.parseInt(temp)) == true) { return true; } temp = temp.substring(1) + temp.charAt(0); } return false; } // Function to check if all array // elements can be converted to // a pronic number or not static boolean 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; } // Driver code public static void main(String[] args) { // Given array int arr[] = { 321, 402, 246, 299 }; int N = arr.length; // Function call System.out.println( (check(arr, N) ? "True" : "False")); } } // 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 Java Program to Check if all array elements can be converted to pronic numbers by rotating digits K kartik Follow Improve Article Tags : Java Mathematical Java Programs DSA Arrays number-digits rotation array-rearrange Numbers +5 More Practice Tags : ArraysJavaMathematicalNumbers Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Like