Open In App

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

Last Updated : 22 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
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.


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.


Python3




# Python implementation of
# the above approach
 
# Function to check if a number
# is a pronic number or not
def isPronic(n):
 
  for i in range(int(n**(1 / 2)) + 1):
    if i * (i + 1) == n:
      return True
 
  return False
 
# Function to check if any permutation
# of n is a pronic number or not
def checkRot(n):
 
  temp = str(n)
 
  for i in range(len(temp)):
 
    if isPronic(int(temp)):
      return True
 
    temp = temp[1:]+temp[0]
 
  return False
 
# Function to check if all array
# elements can be converted to
# a pronic number or not
def check(arr):
 
  # Traverse the array
  for i in arr:
 
    # If current element
    # cannot be converted
    # to a pronic number
    if not checkRot(i):
      return False
  return True
 
# Driver Code
arr = [ 321, 402, 246, 299 ]
print(check(arr))


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
  // function to check Pronic Number
  static bool isPronic(int x)
  {
    int val  = (int)Math.Sqrt(x);
    val += 1;
    for (int i = 0; i < val; 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 bool checkRot(int val)
  {
 
    string temp = val.ToString();
    for (int i = 0; i < temp.Length; i++)
    {
      int a = Int32.Parse(temp);
      if (isPronic(a) == true)
      {
        return true;
      }
      temp = temp.Substring(1, temp.Length - 1) + temp[0];
    }
    return false;
  }
 
  // Function to check if all array
  // elements can be converted to
  // a pronic number or not
  static 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
  public static void Main()
  {
 
    // Given array
    int []arr = { 321, 402, 246, 299 };
    int N = arr.Length;
 
    // function call
    Console.WriteLine(check(arr, N) ? "True" : "False");
  }
}
 
// This code is contributed by ipg2016107.


Javascript




<script>
 
// Javascript program for the above approach
 
// function to check Pronic Number
function isPronic(x)
{
    for (var i = 0; i < parseInt(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
function checkRot(val)
{
 
    var temp = (val).toString();
    for (var i = 0; i < temp.length; i++)
    {
        if (isPronic(parseInt(temp)) == true)
        {
            return true;
        }
        temp = temp.substring(1) + temp[0];
    }
    return false;
}
 
// Function to check if all array
// elements can be converted to
// a pronic number or not
function check(arr, N)
{
 
    // Traverse the array
    for (var 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
 
// Given array
var arr = [ 321, 402, 246, 299 ]
var N = arr.length;
 
// function call
document.write(check(arr, N) ? "True" : "False");
 
// This code is contributed by noob2000.
</script>


Output: 

True

 

Time Complexity: O(N * K * sqrt(val)), where N is the number of elements in the input array, K is the maximum length of a single element in the input array, and val is the maximum value of any element in the input array.
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

C++ Program to 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 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 mod
3 min read
Java Program to 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 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 mod
3 min read
Python3 Program to 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 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 mod
2 min read
Javascript Program to 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 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 mod
3 min read
Check if all array elements can be converted to K using given operations
Given an integer array arr of size N and an integer K, the task is to make all elements of the array equal to K using the following operations: Choose an arbitrary subarray [l....r] of the input arrayReplace all values of this subarray equal to the [((r - l) + 2) / 2]th value in sorted subarray [l...r] Examples: Input: arr [ ] = {5, 4, 3, 1, 2, 6,
9 min read
Check if an array can be converted to another given array by swapping pairs of unequal elements
Given two arrays arr1[] and arr2[] of size N, consisting of binary integers, the task is to check if arr1[] can be converted to arr2[] by swapping any pair of array elements (arr1[i], arr1[j]) such that i &lt; j and arr1[i] is 1 and arr1[j] is 0 (any number of times). If it is possible to do so, then print "Yes". Otherwise, print "No". Examples: In
11 min read
Check if a Zero Array can be converted to a given Array by incrementing some elements with X^i
Given an array B, and two integers N, X (2 ≤ X ≤ 100). Initially, array arr has all its values as zero. The task is to check if array arr[] can be made equal to the given array B after performing the given operations: Choose a given position (p) (1 ≤ p ≤ n)then increase arr[p] by Xi where i is any positive integer.Any element can be chosen to be in
7 min read
Check if any row of the matrix can be converted to the elements present in the target row
Given a matrix mat[][] of dimensions N*M and an array target[] of M integers, the task is to check whether any row of the matrix can be made equal to the array target[] by choosing any two rows of the matrix and update each element of any of the row to the maximum of elements at corresponding indices of the two chosen row. If it is possible to do s
7 min read
Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
Given two binary matrices, A[][] and B[][] of N×M. In a single operation, one can choose a sub-matrix (min of 2 rows and 2c columns) and change the parity of the corner elements i.e. 1 can be changed to a 0, and 0 can be changed to a 1. The task is to check if matrix A can be converted to B using any number of operations. Examples: Input: A[][] = {
8 min read
Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column
Given two matrices A[][] and B[][] of size M × N and an integer X, the task is to check if it is possible to convert matrix A[][] to matrix B[][] by adding any value to X consecutive cells in the same row or same column any number of times (possibly zero). Examples: Input: A[][] = { {0, 0}, {0, 0}}, B[][] = {{1, 2}, {0, 1}}, X = 2Output: YesExplana
9 min read