how to approach this problem.
i used next_permutation which is available in c++ but that gave me TLE .so now how should i solve this

I think there is problem in test cases of 2nd subtask because my code is showing WA rather than TLE. I have check other codes of coders and i found that no one got 100 till now.

I have just changed your code a little bit and make the complexity O(N) per test case. You can see my solution here.

My changes is simple. I just found a repeated pattern in sum[] array, which can easily be calculated without using next_permutation() function. I am not explaining it as i want that you should try first, after that if you wouldn’t reach the solution then comment here. I will explain it for you.

But at last i think 2nd subtask have some problem.

This problem almost drove me nuts tbh. I gave up after 10 mins of brainstorming.

If you still doubt then you can see this article. But it’s only for distinct digits.