If you are a programmer and practicing Data Structures problems, then you might have faced the question of finding the subarray with a target sum.

There are a lot of programmers who face difficulty in finding the solution to this question because they don’t have proper knowledge about it.

You will be able to find the solution only if you have a deep understanding of the Subarray. The Subarray is a continuous part of the Array and there are a lot of questions related to it which you will get to know once you start studying the Data Structure and Algorithms.

So, if you are someone who doesn’t have proper knowledge about the term Subarray and how to find a Subarray with given sum, then it’s the right time for you to learn about it in depth.

## What is Subarray?

If you have started to learn Data Structure and Algorithms, then you might be knowing about their type, Linear Data Structure, and Non-Linear Data Structure.

The Subarray is the continuous part of the Array which is a type of Linear Data Structure. So, the arrays are the collection of similar data types which are located sequentially.

There are a lot of questions that you will be going to face from the topic Subarray which will be based on subarray with the given sum, subarray with given xor, maximum subarray sum, and much more.

All of these types of questions are asked in the interviews and on the coding platforms. So, if you don’t have any knowledge about solving these questions, then you are missing a lot of opportunities.

## Why You Should Know to Find a Subarray with a Given Sum?

There are a lot of famous companies like Amazon, Flipkart, Google, etc. that focus on the knowledge of solving DSA problems of the candidates that are appearing.

You will be going to face this type of question in the rounds of interviews with famous companies. Apart from it, if you are learning the DSA, then you will be going to face this type of question which you will need to solve within a specific time.

You will be only able to solve the question if you have the basic knowledge of the term Subarray and the algorithm that is required to solve it.

The algorithm plays a very vital role in solving the DSA questions. So, if you are having difficulties in writing the algorithm of the problem, then it will be hard for you to solve it.

However, we are here to help you with it. We will be helping you in understanding the algorithm by elaborating it properly.

## How to find a Subarray with a given sum?

The Subarray with a given sum is a type of DSA question where you will have to find the subarray for the target sum which has been asked in the problem statement.

In simple words, we can say that you have to find a continuous part of the array which will contain the target sum with the help of the iteration.

In case, if there are no such elements which are containing the target sum by the addition of the elements, then we will have to return the false value.

To illustrate it, check this example where we will tell you the algorithm along with the dry run.

Example: int arr[]= {1,4,2,10,20,5}; Find the subarray for the target sum=32.

Algorithm

- We will need two variables where in one we will store the sum and in the other, we will take the starting element from the array.
- After it, we will iterate over the array and check whether the current sum is equal to the required sum or not.
- If the current sum is less than the required target sum, then we will add another element and if it is greater, then we will increase the starting index.
- If the length of the array is finished, then we will return the message that there is no subarray.

Dry Run

We have told you how the algorithm will be for the problem statement, now take a look at the dry run for the given example.

- First of all, we will initialize two variables, one will be for storing the current sum and the other for the index at which we have started the for a loop.
- So, we will initialize for loop and nested loop for checking each element and sum.
- Now, for the example {1,4,2,10,20,5}, we will start iterating over the element from the index=0 and with the current sum=0.
- So, we will iterate from the index 0, so, our current sum in every iteration will be 0+1=1, 1+4= 5, 5+2=7, 7+10=17, and 17+20=37 which is greater than 32.
- Now, we will iterate from the next element. So, the current sum will be in every iteration, 0+4=4, 4+2=6, 6+10=16, 16+20=36 which is greater than 32. So, we will increase the starting index to 2.
- Again, we will iterate and check for the sum, 0+2=2, 2+10=12, and 12+20=32 which is equal to 32. So, we have found our subarrays which are {2,10,22}. The subarray in addition comes equivalent to the target sum.

So, this will be how the problem will be solved. You will have to code it in your preferred programming language. Also, when you are implementing the dry run, then make sure to properly use the if-else statements, for loop, and nested loop statements.

Conclusion

Solving a Subarray with a given sum problem is not difficult if you have knowledge of its algorithm.

You will have to solve a lot of questions like this if you want to get a good understanding of DSA and programming language. So, start coding and writing algorithms to get a good understanding of it.

## Comments