acash's blog

By acash, history, 5 years ago, In English

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?

  • Vote: I like it
  • -18
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it -13 Vote: I do not like it

Please help

  • »
    »
    5 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      N=500 Will give tle don't you think O(n^4)

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        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

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +8 Vote: I do not like it

        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.