title: K Th Largest Sum Contiguous Subarray
signature: function k_th_largest_sum_contiguous_subarray(arr, n, k)
source_py: def k_th_largest_sum_contiguous_subarray(arr, n, k):
      sum = []
      sum.append(0)
      sum.append(arr[0])
      for i in range(2, (n + 1)):
          sum.append((sum[(i - 1)] + arr[(i - 1)]))
      Q = []
      heapq.heapify(Q)
      for i in range(1, (n + 1)):
          for j in range(i, (n + 1)):
              x = (sum[j] - sum[(i - 1)])
              if (len(Q) < k):
                  heapq.heappush(Q, x)
              elif (Q[0] < x):
                  heapq.heappop(Q)
                  heapq.heappush(Q, x)
      return Q[0]
source_cpp: int k_th_largest_sum_contiguous_subarray(vector<int> arr, int n, int k) {
    int sum [ n + 1 ];
    sum [ 0 ] = 0;
    sum [ 1 ] = arr [ 0 ];
    for ( int i = 2;
    i <= n;
    i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ];
    priority_queue < int, vector < int >, greater < int > > Q;
    for ( int i = 1;
    i <= n;
    i ++ ) {
      for ( int j = i;
      j <= n;
      j ++ ) {
        int x = sum [ j ] - sum [ i - 1 ];
        if ( Q . size ( ) < k ) Q . push ( x );
        else {
          if ( Q . top ( ) < x ) {
            Q . pop ( );
            Q . push ( x );
          }
        }
      }
    }
    return Q . top ( );
  }
question_info.entry_fn_name: k_th_largest_sum_contiguous_subarray
question_info.entry_cls_name: Solution
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.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(arr, n, k, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n, k) 
      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({2, 3, 5, 7, 8, 29, 29, 44, 47, 52, 60, 65, 73, 83, 87, 92, 92, 95}, 10, 12, 157)
      print(string.format("TEST-0...%s",result))
  
      result = driver({44, -98, -10, 14, -6, -46, 6, -74, -4, 36, 10, -2, 30, 28, 96, -84, -36, -76, 64, -74, -20, 94, -4, 14, 78, 52, -56, 98, -68, -76, -10, 20, 88, -98, 96, 80, 96, -32, -40, -30, 82}, 34, 37, 166)
      print(string.format("TEST-1...%s",result))
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 16, 15, 5)
      print(string.format("TEST-2...%s",result))
  
      result = driver({58, 21, 97, 78, 78, 57, 29, 33, 57, 81, 66, 32, 11, 82, 28, 72, 46, 67, 42, 15, 60, 45, 16, 37}, 14, 20, 508)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-92, -90, -88, -84, -68, -66, -62, -58, -52, -44, -22, -16, -4, -4, 2, 12, 14, 14, 24, 26, 44, 56, 80, 90, 92, 94, 98}, 15, 25, -86)
      print(string.format("TEST-4...%s",result))
  
      result = driver({1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}, 5, 5, 3)
      print(string.format("TEST-5...%s",result))
  
      result = driver({3, 4, 8, 12, 13, 14, 17, 19, 23, 24, 28, 29, 30, 35, 35, 38, 44, 47, 47, 53, 55, 56, 56, 58, 66, 67, 70, 71, 72, 73, 74, 75, 77, 78, 82, 84, 87, 87, 87, 88, 88, 93, 94, 96}, 26, 25, 721)
      print(string.format("TEST-6...%s",result))
  
      result = driver({20, -58, 94, -70, 18, 16, -46, 38, -44, -92, -20, -70, -30, 50}, 11, 7, 36)
      print(string.format("TEST-7...%s",result))
  
      result = driver({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}, 19, 23, 0)
      print(string.format("TEST-8...%s",result))
  
      result = driver({90, 77, 82, 38, 96, 62, 66, 4, 93, 30, 75, 22, 26, 61, 40, 11, 38, 55, 88, 24, 66, 47, 40, 71, 21, 5, 18, 31, 26, 56, 19, 47, 71, 34}, 28, 32, 1041)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.test_list: [{"idx": 0, "outputs": 157, "inputs": {"arr": [2, 3, 5, 7, 8, 29, 29, 44, 47, 52, 60, 65, 73, 83, 87, 92, 92, 95], "n": 10, "k": 12}}, {"idx": 1, "outputs": 166, "inputs": {"arr": [44, -98, -10, 14, -6, -46, 6, -74, -4, 36, 10, -2, 30, 28, 96, -84, -36, -76, 64, -74, -20, 94, -4, 14, 78, 52, -56, 98, -68, -76, -10, 20, 88, -98, 96, 80, 96, -32, -40, -30, 82], "n": 34, "k": 37}}, {"idx": 2, "outputs": 5, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 16, "k": 15}}, {"idx": 3, "outputs": 508, "inputs": {"arr": [58, 21, 97, 78, 78, 57, 29, 33, 57, 81, 66, 32, 11, 82, 28, 72, 46, 67, 42, 15, 60, 45, 16, 37], "n": 14, "k": 20}}, {"idx": 4, "outputs": -86, "inputs": {"arr": [-92, -90, -88, -84, -68, -66, -62, -58, -52, -44, -22, -16, -4, -4, 2, 12, 14, 14, 24, 26, 44, 56, 80, 90, 92, 94, 98], "n": 15, "k": 25}}, {"idx": 5, "outputs": 3, "inputs": {"arr": [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], "n": 5, "k": 5}}, {"idx": 6, "outputs": 721, "inputs": {"arr": [3, 4, 8, 12, 13, 14, 17, 19, 23, 24, 28, 29, 30, 35, 35, 38, 44, 47, 47, 53, 55, 56, 56, 58, 66, 67, 70, 71, 72, 73, 74, 75, 77, 78, 82, 84, 87, 87, 87, 88, 88, 93, 94, 96], "n": 26, "k": 25}}, {"idx": 7, "outputs": 36, "inputs": {"arr": [20, -58, 94, -70, 18, 16, -46, 38, -44, -92, -20, -70, -30, 50], "n": 11, "k": 7}}, {"idx": 8, "outputs": 0, "inputs": {"arr": [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], "n": 19, "k": 23}}, {"idx": 9, "outputs": 1041, "inputs": {"arr": [90, 77, 82, 38, 96, 62, 66, 4, 93, 30, 75, 22, 26, 61, 40, 11, 38, 55, 88, 24, 66, 47, 40, 71, 21, 5, 18, 31, 26, 56, 19, 47, 71, 34], "n": 28, "k": 32}}]