Space complexity O(N)
Sorry, I am a full time employee, I'm little busy with work this week. Your solution may work. In codeproject some one answered the question and it seems to work. Thank you for replying to my question.
No one has replied