Given an array **arr[]** of **N** integers where all elements of the array are from the range **[0, 9]** i.e. a single digit, the task is to find the maximum length of the prefix of this array such that removing exactly one element from the prefix will make the occurrence of the remaining elements in the prefix same.

**Examples:**

Input:arr[] = {1, 1, 1, 2, 2, 2}

Output:5

Required prefix is {1, 1, 1, 2, 2}

After removing 1, every element will have equal frequency i.e. {1, 1, 2, 2}

Input:arr[] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5}

Output:13

Input:arr[] = {10, 2, 5, 4, 1}

Output:5

**Approach:** Iterate over all the prefixes and check for each prefix if we can remove an element so that

each element has same occurrence. In order to satisfy this condition, one of the following conditions must hold true:

- There is only one element in the prefix.
- All the elements in the prefix have the occurrence of 1.
- Every element has the same occurrence, except for exactly one element which has occurrence of 1.
- Every element has the same occurrence, except for exactly one element which has the occurrence exactly 1 more than any other elements.

Below is the implementation of the above approach:

`# Python3 implementation of the approach ` ` ` `# Function to return the maximum ` `# length of the required prefix ` `def` `Maximum_Length(a): ` ` ` ` ` `# Array to store the frequency ` ` ` `# of each element of the array ` ` ` `counts ` `=` `[` `0` `]` `*` `11` ` ` ` ` `# Iterating for all the elements ` ` ` `for` `index, v ` `in` `enumerate` `(a): ` ` ` ` ` `# Update the frequency of the ` ` ` `# current element i.e. v ` ` ` `counts[v] ` `+` `=` `1` ` ` ` ` `# Sorted positive values from counts array ` ` ` `k ` `=` `sorted` `([i ` `for` `i ` `in` `counts ` `if` `i]) ` ` ` ` ` `# If current prefix satisfies ` ` ` `# the given conditions ` ` ` `if` `len` `(k)` `=` `=` `1` `or` `(k[` `0` `]` `=` `=` `k[` `-` `2` `] ` `and` `k[` `-` `1` `]` `-` `k[` `-` `2` `]` `=` `=` `1` `) ` `or` `(k[` `0` `]` `=` `=` `1` `and` `k[` `1` `]` `=` `=` `k[` `-` `1` `]): ` ` ` `ans ` `=` `index ` ` ` ` ` `# Return the maximum length ` ` ` `return` `ans ` `+` `1` ` ` `# Driver code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` `a ` `=` `[` `1` `, ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(Maximum_Length(a)) ` |

*chevron_right*

*filter_none*

**Output:**

5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Count of strings whose prefix match with the given string to a given length k
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Minimize length of prefix of string S containing all characters of another string T
- Maximum length L such that the sum of all subarrays of length L is less than K
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum prefix-sum for a given range
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Maximum subarray sum in O(n) using prefix sum
- Maximum occurrence of prefix in the Array
- Program to find Length of Bridge using Speed and Length of Train
- Find the length of the median of a Triangle if length of sides are given
- Find the longest common prefix between two strings after performing swaps on second string
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Queries to find the Lower Bound of K from Prefix Sum Array with updates using Fenwick Tree
- Maximum length Subsequence with alternating sign and maximum Sum
- Find the Increasing subsequence of length three with maximum product
- Find maximum average subarray of k length

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.