Find the maximum length of the prefix
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: 13Input: 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)) |
5
Recommended Posts:
- 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 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 subarray sum in O(n) using prefix sum
- Maximum prefix-sum for a given range
- Maximum occurrence of prefix in the Array
- Maximum array sum with prefix and suffix multiplications with -1 allowed
- Program to find Length of Bridge using Speed and Length of Train
- 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
- Maximum length of rod for Q-th person
- Maximum Sum Subsequence of length k
- Maximum Length Chain of Pairs | Set-2
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.



