how can we solve this problem Select team The language allowed was only C thats make problem more hard I tried to make all possible pairs store the pairs sum in sorted array ,then i started searching form end to check if it is possible that we can find two teams with maxsum But unable to handle like case 0 0 1 1
can anyone tell me how to approach this problem efficiently considering c language?
Please help
Can't you just consider all ways to choose 1st, 2nd and 3rd student and check whether you have 4th one with skill equal to $$$(S_1+S_2-S_3)$$$? And then choose maximum among all possible teams.
N=500 Will give tle don't you think O(n^4)
No, it's O(n^3*log(n)) because you choose 3 students and check 4th in O(log n) using unordered_map for example. Probably there is a way to choose in O(1), but I don't know how
It's not hard to do it in $$$O(n^3)$$$. Sort the array. Consider all $$$O(n^2)$$$ ways of choosing the first team. Now you have to find if there is a pair of elements, after erasing the first team from the array, with a given sum.
The last problem can be solved in $$$O(n)$$$ time with a two pointers approach in a sorted array.