Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between subarray, subset & subsequence

I'm a bit confused between subarray, subsequence & subset

if I have {1,2,3,4}

then

subsequence can be {1,2,4} OR {2,4} etc. So basically I can omit some elements but keep the order.

subarray would be( say subarray of size 3)

{1,2,3} {2,3,4}  

Then what would be the subset?

I'm bit confused between these 3.

like image 840
user2821242 Avatar asked Oct 26 '14 00:10

user2821242


People also ask

What is the difference between substring and subsequence?

A Substring takes out characters from a string placed between two specified indices in a continuous order. On the other hand, subsequence can be derived from another sequence by deleting some or none of the elements in between but always maintaining the relative order of elements in the original sequence.

Does Subarray mean continuous?

The actual definition of contiguous subarray (as others have answered) is any sub series of elements in a given array that are contiguous ie their indices are continuous. So given [1,2,3,4,5,6]: [1,2,3], [3,4], [3,4,5,6] are all valid contiguous subarrays. Any algorithm can be used to generate the subarrays.

What does Subarray mean?

A subarray is commonly defined as a part or section of an array. An array is a set of variables that a programmer defines collectively. Instead of creating separate variables, the programmer can declare a single array with multiple values labeled.

Is Subarray same as substring?

A substring is exactly the same thing as a subarray but in the context of strings. For instance, the substrings of the string "ara" would be "a" , "r" , "ar" , "ra" , "ara" , "" . Things to note: A substring is just a subarray that is made up of only characters.


2 Answers

In my opinion, if the given pattern is array, the so called subarray means contiguous subsequence.

For example, if given {1, 2, 3, 4}, subarray can be

{1, 2, 3} {2, 3, 4} etc. 

While the given pattern is a sequence, subsequence contain elements whose subscripts are increasing in the original sequence.

For example, also {1, 2, 3, 4}, subsequence can be

{1, 3} {1,4} etc. 

While the given pattern is a set, subset contain any possible combinations of original set.

For example, {1, 2, 3, 4}, subset can be

{1} {2} {3} {4} {1, 2} {1, 3} {1, 4} {2, 3} etc. 
like image 187
Wilson Avatar answered Oct 01 '22 23:10

Wilson


Consider an array:

 {1,2,3,4} 

Subarray: contiguous sequence in an array i.e.

{1,2},{1,2,3} 

Subsequence: Need not to be contiguous, but maintains order i.e.

{1,2,4} 

Subset: Same as subsequence except it has empty set i.e.

 {1,3},{} 

Given an array/sequence of size n, possible

Subarray = n*(n+1)/2 Subseqeunce = (2^n) -1 (non-empty subsequences) Subset = 2^n 
like image 36
Sandip Pawar Avatar answered Oct 01 '22 22:10

Sandip Pawar