This question already has an answer here:
How is Merkle path calculated? I am looking for a detailed algorithm rather than a quick one liner for this so I can implement it.
Note that this is not the same as computing Merkle root or building Merkle tree. Also not asking why Merkle path is needed. I did not find any info on this site going over the algorithm for deriving Merkle path.