Samsung Electronics interview question

Solve a difficult coding challenge