code is here : https://ideone.com/RGihnN