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
- Find subarray of Length K with Maximum Peak
- Find maximum average subarray of k length
- Find maximum sum array of length less than or equal to m
- Find the Increasing subsequence of length three with maximum product
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Count of strings whose prefix match with the given string to a given length k
- Maximum prefix-sum for a given range
- Maximum subarray sum in O(n) using prefix sum
- Maximum occurrence of prefix in the Array
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum length L such that the sum of all subarrays of length L is less than K
- Minimize length of prefix of string S containing all characters of another string T
- 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 index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
- Find the longest common prefix between two strings after performing swaps on second string

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.