Approach for solving problem

Please read the query here
.