Find the length of largest subarray in which all elements are Autobiographical Numbers | C ++ program to find the length of the largest subarray whose every element is an Autobiographical Number ; function to check number is autobiographical ; Convert integer to string ; Iterate for every digit to check for their total count ; Check occurrence of every number and count them ; Check if any position mismatches with total count them return with false else continue with loop ; Function to return the length of the largest subarray whose every element is a Autobiographical number ; Utility function which checks every element of array for Autobiographical number ; Check if element arr [ i ] is an Autobiographical number ; Increment the current length ; Update max_length value ; Return the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ) << " STRNEWLINE " ; return 0 ; }