Find the length of largest subarray in which all elements are Autobiographical Numbers
Last Updated :
18 Sep, 2022
Given an array arr[] of integers, our task is to find the length of the largest subarray such that all the elements of the sub-array are Autobiographical Number.
An Autobiographical Number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on.
For example, 21200 has 2 zero, 1 one, 2 two and 0 three and 0 four.
Examples:
Input: arr[]={21200, 1, 1303, 1210, 2020}
Output: 2
Explanation:
Maximum length of subarray with all numbers as Autobiographical Number is {1210, 2020}.
Input: arr[]={100, 200, 300, 400, 1200, 500}
Output: 0
Explanation:
None of them are Autobiographical Number.
Approach:
To solve the problem mentioned above we have to follow the steps given below:
- Traverse the array from index 0 and initialize a max_length and current_length variable with 0.
- If the current element is an autobiographical number then increment current_length variable and continue, otherwise set current_length to 0.
- At each step, assign max_length as max_length = max(current_length, max_length). The final value of max_length will store the required result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isAutoBiographyNum( int number)
{
int count = 0, position, size, digit;
string NUM;
NUM = to_string(number);
size = NUM.length();
for ( int i = 0; i < size; i++) {
position = NUM[i] - '0' ;
count = 0;
for ( int j = 0; j < size; j++) {
digit = NUM[j] - '0' ;
if (digit == i)
count++;
}
if (position != count)
return false ;
}
return true ;
}
int checkArray( int arr[], int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++) {
if (isAutoBiographyNum(arr[i]))
current_length++;
else
current_length = 0;
max_length = max(max_length, current_length);
}
return max_length;
}
int main()
{
int arr[] = { 21200, 1, 1303, 1210, 2020 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << checkArray(arr, n) << "\n" ;
return 0;
}
|
Java
class GFG {
static boolean isAutoBiographyNum( int number)
{
int count = 0 , position, size, digit;
String NUM;
NUM = Integer.toString(number);
size = NUM.length();
for ( int i = 0 ; i < size; i++)
{
position = NUM.charAt(i) - '0' ;
count = 0 ;
for ( int j = 0 ; j < size; j++)
{
digit = NUM.charAt(j) - '0' ;
if (digit == i)
count++;
}
if (position != count)
return false ;
}
return true ;
}
static int checkArray( int arr[], int n)
{
int current_length = 0 ;
int max_length = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (isAutoBiographyNum(arr[i]) == true )
{
current_length++;
}
else
{
current_length = 0 ;
}
max_length = Math.max(max_length, current_length);
}
return max_length;
}
public static void main (String[] args)
{
int arr[] = { 21200 , 1 , 1303 , 1210 , 2020 };
int n = arr.length;
System.out.println(checkArray(arr, n));
}
}
|
Python3
def isAutoBiographyNum(number):
count = 0 ;
NUM = str (number);
size = len (NUM);
for i in range (size):
position = ord (NUM[i]) - ord ( '0' );
count = 0 ;
for j in range (size):
digit = ord (NUM[j]) - ord ( '0' );
if (digit = = i):
count + = 1 ;
if (position ! = count):
return False ;
return True ;
def checkArray(arr, n):
current_length = 0 ;
max_length = 0 ;
for i in range (n):
if (isAutoBiographyNum(arr[i])):
current_length + = 1 ;
else :
current_length = 0 ;
max_length = max (max_length,
current_length);
return max_length;
if __name__ = = "__main__" :
arr = [ 21200 , 1 , 1303 , 1210 , 2020 ];
n = len (arr);
print (checkArray(arr, n));
|
C#
using System;
class GFG {
static bool isAutoBiographyNum( int number)
{
int count = 0, position, size, digit;
string NUM;
NUM = number.ToString();
size = NUM.Length;
for ( int i = 0; i < size; i++)
{
position = NUM[i] - '0' ;
count = 0;
for ( int j = 0; j < size; j++)
{
digit = NUM[j] - '0' ;
if (digit == i)
count++;
}
if (position != count)
return false ;
}
return true ;
}
static int checkArray( int []arr, int n)
{
int current_length = 0;
int max_length = 0;
for ( int i = 0; i < n; i++)
{
if (isAutoBiographyNum(arr[i]) == true )
{
current_length++;
}
else
{
current_length = 0;
}
max_length = Math.Max(max_length,
current_length);
}
return max_length;
}
public static void Main ( string [] args)
{
int []arr = { 21200, 1, 1303, 1210, 2020 };
int n = arr.Length;
Console.WriteLine(checkArray(arr, n));
}
}
|
Javascript
<script>
function isAutoBiographyNum(number)
{
let count = 0, position, size, digit;
let NUM;
NUM = number.toString();
size = NUM.length;
for (let i = 0; i < size; i++) {
position = NUM[i].charCodeAt() - '0' .charCodeAt();
count = 0;
for (let j = 0; j < size; j++) {
digit = NUM[j].charCodeAt() - '0' .charCodeAt();
if (digit == i)
count++;
}
if (position != count)
return false ;
}
return true ;
}
function checkArray(arr, n)
{
let current_length = 0;
let max_length = 0;
for (let i = 0; i < n; i++) {
if (isAutoBiographyNum(arr[i]))
current_length++;
else
current_length = 0;
max_length = Math.max(max_length, current_length);
}
return max_length;
}
let arr = [ 21200, 1, 1303, 1210, 2020 ];
let n = arr.length;
document.write(checkArray(arr, n));
</script>
|
Time Complexity: O(n * log n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...