A manuscript I refereed gave me an idea for a paper, not sure how to proceed So, last week I refereed a paper for a journal. The paper in question is a reply to a previous paper by a different author: it points out a serious problem in the original paper and then proposes a solution. The evaluation I sent back to the editors is that, while the problem is real (to the extent that a different researcher has independently identified it), there are a number of reasons why the proposed solution is not going to work. 

Over the weekend, I started thinking about the paper in question again and suddenly realized that I know how to solve the problem (in a nutshell: you need to use a technique originally developed to solve a mildly related class of problems, but which nobody had yet thought of applying to this particular domain). Right now, I have a bunch of handwritten notebook pages with everything I need to write a paper, and all I have left to do is to find a couple of hours to sit in front of my computer and type it up properly.

The question is, how should I proceed now? On the one hand, given that I've developed my own solution, I'm not plagiarizing the paper I refereed (in fact, I intend to give it proper credit for discovering the problem). On the other hand, if I hadn't been asked to referee this paper, I wouldn't have put enough thought into it to come up with my own solution. More generally, to what extent is it acceptable to write a paper that directly builds on a paper you've been asked to referee?
I think there are two questions that need to be answered first.


Is it important to you (e.g. for career reasons) to publish solo, or would you consider publishing with the other author?  Keep in mind that publishing jointly may have extra benefits: the two of you together might come up with an even better solution, or it may lead to future collaborations on other projects.  Or do you perhaps not care about getting credit at all?
Is the manuscript public?  Has it been posted as a preprint on arXiv or a similar server, or on the author's website, etc?


Depending on the answers, there are a few cases:


You don't care about getting credit. Write your report on the paper: "The approach to agrobaric frobotzim via PDQ analysis is unworkable because of foo, bar and baz.  However, this could be fixed by using QPM analysis instead.  [Sketch your idea here.]  Pending this fix [and any other suggested revisions], I recommend the paper for publication."  Expect that when the paper is published, there will be an acknowledgement: "We thank the anonymous referee for suggesting the approach used in Section 5."
You want to publish jointly. Contact the editor: "The author's approach to agrobaric frobotzim via PDQ analysis is unworkable, and as such I cannot recommend the paper for publication as it stands.  However, I have some ideas about how to resolve the problem, and I would be interested in collaborating with the author to work them out.  Would you be willing to put me in touch with her?"


The editor says yes. Great!
The editor says no, and the manuscript is public. Submit your report pointing out the flaws and recommending rejection.  After a decent interval contact the author: "I saw your preprint, and had an idea to improve the results using QPM analysis.  Would you be interested in working together on this?"  Opinions vary on whether you should reveal that you were the referee; it is possible the author will guess anyway.
The editor says no, and the manuscript is not public, but you know who the author is. Submit your report pointing out the flaws and recommending rejection.  The next step is a bit controversial.  Some would say you should wait until it is public, so as not to break the anonymity of the reviewing process.  Others think it is fine to reveal yourself and contact the author directly to suggest collaboration.  I am not sure what to say here.
The editor says no, and the manuscript is not public, and you do not know who the author is (double-blind reviewing). You have no choice but to wait until the manuscript is made public (maybe until it is published somewhere else), since that's the only way to find the author and suggest collaboration.  If you think you should give up on finding the author and publish solo instead, see below.

You want to publish solo.  Write and submit your report.  Now, is the manuscript public?


The manuscript is public. You may write your paper, citing the other author's preprint and giving her due credit for noticing the problem.  This is ethical, but if you meet the author at a conference, don't expect her to buy you a beer: she was hoping to solve this problem but you beat her to it.
The manuscript is not public. You may not proceed.  As a reviewer, you received the manuscript in confidence, and to write a solo paper based on it would be to take unfair advantage of that access.  You must wait until the preprint appears publicly.  (This might be when it is published in another journal, or maybe never.)  It's possible that in the meantime, the author will discover your solution independently; those are the breaks.  This would be a great time to reconsider seeking joint authorship.



In case 2, if your field has a notion of "first authorship", that would be something you'd need to negotiate with the other author, based on your field's norms.  (Mine uses alphabetical ordering almost exclusively, so this issue wouldn't arise.)