Doubt in Gray Similar Code ques of Codechef

question link :https://www.codechef.com/problems/GRAYSC
my solution : https://ide.codingblocks.com/s/237594

above code is giving wrong ans on submitting on codechef but it seems right to me.
Pls tell me the error in my code

Hey @saarthakseth
Welcome to the Coding Blocks family
You are using binary search to pick last element
But the range of binary search is not correct
It should be from (a+i+1,a+n,d)
Also you can use 4 nested loops to solve this problem easily
There is no need for the binary search optimisation

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.