A beautiful beautiful problem. My very first question that I asked on this forum*. The necessary and sufficient conditions is for f to be continous at 0. Since f is continous everywhere it must be continous at 0. Follow the elegant solution hpe gave.
*)Actually it is find all such functions.