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
Approach:
- Traverse the array with two variables named current_max and max_so_far. Initialize both of them with 0.
- 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.
- If a automorphic number is found then increment current_max and compare it with max_so_far
- If current_max is greater than max_so_far, then assign max_so_far with current_max
- Every time a non automorphic element is found, reset current_max to 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAutomorphic( int N)
{
int sq = N * N;
while (N > 0) {
if (N % 10 != sq % 10)
return false ;
N /= 10;
sq /= 10;
}
return true ;
}
int maxAutomorphicSubarray( int arr[], int n)
{
int current_max = 0, max_so_far = 0;
for ( int i = 0; i < n; i++) {
if (isAutomorphic(arr[i]) == false )
current_max = 0;
else {
current_max++;
max_so_far = max(current_max, max_so_far);
}
}
return max_so_far;
}
int main()
{
int arr[] = { 0, 3, 2, 5, 1, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxAutomorphicSubarray(arr, n);
return 0;
}
|
Java
class GFG
{
static boolean isAutomorphic( int N)
{
int sq = N * N;
while (N > 0 )
{
if (N % 10 != sq % 10 )
return false ;
N /= 10 ;
sq /= 10 ;
}
return true ;
}
static int maxAutomorphicSubarray( int []arr, int n)
{
int current_max = 0 , max_so_far = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (isAutomorphic(arr[i]) == false )
current_max = 0 ;
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
return max_so_far;
}
public static void main(String[] args)
{
int []arr = { 0 , 3 , 2 , 5 , 1 , 9 };
int n = arr.length;
System.out.println(maxAutomorphicSubarray(arr, n));
}
}
|
Python3
def isAutomorphic(N) :
sq = N * N;
while (N > 0 ) :
if (N % 10 ! = sq % 10 ) :
return False ;
N / / = 10 ;
sq / / = 10 ;
return True ;
def maxAutomorphicSubarray(arr, n) :
current_max = 0 ; max_so_far = 0 ;
for i in range (n) :
if (isAutomorphic(arr[i]) = = False ) :
current_max = 0 ;
else :
current_max + = 1 ;
max_so_far = max (current_max,
max_so_far);
return max_so_far;
if __name__ = = "__main__" :
arr = [ 0 , 3 , 2 , 5 , 1 , 9 ];
n = len (arr) ;
print (maxAutomorphicSubarray(arr, n));
|
C#
using System;
class GFG
{
static bool isAutomorphic( int N)
{
int sq = N * N;
while (N > 0)
{
if (N % 10 != sq % 10)
return false ;
N /= 10;
sq /= 10;
}
return true ;
}
static int maxAutomorphicSubarray( int []arr, int n)
{
int current_max = 0, max_so_far = 0;
for ( int i = 0; i < n; i++)
{
if (isAutomorphic(arr[i]) == false )
current_max = 0;
else
{
current_max++;
max_so_far = Math.Max(current_max, max_so_far);
}
}
return max_so_far;
}
static void Main()
{
int []arr = { 0, 3, 2, 5, 1, 9 };
int n = arr.Length;
Console.WriteLine(maxAutomorphicSubarray(arr, n));
}
}
|
PHP
<?php
function isAutomorphic( $N )
{
$sq = $N * $N ;
while ( $N > 0)
{
if ( $N % 10 != $sq % 10)
return false;
$N = (int)( $N / 10);
$sq = (int)( $sq / 10);
}
return true;
}
function maxAutomorphicSubarray( $arr , $n )
{
$current_max = 0; $max_so_far = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if (isAutomorphic( $arr [ $i ]) == false)
$current_max = 0;
else
{
$current_max ++;
$max_so_far = max( $current_max ,
$max_so_far );
}
}
return $max_so_far ;
}
$arr = array (0, 3, 2, 5, 1, 9 );
$n = sizeof( $arr );
echo (maxAutomorphicSubarray( $arr , $n ));
?>
|
Javascript
<script>
function isAutomorphic(N)
{
var sq = N * N;
while (N > 0)
{
if (N % 10 != sq % 10)
return false ;
N = parseInt(N / 10);
sq = parseInt(sq / 10);
}
return true ;
}
function maxAutomorphicSubarray(arr, n)
{
var current_max = 0, max_so_far = 0;
for ( var i = 0; i < n; i++)
{
if (isAutomorphic(arr[i]) == false )
current_max = 0;
else
{
current_max++;
max_so_far = Math.max(current_max,
max_so_far);
}
}
return max_so_far;
}
var arr = [ 0, 3, 2, 5, 1, 9 ];
var n = arr.length;
document.write(maxAutomorphicSubarray(arr, n));
</script>
|
Time Complexity: O(n * log10m), where n is the size of the given array and m is the maximum element in the array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...