title: Find Subarray With Given Sum 1
signature: class Solution {
  
      public Integer findSubarrayWithGivenSum1(ArrayList<Integer> arr, Integer n, Integer 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.extension: java
question_info.entry_fn_name: findSubarrayWithGivenSum1
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.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer sum, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n, sum,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(7, 7, 8, 8, 23, 24, 28, 32, 48, 53, 56, 62, 69, 77, 81, 82, 84, 87, 88, 90)), 16, 31, 1);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-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);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-86, -76, -64, -22, -16, -8, 4, 6, 8, 32, 38, 60, 68, 74)), 7, 2, 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(98, -60)), 1, 98, 1);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 1, 1, 1, 1, 1, 1)), 8, 0, 1);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer sum, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n, sum),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__