Samsung Electronics interview question

Hard questions from Codechef.com