adding source code of accepted solution