Find the length of largest subarray in which all elements are Autobiographical Numbers

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the length of the
// largest subarray whose every element is an
// Autobiographical Number
  
#include <bits/stdc++.h>
using namespace std;
  
// function to check number is autobiographical
bool isAutoBiographyNum(int number)
{
  
    int count = 0, position, size, digit;
    string NUM;
  
    // Convert integer to string
    NUM = to_string(number);
    size = NUM.length();
  
    // Iterate for every digit to check 
    // for their total count
    for (int i = 0; i < size; i++) {
        position = NUM[i] - '0';
        count = 0;
  
        // Check occurrence of every number 
        // and count them
        for (int j = 0; j < size; j++) {
            digit = NUM[j] - '0';
            if (digit == i)
                count++;
        }
  
        // Check if any position mismatches with 
        // total count them return with false 
        // else continue with loop
        if (position != count)
            return false;
    }
  
    return true;
}
  
// Function to return the length of the
// largest subarray whose every
// element is a Autobiographical number
int checkArray(int arr[], int n)
{
  
    int current_length = 0;
    int max_length = 0;
  
    // Utility function which checks every element 
    // of array for Autobiographical number
    for (int i = 0; i < n; i++) {
  
        // Check if element arr[i] is an 
        // Autobiographical number
        if (isAutoBiographyNum(arr[i]))
            // Increment the current length
            current_length++;
  
        else
            current_length = 0;
  
        // Update max_length value
        max_length = max(max_length, current_length);
    }
  
    // Return the final result
    return max_length;
}
  
// Driver code
int main()
{
    int arr[] = { 21200, 1, 1303, 1210, 2020 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << checkArray(arr, n) << "\n";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the length of the 
// largest subarray whose every element is  
// an autobiographical number 
class GFG {
  
// Function to check number is autobiographical 
static boolean isAutoBiographyNum(int number) 
      
    int count = 0, position, size, digit; 
    String NUM; 
      
    // Convert integer to string 
    NUM = Integer.toString(number); 
    size = NUM.length(); 
      
    // Iterate for every digit to check 
    // for their total count 
    for(int i = 0; i < size; i++)
    
       position = NUM.charAt(i) - '0'
       count = 0
         
       // Check occurrence of every number 
       // and count them 
       for(int j = 0; j < size; j++)
       
          digit = NUM.charAt(j) - '0'
          if (digit == i)
              count++; 
       
         
       // Check if any position mismatches with 
       // total count them return with false 
       // else continue with loop 
       if (position != count) 
           return false
    
      
    return true
      
// Function to return the length of the 
// largest subarray whose every 
// element is a Autobiographical number 
static int checkArray(int arr[], int n) 
    int current_length = 0
    int max_length = 0
      
    // Utility function which checks every element 
    // of array for autobiographical number 
    for(int i = 0; i < n; i++)
    
         
       // Check if element arr[i] is an 
       // autobiographical number 
       if (isAutoBiographyNum(arr[i]) == true)
       
             
           // Increment the current length 
           current_length++;
       }
       else
       {
           current_length = 0;
       
  
       // Update max_length value 
       max_length = Math.max(max_length, current_length); 
    
      
    // Return the final result 
    return max_length; 
      
// Driver code 
public static void main (String[] args)
    int arr[] = { 21200, 1, 1303, 1210, 2020 }; 
    int n = arr.length; 
      
    System.out.println(checkArray(arr, n)); 
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the length of the 
# largest subarray whose every element is an 
# autobiographical number 
  
# Function to check number is autobiographical 
def isAutoBiographyNum(number):
  
    count = 0
  
    # Convert integer to string 
    NUM = str(number); 
    size = len(NUM); 
  
    # Iterate for every digit to check 
    # for their total count 
    for i in range(size): 
        position = ord(NUM[i]) - ord('0'); 
        count = 0
  
        # Check occurrence of every number 
        # and count them 
        for j in range(size):
              
            digit = ord(NUM[j]) - ord('0'); 
            if (digit == i):
                count += 1
  
        # Check if any position mismatches with 
        # total count them return with false 
        # else continue with loop 
        if (position != count):
            return False
  
    return True
  
# Function to return the length of the 
# largest subarray whose every 
# element is a autobiographical number 
def checkArray(arr, n): 
  
    current_length = 0
    max_length = 0
  
    # Utility function which checks every element 
    # of array for autobiographical number 
    for i in range(n): 
  
        # Check if element arr[i] is an 
        # autobiographical number 
        if (isAutoBiographyNum(arr[i])):
              
            # Increment the current length 
            current_length += 1
        else:
            current_length = 0
  
        # Update max_length value 
        max_length = max(max_length, 
                         current_length); 
  
    # Return the final result 
    return max_length; 
  
# Driver code 
if __name__ == "__main__"
  
    arr = [ 21200, 1, 1303, 1210, 2020 ]; 
    n = len(arr); 
  
    print(checkArray(arr, n)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the length of the 
// largest subarray whose every element  
// is an autobiographical number
using System;
  
class GFG {
      
// Function to check number is autobiographical 
static bool isAutoBiographyNum(int number) 
    int count = 0, position, size, digit; 
    string NUM; 
          
    // Convert integer to string 
    NUM = number.ToString(); 
    size = NUM.Length; 
          
    // Iterate for every digit to check 
    // for their total count 
    for(int i = 0; i < size; i++)
    
       position = NUM[i] - '0'
       count = 0; 
         
       // Check occurrence of every number 
       // and count them 
       for(int j = 0; j < size; j++)
       
          digit = NUM[j] - '0'
          if (digit == i)
              count++; 
       
         
       // Check if any position mismatches  
       // with total count them return with  
       // false else continue with loop 
       if (position != count) 
           return false
    
    return true
          
// Function to return the length of the 
// largest subarray whose every element
// is a autobiographical number 
static int checkArray(int []arr, int n) 
    int current_length = 0; 
    int max_length = 0; 
          
    // Utility function which checks every element 
    // of array for autobiographical number 
    for(int i = 0; i < n; i++)
    
         
       // Check if element arr[i] is an 
       // autobiographical number 
       if (isAutoBiographyNum(arr[i]) == true)
       
             
           // Increment the current length 
           current_length++;
       }
       else
       {
           current_length = 0;
       
         
       // Update max_length value 
       max_length = Math.Max(max_length, 
                             current_length); 
    
      
    // Return the final result 
    return max_length; 
          
// Driver code 
public static void Main (string[] args)
    int []arr = { 21200, 1, 1303, 1210, 2020 }; 
    int n = arr.Length; 
          
    Console.WriteLine(checkArray(arr, n)); 
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

2

Time Complexity: O(n * log n)

competitive-programming-img




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.



Improved By : AnkitRai01, nidhi_biet