title: Find a subarray from a array that equals some value X
body: <p><strong>Background</strong></p>
  
  <p>You are given an array that includes positive and negative numbers.
  You are to find a subarray within the array that equals some value X.
  The input is the array and the X value.
  The output is the starting and ending index of the subarray.</p>
  
  <p><strong>Example</strong></p>
  
  <pre><code>Array = [2,6,0,9,7,3,1,4,1,10] 
  X = 15
  Output = [1,3]
  </code></pre>
  
  <p>Following is the Code I found on <a href="https://www.geeksforgeeks.org/find-subarray-with-given-sum-in-array-of-integers/" rel="nofollow noreferrer">geeks4geeks</a></p>
  
  <pre><code>  public static void subArraySum(int[] arr, int n, int sum) { 
          //cur_sum to keep track of cummulative sum till that point 
          int cur_sum = 0; 
          int start = 0; 
          int end = -1; 
          HashMap&lt;Integer, Integer&gt; hashMap = new HashMap&lt;&gt;(); 
  
          for (int i = 0; i &lt; n; i++) { 
              cur_sum = cur_sum + arr[i]; 
              //check whether cur_sum - sum = 0, if 0 it means 
              //the sub array is starting from index 0- so stop 
              if (cur_sum - sum == 0) { 
                  start = 0; 
                  end = i; 
                  break; 
              } 
              //if hashMap already has the value, means we already  
              // have subarray with the sum - so stop 
              if (hashMap.containsKey(cur_sum - sum)) { 
                  start = hashMap.get(cur_sum - sum) + 1; 
                  end = i; 
                  break; 
              } 
              //if value is not present then add to hashmap 
              hashMap.put(cur_sum, i); 
  
          } 
          // if end is -1 : means we have reached end without the sum 
          if (end == -1) { 
              System.out.println("No subarray with given sum exists"); 
          } else { 
              System.out.println("Sum found between indexes " 
                              + start + " to " + end); 
  </code></pre>
  
  <p><strong>QUESTION</strong>
  Overall I am able to understand why this solution works. The first block with the condition <code>cur_sum - sum == 0</code> makes total sense to me. </p>
  
  <p>However I am extremely confused about why the <code>hashMap.containsKey(cur_sum - sum)</code> check is working. I know that it works every time but I want to understand the mathematical significance behind it. I read in a interview blog that this uses the common diff technique but i am not sure how this applies to this.</p>
  
  <p>I am not trying to memorize this solution as I prepare for interview but trying to build a solid understanding of why it is working.</p>
  
  <p>I have spent hours coming up with examples and traced through them by hand and the solution works but I am not able to grasp why this technique works and i refuse to memorize it blindly.</p>
  
  <p>For instance if we are talking about a array with positive numbers only then i know the "SLIDING WINDOW" technique can be used and that i am able to completely comprehend. As you expand the window the sum gets larger and as you close the window the sum gets smaller. This is not the case for the problem above and so would appreciate some help. </p>