title: Find Largest D In Array Such That A B C D
signature: function find_largest_d_in_array_such_that_a_b_c_d(s, n)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(s, n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(s, n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver({8, 12, 14, 15, 16, 20, 27, 28, 29, 30, 35, 41, 46, 51, 53, 55, 55, 58, 63, 64, 72, 73, 75, 75, 75, 82, 82, 86, 89, 91, 92, 94, 95, 95, 97, 97, 98}, 24, 75)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-96, -82, -62, -58, -56, -50, -44, -44, -40, -28, -22, -20, -12, -2, 10, 26, 34, 42, 48, 74, 86, 92}, 19, 48)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 1, 1, 1}, 8, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver({10, 10, 47, 54, 55, 58, 65, 66, 67, 77, 84}, 5, -1)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-46, -28, -20, -18, 4, 8, 18, 38, 90, 90}, 6, -1)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 35, 1)
      print(string.format("TEST-5...%s",result))
  
      result = driver({11, 13, 14, 21, 26, 28, 36, 39, 41, 42, 43, 44, 49, 49, 57, 58, 59, 59, 63, 64, 67, 69, 70, 75, 78, 79, 83, 83, 86, 91, 92, 93, 96, 96, 96, 97}, 30, 91)
      print(string.format("TEST-6...%s",result))
  
      result = driver({-88, -82, -82, -76, -70, -66, -62, -56, -48, -48, -32, -30, -18, -16, 0, 4, 34, 38, 46, 46, 50, 52, 54, 54, 62, 74, 76, 78, 96}, 16, -30)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 17, 1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({4, 5, 6, 9, 11, 14, 18, 20, 21, 25, 33, 49, 55, 56, 58, 61, 66, 66, 68, 74, 74, 87, 92, 99}, 23, 92)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
source_py: def find_largest_d_in_array_such_that_a_b_c_d(S, n):
      found = False
      S.sort()
      for i in range((n - 1), (- 1), (- 1)):
          for j in range(0, n):
              if (i == j):
                  continue
              for k in range((j + 1), n):
                  if (i == k):
                      continue
                  for l in range((k + 1), n):
                      if (i == l):
                          continue
                      if (S[i] == ((S[j] + S[k]) + S[l])):
                          found = True
                          return S[i]
      if (found == False):
          return (- 1)
source_cpp: int find_largest_d_in_array_such_that_a_b_c_d(vector<int> S, int n) {
    bool found = false;
    sort(S.begin(), S.end());
    for ( int i = n - 1;
    i >= 0;
    i -- ) {
      for ( int j = 0;
      j < n;
      j ++ ) {
        if ( i == j ) continue;
        for ( int k = j + 1;
        k < n;
        k ++ ) {
          if ( i == k ) continue;
          for ( int l = k + 1;
          l < n;
          l ++ ) {
            if ( i == l ) continue;
            if ( S [ i ] == S [ j ] + S [ k ] + S [ l ] ) {
              found = true;
              return S [ i ];
            }
          }
        }
      }
    }
    if ( found == false ) return INT_MIN;
  }