In this I have solved the question but it shows time limit error.
change line no 23 : for(int i=0;i<num;i++)
See this Code https://ide.codingblocks.com/s/265846