title: Given an input array find all subarrays with given sum K
body: <p>Given an input array we can find a single sub-array which sums to K (given) in linear time, by keeping track of sum found so far and the start position. If the current sum becomes greater than the K we keep removing elements from start position until we get current sum &lt;= K. </p>
  
  <p>I found sample code from geeksforgeeks and updated it to return all such possible sets. But the assumption is that the input array consists of only +ve numbers. </p>
  
  <pre><code>bool subArraySum(int arr[], int n, int sum)
  {
      int curr_sum = 0, start = 0, i;
      bool found = false;
  
      for (i = 0; i &lt;= n; i++)
      {
          while (curr_sum &gt; sum &amp;&amp; start &lt; i)
          {
              curr_sum = curr_sum - arr[start];
              start++;
          }
  
          if (curr_sum == sum)
          {
              cout&lt;&lt;"Sum found in b/w indices: "&lt;&lt;start&lt;&lt;" &amp; "&lt;&lt;(i-1)&lt;&lt;"\n";
              curr_sum -= arr[start];
              start++;
              found = true;
          }
  
          // Add this element to curr_sum
          if (i &lt; n) {
            curr_sum = curr_sum + arr[i];
          }
      }
  
      return found;
  }
  </code></pre>
  
  <p>My question is do we have such a solution for mixed set of numbers too (both positive and negative numbers)?</p>