Program to find the maximum difference between the index of any two different numbers | Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
< ? php function findMaximumDiff ( $ a , $ n ) { $ ind1 = 0 ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ a [ 0 ] != $ a [ $ i ] ) { $ ind1 = $ i ; break ; } } $ ind2 = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ n - 1 ] != $ a [ $ i ] ) { $ ind2 = ( $ n - 1 - $ i ) ; break ; } } return max ( $ ind1 , $ ind2 ) ; } $ a = array ( 1 , 2 , 3 , 2 , 3 ) ; $ n = count ( $ a ) ; echo findMaximumDiff ( $ a , $ n ) ; ? >