title: Find Subarray With Given Sum 1
signature: function find_subarray_with_given_sum_1(arr, n, sum)
source_py: def find_subarray_with_given_sum_1(arr, n, sum):
      curr_sum = arr[0]
      start = 0
      i = 1
      while (i <= n):
          while ((curr_sum > sum) and (start < (i - 1))):
              curr_sum = (curr_sum - arr[start])
              start += 1
          if (curr_sum == sum):
              print('Sum found between indexes')
              print(('%d and %d' % (start, (i - 1))))
              return 1
          if (i < n):
              curr_sum = (curr_sum + arr[i])
          i += 1
      print('No subarray found')
      return 0
source_cpp: int find_subarray_with_given_sum_1(vector<int> arr, int n, int sum) {
    int curr_sum = arr [ 0 ], start = 0, i;
    for ( i = 1;
    i <= n;
    i ++ ) {
      while ( curr_sum > sum && start < i - 1 ) {
        curr_sum = curr_sum - arr [ start ];
        start ++;
      }
      if ( curr_sum == sum ) {
        return 1;
      }
      if ( i < n ) curr_sum = curr_sum + arr [ i ];
    }
    return 0;
  }
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, sum, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(arr, n, sum) 
      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({7, 7, 8, 8, 23, 24, 28, 32, 48, 53, 56, 62, 69, 77, 81, 82, 84, 87, 88, 90}, 16, 31, 1)
      print(string.format("TEST-0...%s",result))
  
      result = driver({-62, -62, -80, -30, -80, 44, -12, -76, 16, -52, -86, 72, 32, -60, -70, -62, -78, -96, -18, 40, -4, -18, -58, 30, -70, 6, 0, -62, -66, 20, 92, -64, 20, -48, 48, -32, 64, 22, 16, 26}, 39, 44, 0)
      print(string.format("TEST-1...%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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 40, 7, 1)
      print(string.format("TEST-2...%s",result))
  
      result = driver({50, 25, 6, 87, 55, 86, 61, 97, 24, 30, 51, 43, 26, 1, 80, 47, 19, 36, 64, 62, 92, 5, 48, 27, 82, 76, 70, 59, 1, 43, 1, 36, 28, 9, 52, 22, 43}, 29, 105, 1)
      print(string.format("TEST-3...%s",result))
  
      result = driver({-86, -76, -64, -22, -16, -8, 4, 6, 8, 32, 38, 60, 68, 74}, 7, 2, 0)
      print(string.format("TEST-4...%s",result))
  
      result = driver({0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0}, 31, 10, 1)
      print(string.format("TEST-5...%s",result))
  
      result = driver({7, 7, 12, 25, 25, 32, 33, 34, 37, 39, 39, 41, 46, 48, 56, 56, 57, 58, 61, 62, 62, 63, 65, 66, 69, 72, 74, 78, 78, 79, 80, 85, 89, 94, 95, 99}, 22, 39, 1)
      print(string.format("TEST-6...%s",result))
  
      result = driver({98, -60}, 1, 98, 1)
      print(string.format("TEST-7...%s",result))
  
      result = driver({0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, 8, 0, 1)
      print(string.format("TEST-8...%s",result))
  
      result = driver({80, 89, 83, 42, 75, 30, 64, 25, 95, 17, 90, 6, 11, 1, 77, 16, 75, 86, 96, 67, 27, 80, 27}, 16, 108, 1)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 1, "inputs": {"arr": [7, 7, 8, 8, 23, 24, 28, 32, 48, 53, 56, 62, 69, 77, 81, 82, 84, 87, 88, 90], "n": 16, "sum": 31}}, {"idx": 1, "outputs": 0, "inputs": {"arr": [-62, -62, -80, -30, -80, 44, -12, -76, 16, -52, -86, 72, 32, -60, -70, -62, -78, -96, -18, 40, -4, -18, -58, 30, -70, 6, 0, -62, -66, 20, 92, -64, 20, -48, 48, -32, 64, 22, 16, 26], "n": 39, "sum": 44}}, {"idx": 2, "outputs": 1, "inputs": {"arr": [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], "n": 40, "sum": 7}}, {"idx": 3, "outputs": 1, "inputs": {"arr": [50, 25, 6, 87, 55, 86, 61, 97, 24, 30, 51, 43, 26, 1, 80, 47, 19, 36, 64, 62, 92, 5, 48, 27, 82, 76, 70, 59, 1, 43, 1, 36, 28, 9, 52, 22, 43], "n": 29, "sum": 105}}, {"idx": 4, "outputs": 0, "inputs": {"arr": [-86, -76, -64, -22, -16, -8, 4, 6, 8, 32, 38, 60, 68, 74], "n": 7, "sum": 2}}, {"idx": 5, "outputs": 1, "inputs": {"arr": [0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0], "n": 31, "sum": 10}}, {"idx": 6, "outputs": 1, "inputs": {"arr": [7, 7, 12, 25, 25, 32, 33, 34, 37, 39, 39, 41, 46, 48, 56, 56, 57, 58, 61, 62, 62, 63, 65, 66, 69, 72, 74, 78, 78, 79, 80, 85, 89, 94, 95, 99], "n": 22, "sum": 39}}, {"idx": 7, "outputs": 1, "inputs": {"arr": [98, -60], "n": 1, "sum": 98}}, {"idx": 8, "outputs": 1, "inputs": {"arr": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1], "n": 8, "sum": 0}}, {"idx": 9, "outputs": 1, "inputs": {"arr": [80, 89, 83, 42, 75, 30, 64, 25, 95, 17, 90, 6, 11, 1, 77, 16, 75, 86, 96, 67, 27, 80, 27], "n": 16, "sum": 108}}]
question_info.extension: lua
question_info.entry_fn_name: find_subarray_with_given_sum_1
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__