Hi Vivek Sethia and welcome to bytes.com!
This sounds like a homework question and according to our
posting guidelines we won't answer those for you. If you want help solving it, please tell us what you've come up with so far and we'll try to help you.
Generally speaking, for most problems (including this one) there are several possible solutions that optimize for different things. You could for example optimize for time but need a lot of memory to solve it. You could also optimize for minimal memory consumption but it may take a long time to find the solution.