Disclaimer: This is a user generated content submitted by a member of the WriteUpCafe Community. The views and writings here reflect that of the author and not of WriteUpCafe. If you have any complaints regarding this post kindly report it to us.

Count on all the essential DSA concepts to crack even the toughest coding interviews! 


From array related concepts to strings to longest increasing subsequence to binary tree to dynamic programming, coding interviewer takes due weightage to all such concepts.


When we dive deep inside the array problems, we often encounter one of the most crucial concepts that is subarray with given sum


Subarray is a part of an array whose sum can be calculated with the given number of elements.


In this tutorial, we have compiled the necessary information related to this topic to help you gain the knowledge momentum. 


Let’s get started!


Subarray Sum – An overview 


A subarray is defined as the contiguous part of an array. Or, it is touted as an array inside of another array. 


Take a quick glance at the below example;


Consider this array; [ 1, 2, 3, 4] 


In this case, there will be 10 non-empty subarrays. These are; [1], [2], [3], [4], [1,2], [ 2, 3] , [ 3, 4] , [ 1, 2, 3] , [ 2, 3, 4] , [ 1, 2, 3, 4] 


In simpler terms, for any given string or an array of the given size n, there will be n* [ n +1] / 2 arrays of certain size.


Let’s consider the problem statement:


A given unsorted array as nums or integer sum k is given to you. The array will be having the non-negative integers in them. You need to find the contiguous subarrays in it where the elements may sum up till the given sum.


There would be multiple such subarrays that can be found successfully with the start or at the end of a given index in order to print the range of all subarrays that are present. 


Though, if the sum of a given subarray with that of the given sum will not be present, you may print simply; No Subarray Found’


Take an example;


If the required input in a case is; [ 4, 3, 6, 5, 2, 8] 


The sum is 13 




As you will see that an array is given to you along with certain elements in it. Sum of this subarray will be found which can go till it. 


In this example of the given array, the subarray sum would be the 13. Hece, the elements 6, 5 and 2 will be those that are creating a subarray sum that would be 13 [ 6 +5+2].


Hence, the required output in this case would be from the start or the end of an index that will give you the subarray that needs to be printed. 


Output in the case of multiple subarrays 


It may show that the subarray with the given sum may exactly match the given input sum. 


In brief, we are going to find a sub-array that would match the perspective in case of a given sum. Hence, for that you may need to follow certain approaches. 


Brute force algorithm approach 


To find the subarray of a given sum, you can use the brute force algorithm approach. In this case, you need to find subarrays whose total elements would be equal with the specified values.


Algorithm to follow:


  • Ask the user an array denoted by ‘n’ which will represent the non-negative integers that are useful in the main function 
  • Call the function by locating a subarray in which the total number of given elements will be equal to the specified total. As the certain argument, pass all the functions in the original array with the certain number of elements
  • Run the required two loops in your subarray function in a way that one loop will go from the index 0 till the last index. In order to navigate your array from the index 0 till index n -1, you may need to define the i = 0 till i = n -1
  • In the second loop which is called the nested loop, you need to run it from the j = i +1 till n of that loop 
  • Declare the variable as the currentsum variable , if it doesn’t print, you can print the required sum of index
  • Determine if the currentsum is exceeding the sum j == n to exit the loop 
  • Assign the currensum to be equal to the currentsum plus arr [j] 
  • If the test gets unsuccessful, you need to again calculate the current sum at the index j 
  • Then the outcome would be obtained after you run the loop 1 and the nested loop 2 inside the loop 1
  • When everything will be finished, a desired outcome would appear in front of your screen 

In order to get the sum of a subarray, you may need to traverse the given array in such a way so as to use the for loop for checking the condition. 


Time complexity in this case would be T[n] = O[n]. O[n] = O [n^2] 


The space complexity in this case would be the S [n] = O [1] as there will not be any additional space which is needed in order to store the variables. 


Hashmap Approach 


In this approach you need to first create an empty dictionary with the value type list in order to initialise all the variables with the value zero. 


Iterate over all the elements with the corresponding indexes in a number lit. while you iterate it, add the current elements. In a dictionary if the key is present, append the index.


Fetch the required sum by subtraction of the sum of the required arrays with that of a running sum. To find indexes, add the variable indices to store all the subarrays. 


If the sum comes out as zero, the current index will have the value equal to the sum. 

Wrapping Up 


Kickstart your interview preparation by brushing your knowledge on all vital topics like  longest increasing subsequence, arrays, reversing an array, binary tree among others.


Keep this guide as your ultimate resource to win over the concept of subarray with given sum by implementing its approaches.


Happy learning, happy coding!


Welcome to WriteUpCafe Community

Join our community to engage with fellow bloggers and increase the visibility of your blog.
Join WriteUpCafe