I am getting TLE as my code takes about 19 sec to execute. Could someone suggest me, on how could I optimize my approach (or the code) so as to make it fit in 10 sec ??
The problem here is that your are executing if(array[k]==j){inverse[j-1]=k+1;}repeatedly which is not needed here. You just check it once and if it is true then break it like this if(array[k]==j){inverse[j-1]=k+1;break;}.
Think that there is only one element in array which will be same as j in inner for loop at a time so why check for all. So just execute inner for loop till ‘if’ becomes true and add break statement there as i explained above.
It can be done in a single iteration as well. After all, all you have to check is that the value at index i is the index of value i. OR in other words-