# A Brief Introduction To Subarray And The Difference Between Subarray Vs Subsequence

**Array, subarrays, and subsequence are programming concepts that are something we have heard of in one way or another! **

But before jumping further, we would like to ask you a few questions:

- Are arrays a set of elements?
- Is the subarray part of the array?
- What is the difference between subarray, and sequence?

Well, this blog is all that you need as help to widen your knowledge on array related concepts. Further, we will be discussing the possibility of subarray sum divisible by K and the grid unique paths.

Let’s get started!

**What is a subarray? **

Before going into the details, it’s important to know about the basis of subarray. A subarray can be defined as the elements that are present inside an already structured array. The elements of a subarray are contiguous to each other.

It can be determined or found by running two different nested loops in the logic. A nested loop is basically a loop inside a loop much like the subarrays which we are discussing currently.

Now, running two nested loops would imply that we can use the inner loop to figure out the elements in the right of the subarray. Whereas, the outer loop can figure out the elements at the beginning of the array.

With that said, before heading on to the differences between subarray and subsequence, let us first take a look at a brief definition of subsequence.

**What is the subsequence?**

By removing other elements or zero from a given sequence you can derive the subsequence of the data.

Also, you have to make sure that the general arrangement and placement of the other elements of the sequence remain in the same position.

We can also say that a subsequence data structure is the generalized form of a subarray that does not follow the rule of contiguity. This means that the elements depicted in a subsequence may or may not be attached to each other.

Now that we have the definition understood, let us look at the differences between a subarray and a subsequence.

**Differences between subarray and subsequence**

- The major difference between a subarray and a subsequence is that the elements in a subarray are required to be contiguous, this is not the case with a subsequence.

This means that the elements in a subsequence are not attached to each other.

- In the case of subarrays, you can use both {} and <>’ s to depict the arrays. Whereas, in the case of subsequence you can only use <>’s to depict the subsequence.

Apart from the differences mentioned above the other rules applied to sub-arrays are similar to the subsequence. Hence, this goes to say that these data structures have more in common than they are different.

We would also like to throw in the fact that if you are planning to appear for a programming interview in the future, it would be helpful to learn the concept of subsequence beforehand because it is one of the most discussed topics in an interview.

We will now move on to the problem of grid unique paths.

**Grid unique paths**

The problems related to this particular concept usually challenge the programmer to find the unique paths in a grid.

There are a few different approaches or methods that you can apply to solve problems of this sort, they are as follows:

This method calls out to use the solutions to smaller problems than the main solution that you have been presented with.

You can use the recursive method and start traversing the data structure from the bottom. This will help you navigate the problem faster.

This approach is the abbreviated form of dynamic programming which is used to solve the overlapping subproblems in a given problem structure.

We can go by this approach from the top down by first constructing a matrix with the data that has been provided.

Or, alternatively, we can also navigate the grid bottom-up and also construct a matrix to solve the problem.

**Optimizing space from the DP approach**

In this case, we will be handling the problem from the bottom-up approach and we will be storing the previously generated answer in a 2-D matrix.

Those were some of the approaches that you can use in order to solve grid unique paths problems. For the final section of the blog, we will discuss how to figure out if the subarray sum is divisible or not.

**How to figure out if the subarray sum is divisible or not? **

For understanding this concept, let us assume that you have been provided with an array consisting of negative integers with a k value.

Now, the easiest method to approach the solution to this problem would be to calculate the sum of all the given subarrays one at a time and determine whether they are divisible by k or not.

You can create an auxiliary array for starters of k size for holding the remainder of the sum that you will be deriving after dividing each cumulative index.

From there onwards you can traverse this auxiliary array and figure out if the subarray sum is divisible by k or not in the case of this example.

**Conclusion**

From what we have discussed in the blog it would be right to conclude that the similarities between a subsequence and a subarray are way greater than their differences.

Essentially the only major difference between the two is that the elements of the subarray are attached to each other, while it is not the case with subsequence.

Other than subarray and subsequence we have also mentioned the approaches and methods you can imply for solving problems related to the concepts of, grid unique paths and how to figure out if the subarray sum divisible by k or not.

Mike K. Watson

Source link