# Maximum number of continuous Automorphic numbers

Given an array of N elements. The task is to find the maximum number of the contiguous automorphic numbers in the given array.

Automorphic Numbers: A number is called Automorphic number if and only if its square ends in the same digits as the number itself.

For Example:

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

Examples:

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

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Traverse the array with two variables named current_max and max_so_far. Initialize both of them with 0.
2. Check at each element if it is automorphic.
• Calculate the square of current number.
• Keep extracting and comparing digits from the end of both the current number and its square.
• If any mismatch is found, then the number is not automorphic.
• Otherwise if all of the digits from the current number is extracted without any mismatch, then the number is automorphic.
3. If a automorphic number is found then increment current_max and compare it with max_so_far
4. If current_max is greater than max_so_far, then assign max_so_far with current_max
5. Every time a non automorphic element is found, reset current_max to 0.

Below is the implementation of the above approach:

## 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); ` ` `  `    ``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. ` `?> `

Output:

```2
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.