title: Find Subarray With Given Sum 1
signature: find_subarray_with_given_sum_1 <- function(arr, n, sum) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, sum, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, sum), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(7L, 7L, 8L, 8L, 23L, 24L, 28L, 32L, 48L, 53L, 56L, 62L, 69L, 77L, 81L, 82L, 84L, 87L, 88L, 90L), 16L, 31L, 
          1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-62L, -62L, -80L, -30L, -80L, 44L, -12L, -76L, 16L, -52L, -86L, 72L, 32L, -60L, -70L, -62L, -78L, -96L, -18L, 40L, -4L, -18L, -58L, 30L, -70L, 6L, 0L, -62L, -66L, 20L, 92L, -64L, 20L, -48L, 48L, -32L, 64L, 22L, 16L, 26L), 39L, 44L, 
          0L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 40L, 7L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(50L, 25L, 6L, 87L, 55L, 86L, 61L, 97L, 24L, 30L, 51L, 43L, 26L, 1L, 80L, 47L, 19L, 36L, 64L, 62L, 92L, 5L, 48L, 27L, 82L, 76L, 70L, 59L, 1L, 43L, 1L, 36L, 28L, 9L, 52L, 22L, 43L), 29L, 105L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-86L, -76L, -64L, -22L, -16L, -8L, 4L, 6L, 8L, 32L, 38L, 60L, 68L, 74L), 7L, 2L, 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L), 31L, 10L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(7L, 7L, 12L, 25L, 25L, 32L, 33L, 34L, 37L, 39L, 39L, 41L, 46L, 48L, 56L, 56L, 57L, 58L, 61L, 62L, 62L, 63L, 65L, 66L, 69L, 72L, 74L, 78L, 78L, 79L, 80L, 85L, 89L, 94L, 95L, 99L), 22L, 39L, 
          1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(98L, -60L), 1L, 98L, 
          1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 8L, 0L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(80L, 89L, 83L, 42L, 75L, 30L, 64L, 25L, 95L, 17L, 90L, 6L, 11L, 1L, 77L, 16L, 75L, 86L, 96L, 67L, 27L, 80L, 27L), 16L, 108L, 
          1L
      )))
  
  }
  
  main()
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;
  }