# Subarrays, Subsequences, and Subsets in Array

## What is a Subarray?

A

subarrayis a contiguous part of array, i.e., Subarray is an array that is inside another array.

In general, for an array of size n, there are **n*(n+1)/2** non-empty subarrays.

For example, Consider the array [1, 2, 3, 4], There are 10 non-empty sub-arrays. The subarrays are:

(1), (2), (3), (4), (1,2), (2,3), (3,4), (1,2,3), (2,3,4), and (1,2,3,4)

## What is a Subsequence?

A

subsequenceis a sequence that can be derived from another sequence by removing zero or more elements, without changing the order of the remaining elements.

More generally, we can say that for a sequence of size n, we can have **(2 ^{n} – 1)** non-empty sub-sequences in total.

For the same above example, there are 15 sub-sequences. They are:

(1), (2), (3), (4), (1,2), (1,3),(1,4), (2,3), (2,4), (3,4), (1,2,3), (1,2,4), (1,3,4), (2,3,4), (1,2,3,4).

## What is a Subset?

If a Set has all its elements belonging to other sets, this set will be known as a

subsetof the other set.

A Subset is denoted as â€œ**âŠ†**â€œ. If set A is a subset of set B, it is represented as **A âŠ† B**.

For example, Let Set_A = {m, n, o, p, q}, Set_ B = {k, l, m, n, o, p, q, r}

Then, A âŠ† B.

**Topics:**

## Problems on Subarray:

**Easy Problems on Subarray:**- Split an array into two equal Sum subarrays
- Check if subarray with given product exists in an array
- Subarray of size k with given sum
- Sort an array where a subarray of a sorted array is in reverse order
- Count subarrays with all elements greater than K
- Maximum length of the sub-array whose first and last elements are same
- Check whether an Array is Subarray of another Array
- Find array such that no subarray has xor zero or Y
- Maximum subsequence sum such that all elements are K distance apart
- Longest sub-array with maximum GCD
- Count of subarrays with sum at least K
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Sum of all subarrays of size K
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Find an array of size N having exactly K subarrays with sum S
- Find the subarray of size K with minimum XOR
- Length of the longest alternating even odd subarray
- Count of subarrays which start and end with the same element
- Count of subarrays having exactly K perfect square numbers
- Split array into two subarrays such that difference of their maximum is minimum

**Intermediate problems on Subarray:**- Print all K digit repeating numbers in a very large number
- Length of longest subarray whose sum is not divisible by integer K
- Min difference between maximum and minimum element in all Y size subarrays
- Longest subarray of non-empty cells after removal of at most a single empty cell
- First subarray with negative sum from the given Array
- Largest subarray with frequency of all elements same
- Bitwise operations on Subarrays of size K
- Count subarrays having sum of elements at even and odd positions equal
- Longest Subarray consisting of unique elements from an Array
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Split array into two subarrays such that difference of their sum is minimum
- Maximize count of non-overlapping subarrays with sum K
- Smallest subarray which upon repetition gives the original array
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Maximize product of subarray sum with its minimum element
- Sum of products of all possible Subarrays
- Check if all subarrays contains at least one unique element
- Length of smallest subarray to be removed such that the remaining array is sorted
- Length of longest subarray having frequency of every element equal to K
- Length of the longest increasing subsequence which does not contain a given sequence as Subarray

**Hard Problems on Subarray:**- Length of smallest subarray to be removed to make sum of remaining elements divisible by K
- Maximum length of same indexed subarrays from two given arrays satisfying the given condition
- Count ways to split array into two equal sum subarrays by changing sign of any one array element
- Longest subarray in which all elements are smaller than K
- Maximize product of a strictly increasing or decreasing subarray
- Sum of maximum of all subarrays by adding even frequent maximum twice
- Longest subarray of an array which is a subsequence in another array
- Count of subarrays having product as a perfect cube
- Minimize difference between maximum and minimum array elements by removing a K-length subarray
- Maximum sum submatrix
- Minimum removal of elements from end of an array required to obtain sum K
- Check if any subarray of length M repeats at least K times consecutively or not
- Minimize flips on K-length subarrays required to make all array elements equal to 1
- Split array into K subarrays such that sum of maximum of all subarrays is maximized
- Find minimum subarray sum for each index i in subarray [i, N-1]
- Longest subarray with GCD greater than 1
- Longest subsegment of â€˜1â€™s formed by changing at most k â€˜0â€™s
- Lexicographically smallest Permutation of Array by reversing at most one Subarray
- Find a subsequence which upon reversing gives the maximum sum subarray
- Minimize steps to make Array elements 0 by reducing same A[i] â€“ X from Subarray

## Problems on Subsequence:

**Easy Problems on Subsequence:**- Longest subsequence having equal numbers of 0 and 1
- Powers of two and subsequences
- Longest Subsequence where index of next element is arr[arr[i] + i]
- Number of subsequences with zero sum
- Longest sub-sequence with maximum GCD
- Maximum Bitwise AND value of subsequence of length K
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Maximum product of bitonic subsequence of size 3
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Longest subsequence of even numbers in an Array
- Maximum length Subsequence with alternating sign and maximum Sum
- Count of possible subarrays and subsequences using given length of Array
- Maximum bitwise OR value of subsequence of length K
- Count of subsequences consisting of the same element
- Smallest occurring element in each subsequence
- Length of the longest subsequence consisting of distinct elements
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Maximize subsequences having array elements not exceeding length of the subsequence
- Length of longest subsequence consisting of distinct adjacent elements
- Maximum Sum Subsequence

**Intermediate Problems on Subsequence:**- Minimum removals required to make a given array Bitonic
- Check if a non-contiguous subsequence same as the given subarray exists or not
- Minimize the number of strictly increasing subsequences in an array
- Count of unique subsequences from given number which are power of 2
- Minimum number of insertions required such that first K natural numbers can be obtained as sum of a subsequence of the array
- Length of longest subsequence such that prefix sum at every element remains greater than zero
- Check if given Array can be divided into subsequences of K increasing consecutive integers
- Longest Subsequence such that difference between adjacent elements is either A or B
- Count subsequences of Array having single digit integer sum K
- Shortest Subsequence with sum exactly K
- Printing Longest Bitonic Subsequence
- Sorted subsequence of size 3 in linear time using constant space
- Count of subsequences having maximum distinct elements
- Construct array having X subsequences with maximum difference smaller than d
- Print all subsequences of a string using ArrayList
- Longest Subsequence with at least one common digit in every element
- Maximum Sum Subsequence of length k
- Sum of minimum element of all sub-sequences of a sorted array
- Find all combinations of two equal sum subsequences
- Minimum cost of choosing 3 increasing elements in an array of size N

**Hard Problems on Subsequence:**- Number of subsequences with positive product
- Longest subsequence having difference atmost K
- Find all subsequences with sum equals to K
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Count of subsequences of length atmost K containing distinct prime elements
- Sum of all subsequences of length K
- Minimize sum of smallest elements from K subsequences of length L
- Unique subsequences of length K with given sum
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Maximize product of same-indexed elements of same size subsequences
- Longest Increasing Subsequence having sum value atmost K
- Longest subsequence of a number having same left and right rotation
- Maximize length of Non-Decreasing Subsequence by reversing at most one Subarray
- Maximum subsequence sum possible by multiplying each element by its index
- Generate all distinct subsequences of array using backtracking
- Maximum subsequence sum such that no K elements are consecutive
- Print all possible K-length subsequences of first N natural numbers with sum N
- Longest subsequence having difference between the maximum and minimum element equal to K
- Maximize difference between sum of even and odd-indexed elements of a subsequence
- Convert an array into another by repeatedly removing the last element and placing it at any arbitrary index

## Problems on Subset:

**Easy Problems on Subset:**- Find if there is any subset of size K with 0 sum in an array of -1 and +1
- Sum of sum of all subsets of a set formed by first N natural numbers
- Count of subsets not containing adjacent elements
- Sum of the sums of all possible subsets
- Find whether an array is subset of another array
- Total number of Subsets of size at most K
- Check if it is possible to split given Array into K odd-sum subsets
- Partition a set into two subsets such that difference between max of one and min of other is minimized
- Sum of all possible expressions of a numeric string possible by inserting addition operators
- Check if itâ€™s possible to split the Array into strictly increasing subsets of size at least K
- Largest subset of Array having sum at least 0

**Intermediate Problems on Subset:**- Number of subsets with sum divisible by m
- Fibonacci sum of a subset with all elements <= k
- Number of possible Equivalence Relations on a finite set
- Largest divisible pairs subset
- Recursive program to print all subsets with given sum
- Subset Sum Queries in a Range using Bitset
- Find all distinct subset (or subsequence) sums of an array | Set-2
- Sum of (maximum element â€“ minimum element) for all the subsets of an array
- Count no. of ordered subsets having a particular XOR value
- Sum of subsets of all the subsets of an array
- Perfect Sum Problem
- Count of subsets having sum of min and max element less than K
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Check if an array can be split into subsets of K consecutive elements

**Hard Problems on Subset:**- Minimize count of divisions by D to obtain at least K equal array elements
- Split array into K-length subsets to minimize sum of second smallest element of each subset
- Median of all non-empty subset sums
- Minimum removals required such that sum of remaining array modulo M is X
- Sum of length of two smallest subsets possible from a given array with sum at least K
- Reduce sum of any subset of an array to 1 by multiplying all its elements by any value
- Sum of all subsets whose sum is a Perfect Number from a given array
- Minimize sum of incompatibilities of K equal-length subsets made up of unique elements
- Maximize sum of subsets from two arrays having no consecutive values
- Product of the maximums of all subsets of an array
- Count ways to place â€˜+â€™ and â€˜-â€˜ in front of array elements to obtain sum K
- Count ways to split array into two subsets having difference between their sum equal to K
- Find the subset of Array with given LCM
- Count of subsets whose product is multiple of unique primes
- Minimum count of elements to be inserted in Array to form all values in [1, K] using subset sum
- Maximum subset sum having difference between its maximum and minimum in range [L, R]
- Find all unique subsets of a given set using C++ STL
- Subset sum problem where Array sum is at most N

**Data Structure and Algorithms Course**

**Recent articles on Subarray**

**Recent articles on Subsequence**

**Recent articles on Subset**

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above