58

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.

Amit Upadhyay
  • 7,179
  • 4
  • 43
  • 57
user2821242
  • 1,041
  • 3
  • 9
  • 16
  • 1
    A sub--- is a subset of a ---. – Hot Licks Oct 26 '14 at 00:23
  • It's not clear how this is a programming question. It appears to be a question about terminology in theoretical CS. Also, when you write "If I have {1, 2, 3, 4}", what is "{1, 2, 3, 4}?" Is it an array? Is it a set? Is it a sequence? – Raymond Chen Oct 26 '14 at 00:51
  • what I meant is: given an array of integers eg, if I m asked to find all subsets & subarrays then are they both the same ? if not what would come under subset & what would come under a subarray? in such a scenario? – user2821242 Oct 26 '14 at 13:21

10 Answers10

47

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
Soyol
  • 763
  • 2
  • 10
  • 29
Sandip Pawar
  • 585
  • 4
  • 10
45

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.
Community
  • 1
  • 1
Wilson
  • 501
  • 4
  • 6
12

Consider these two properties in collection (array, sequence, set, etc) of elements: Order and Continuity.

Order is when you cannot switch the indices or locations of two or more elements (a collection with a single element has an irrelevant order).

Continuity is that an element must have their neighbors remain with them or be null.

A subarray has Order and Continuity.

A subsequence has Order but not Continuity.

A subset does not Order nor Continuity.

A collection with Continuity but not Order does not exist (to my knowledge)

user2361174
  • 1,872
  • 4
  • 33
  • 51
10

In the context of an array, SubSequence - need not be contigious but needs to maintain the order. But SubArray is contigious and inherently maintains the order.

if you have {1,2,3,4} --- {1,3,4} is a valid SubSequence but its not a subarray.

And subset is no order and no contigious.. So you {1,3,2} is a valid sub set but not a subsequence or subarray.

{1,2} is a valid subarray, subset and subsequence.

All Subarrays are subsequences and all subsequence are subset.

But sometimes subset and subarrays and sub sequences are used interchangably and the word contigious is prefixed to make it more clear.

Roma
  • 333
  • 3
  • 11
4

Per my understanding, for example, we have a list say [3,5,7,8,9]. here

subset doesn’t need to maintain order and has non-contiguous behavior. For example, [9,3] is a subset

subsequence maintain order and has non-contiguous behavior. For example, [5,8,9] is a subsequence

subarray maintains order and has contiguous behavior. For example, [8,9] is a subarray

nillu
  • 585
  • 4
  • 15
3

subarray: some continuous elements in the array

subset: some elements in the collection

subsequence: in most case, some elements in the array maintaining relative order (not necessary to be continuous)

GraceMeng
  • 949
  • 8
  • 6
3

A Simple and Straightforward Explanation:

Subarray: It always should be in contiguous form.

For example, lets take an array int arr=[10,20,30,40,50];

-->Now lets see its various combinations:

  subarr=[10,20] //true
  subarr=[10,30] //false, because its not in contiguous form
  subarr=[40,50] //true

Subsequence: which don't need to be in contiguous form but same order.

For example, lets take an array int arr=[10,20,30,40,50];

-->Now lets see its various combinations:

  subseq=[10,20]; //true
  subseq=[10,30]; //true
  subseq=[30,20]; //false, because order isn't maintained

Subset: which mean any possible combinations.

For example, lets take an array int arr=[10,20,30,40,50];

-->Now lets see its various combinations:

  subset={10,20}; //true
  subset={10,30}; //true
  subset={30,20}; //true
Safin Ghoghabori
  • 516
  • 1
  • 6
  • 17
2

Following Are Example of Arrays

Array : 1,2,3,4,5,6,7,8,9
Sub Array : 2,3,4,5,6 >> Contagious Elements in order
Sub Sequence : 2,4,7,8 >> Elements in order by skipping any or 0 elements
Subset : 9,5,2,1 >> Elements by skipping any or 0 elements but not in order
Kalpit Sharma
  • 21
  • 1
  • 3
0
Suppose an Array [3,4,6,7,9]

Sub Array is a continuous and ordered part of that array
example is [3,4,6],[7,9],[5]

Sub Sequence has not need to be continuous but they should be in order
example is [3,4,9],[3,7],[6]

Subset neither need to be continuous nor to be in order
Example is [9,4,7],[3,4],[5]
0

A subarray is a contiguous part of an array and maintains a relative ordering of elements. For an array/string of size n, there are n*(n+1)/2 non-empty subarrays/substrings.

A subsequence maintains a relative ordering of elements but may or may not be a contiguous part of an array. For a sequence of size n, we can have 2^n-1 non-empty sub-sequences in total.

A subset does not maintain a relative ordering of elements and is neither a contiguous part of an array. For a set of size n, we can have (2^n) sub-sets in total. Let us understand it with an example.

Consider an array:

array = [1,2,3,4]

Subarray : [1,2],[1,2,3] — is continuous and maintains relative order of elements

Subsequence: [1,2,4] — is not continuous but maintains relative order of elements

Subset: [1,3,2] — is not continuous and does not maintain the relative order of elements

Some interesting observations:

Every Subarray is a Subsequence. Every Subsequence is a Subset.