I wrote this solution in my Wikipedia's sandbox, visit the link to see the article: https://en.wikipedia.org/wiki/User:Bhpsngum/sandbox