i am getting the wrong answer,how to approach this problem
Yeah your logic is incorrect . Please see this code https://ide.codingblocks.com/s/234791