Traverse from right until we find a digit which is greater than its next digit . For example , in 34125 , our index is 4.
for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) > str . charAt ( i + 1 ) ) { index = i ; break ; } }