## Subarray with given sum- In this problem, we are provided with an unsorted array of non-negative integers and a sum integer value.
- We need to find the part from the array, or can we need to find the Subarray, in which the sum of the elements of that array is exactly matched to the given sum.
- We need to apply the algorithm to solve this problem, which ultimately results in the desired output.
Let us try to understand it with the help of an example - We are given an array and a sum. ## Example 1 :
## Example 2 :
## Example 3 :
## Example 4 :
Here if we observe that, we have more than one Subarray, in which the sum of all elements is equal to 14, i.e., are as follows - - First subarray is found from indexes 0 to 1, i.e., { 1 , 13 }.
- Second subarray is found on index 3, i.e., { 14 }.
- The third Subarray is found from indexes 4 to 6, i.e., { 3, 7, 4 }.
## What is the output if we get multiple such sub-arrays producing the desired sum by adding their elements?It will display the ## Methods to Solve the Problem for finding the Subarray from the given array, whose sum of elements matches with the given sum -- To use the Brute force approach, an inefficient and simple method.
- To use the Linear complexity method, an efficient approach.
Let us find the Subarray from the given array, whose sum matches the given sum using these methods mentioned above. ## Method # 1 Brute Force approach, an inefficient and simple method -The method a programmer must prefer is always brute force approach, which will increase our understanding of the problem. Once we have solved this problem using this method, it will lead to raising our confidence level, and after then we can go further to try with the other methods, which is probably more efficient then this method. Let us discuss the above problem using this method: As we have already seen, the working of this problem, we all need to find a subarray in which the sum of all elements matches with the given sum value.
After executing the program successfully in a specific programming language and following the Brute force algorithm using the Now let us analyze the running time and performance of the algorithm we are applying to sort the array in ascending order by finding the algorithm's time complexity and space complexity using in Method # 1.
For finding the time complexity, we need to observe the algorithm and analyze the time taken by a particular line for execution. We can observe that we require traversing the array using two for loops and checking the condition for finding the sum. Hence time complexity will be - T ( n ) = O ( n ).O ( n )
Hence time complexity will be O ( n
To find space complexity, we need to observe the program and analyze the space required to store all the variables; as we notice the program, we will observe that no extra space is required to store the elements.
Hence the space complexity for above method # 1 will be constant. ## Method # 2 Linear Complexity method, an inefficient and simple approach -As we have already seen the working of method 1, i.e., Brute force approach, we will move further towards a more efficient method; it is very easy to implement to solve any problem; as the name suggests, the linear complexity method has linear complexity. Once you have succeeded in making the program, try to find the other best alternative to solve the given problem, in a much more efficient way, whose performance is better and time complexity is less than this method. Let us discuss the above problem using this method: As we have already seen, the working of this problem, we all need to find a subarray in which the sum of all elements matches with the given sum value. ## Algorithm to implement Linear complexity method to solve this problem -
Now let us analyze the performance and running time of the algorithm we are applying to find a subarray in which the sum of its elements is equal to the given sum value by finding the algorithm's time complexity and space complexity using in Method # 2.
For finding the time complexity, first, we need to observe the algorithm and analyze the time taken by a particular line of code for execution. We can observe that traversing of an array is done by using only one for loop and checking the condition for finding the sum. Hence time complexity will be - T ( n ) = O ( n ) + C Here, ' C ' be any constant
Hence time complexity will be O ( n ), as we do the asymptotically rough idea that we require ' n ' time to solve the problem of ' n ' elements.
To find space complexity, we need to observe the lines of code and analyze the space required to store all the variables, as we notice the program that no extra space is required to store the elements.
Hence the space complexity for above method # 2 will be constant. Next TopicSelf-organizing List |