I guess if we start doing mathematical calculation then we can easily find exjact block no which was formed just before difficulty was adjusted. But is there anyway around like by using RPC by which we can find it out?
I don't know a way to get this directly through the RPC without looking at multiple blocks and comparing them, but the math formula is easy:
floor(current_number_of_blocks / 2016) * 2016 - 1
The floor function rounds a number down to an integer value; it's available in most programming languages, although it's sometimes named something to do with integers (e.g.
int()). The number 2,016 is the number of blocks in a retarget period (the time between difficulty changes).
Here's an example command:
bitcoin-cli getblockchaininfo | jq '.blocks / 2016 | floor * 2016 - 1' 524159
As requested, as of this writing, block 524,159 is the last block before the difficulty change. To get the first block with the new difficulty, remove the
-1 (so block 524,160 in this example).
The command used in the above example is jq.