📜  最大连续自同构数

📅  最后修改于: 2022-05-13 01:57:46.953000             🧑  作者: Mango

最大连续自同构数

给定一个包含 N 个元素的数组。任务是找到给定数组中连续自守数的最大数量。
自守数:一个数称为自守数当且仅当它的平方以与数字本身相同的数字结束。

例如:

-> 76 is automorphic, since 76*76 = 5776(ends in 76)
-> 5 is automorphic, since 5*5 = 25(ends in 5)

例子:

Input :  arr[] = {22, 6, 1, 625, 2, 1, 9376}
Output : 3

Input : arr[] = {99, 42, 31, 1, 5}
Output : 2

方法:

  1. 使用名为 current_max 和 max_so_far 的两个变量遍历数组。用 0 初始化它们。
  2. 检查每个元素是否是自同构的。
    • 计算当前数字的平方。
    • 继续从当前数字及其平方的末尾提取和比较数字。
    • 如果发现任何不匹配,则该数字不是自同构的。
    • 否则,如果从当前数字中提取的所有数字都没有任何不匹配,则该数字是自同构的。
  3. 如果找到自同构数,则增加 current_max 并将其与 max_so_far 进行比较
  4. 如果 current_max 大于 max_so_far,则将 max_so_far 分配给 current_max
  5. 每次找到非自构元素时,将 current_max 重置为 0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to check Automorphic number
bool isAutomorphic(int N)
{
    // Store the square
    int sq = N * N;
 
    // Start Comparing digits
    while (N > 0) {
 
        // Return false, if any digit of N doesn't
        // match with its square's digits from last
        if (N % 10 != sq % 10)
            return false;
 
        // Reduce N and square
        N /= 10;
        sq /= 10;
    }
 
    return true;
}
 
// Function to find the length of the maximum
// contiguous subarray of automorphic numbers
int maxAutomorphicSubarray(int arr[], int n)
{
    int current_max = 0, max_so_far = 0;
 
    for (int i = 0; i < n; i++) {
 
        // check if element is non automorphic
        if (isAutomorphic(arr[i]) == false)
            current_max = 0;
 
        // If element is automorphic, than update
        // current_max and max_so_far accordingly.
        else {
            current_max++;
            max_so_far = max(current_max, max_so_far);
        }
    }
 
    return max_so_far;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 3, 2, 5, 1, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxAutomorphicSubarray(arr, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to check Automorphic number
static boolean isAutomorphic(int N)
{
    // Store the square
    int sq = N * N;
 
    // Start Comparing digits
    while (N > 0)
    {
 
        // Return false, if any digit of N doesn't
        // match with its square's digits from last
        if (N % 10 != sq % 10)
            return false;
 
        // Reduce N and square
        N /= 10;
        sq /= 10;
    }
 
    return true;
}
 
// Function to find the length of the maximum
// contiguous subarray of automorphic numbers
static int maxAutomorphicSubarray(int []arr, int n)
{
    int current_max = 0, max_so_far = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // check if element is non automorphic
        if (isAutomorphic(arr[i]) == false)
            current_max = 0;
 
        // If element is automorphic, than update
        // current_max and max_so_far accordingly.
        else
        {
            current_max++;
            max_so_far = Math.max(current_max,
                                  max_so_far);
        }
    }
 
    return max_so_far;
}
 
// Driver Code
public static void main(String[] args)
{
    int []arr = { 0, 3, 2, 5, 1, 9 };
    int n = arr.length;
 
    System.out.println(maxAutomorphicSubarray(arr, n));
}
}
 
// This code is contributed by Code_Mech.


Python3
# Python3 implementation of the approach
 
# Function to check Automorphic number
def isAutomorphic(N) :
 
    # Store the square
    sq = N * N;
 
    # Start Comparing digits
    while (N > 0) :
 
        # Return false, if any digit of N doesn't
        # match with its square's digits from last
        if (N % 10 != sq % 10) :
            return False;
 
        # Reduce N and square
        N //= 10;
        sq //= 10;
 
    return True;
 
# Function to find the length of the maximum
# contiguous subarray of automorphic numbers
def maxAutomorphicSubarray(arr, n) :
     
    current_max = 0; max_so_far = 0;
 
    for i in range(n) :
 
        # check if element is non automorphic
        if (isAutomorphic(arr[i]) == False) :
            current_max = 0;
 
        # If element is automorphic, than update
        # current_max and max_so_far accordingly.
        else :
            current_max += 1;
            max_so_far = max(current_max,
                             max_so_far);
 
    return max_so_far;
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 0, 3, 2, 5, 1, 9 ];
    n = len(arr) ;
 
    print(maxAutomorphicSubarray(arr, n));
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to check Automorphic number
static bool isAutomorphic(int N)
{
    // Store the square
    int sq = N * N;
 
    // Start Comparing digits
    while (N > 0)
    {
 
        // Return false, if any digit of N doesn't
        // match with its square's digits from last
        if (N % 10 != sq % 10)
            return false;
 
        // Reduce N and square
        N /= 10;
        sq /= 10;
    }
 
    return true;
}
 
// Function to find the length of the maximum
// contiguous subarray of automorphic numbers
static int maxAutomorphicSubarray(int []arr, int n)
{
    int current_max = 0, max_so_far = 0;
 
    for (int i = 0; i < n; i++)
    {
 
        // check if element is non automorphic
        if (isAutomorphic(arr[i]) == false)
            current_max = 0;
 
        // If element is automorphic, than update
        // current_max and max_so_far accordingly.
        else
        {
            current_max++;
            max_so_far = Math.Max(current_max, max_so_far);
        }
    }
 
    return max_so_far;
}
 
// Driver Code
static void Main()
{
    int []arr = { 0, 3, 2, 5, 1, 9 };
    int n = arr.Length;
 
    Console.WriteLine(maxAutomorphicSubarray(arr, n));
 
}
}
 
// This code is contributed by mits


PHP
 0)
    {
 
        // Return false, if any digit of N doesn't
        // match with its square's digits from last
        if ($N % 10 != $sq % 10)
            return false;
 
        // Reduce N and square
        $N = (int)($N / 10);
        $sq = (int)($sq / 10);
    }
 
    return true;
}
 
// Function to find the length of the maximum
// contiguous subarray of automorphic numbers
function maxAutomorphicSubarray($arr, $n)
{
    $current_max = 0; $max_so_far = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
 
        // check if element is non automorphic
        if (isAutomorphic($arr[$i]) == false)
            $current_max = 0;
 
        // If element is automorphic, than update
        // current_max and max_so_far accordingly.
        else
        {
            $current_max++;
            $max_so_far = max($current_max,
                              $max_so_far);
        }
    }
 
    return $max_so_far;
}
 
// Driver Code
$arr = array(0, 3, 2, 5, 1, 9 );
$n = sizeof($arr);
 
echo(maxAutomorphicSubarray($arr, $n));
 
// This code is contributed by Code_Mech.
?>


Javascript


输出:
2