Find the maximum length of the prefix | C # implementation of the approach ; Function to return the maximum length of the required prefix ; Array to store the frequency of each element of the array ; Iterating for all the elements ; Update the frequency of the current element i . e . v ; Sorted positive values from counts array ; If current prefix satisfies the given conditions ; Return the maximum length ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int Maximum_Length ( List < int > a ) { int [ ] counts = new int [ 11 ] ; int ans = 0 ; for ( int index = 0 ; index < a . Count ; index ++ ) { counts [ a [ index ] ] += 1 ; List < int > k = new List < int > ( ) ; foreach ( int i in counts ) if ( i != 0 ) k . Add ( i ) ; k . Sort ( ) ; if ( k . Count == 1 || ( k [ 0 ] == k [ k . Count - 2 ] && k [ k . Count - 1 ] - k [ k . Count - 2 ] == 1 ) || ( k [ 0 ] == 1 && k [ 1 ] == k [ k . Count - 1 ] ) ) ans = index ; } return ans + 1 ; } static void Main ( ) { List < int > a = new List < int > ( new int [ ] { 1 , 1 , 1 , 2 , 2 , 2 } ) ; Console . Write ( Maximum_Length ( a ) ) ; } }