My soution to the problem was to first handle the case where the number is odd by checking the last two digits. In case the number is even, I check for the sum and find the candidate digits, i.e,

*digit (MOD 3) = sum of digits of the number (MOD 3)*

Finding the leftmost index where the next digit is greater than the current, in case there aren’t any pick the rightmost such that next digit is less than the current, if there aren’t any return -1.

I am unable to find the mistake in my implementation, could someone be kind enough to point out the mistake here?

Thanks.