Subset Range
The subset range problem involves determining if a given multiset is a subrange of another multiset - that is, if all the elements of the first set occur with at least the same frequency in the second set.
For example:
{1, 2, 2, 3} is a subrange of {1, 1, 1, 2, 2, 3, 4}
But, {1, 2, 3} is NOT a subrange of {1, 1, 2, 2, 4} (3 does not occur enough times).
Java example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| boolean isSubrange(int[] subset, int[] multiset) {
Map<Integer, Integer> freq = new HashMap<>();
for (int n : multiset) {
freq.put(n, freq.getOrDefault(n, 0) + 1);
}
for (int n : subset) {
if (!freq.containsKey(n)) {
return false;
}
freq.put(n, freq.get(n) - 1);
if (freq.get(n) < 0) {
return false;
}
}
return true;
}
|
C++ example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| bool isSubrange(vector<int> subset, vector<int> multiset) {
unordered_map<int, int> freq;
for (int n : multiset) {
freq[n]++;
}
for (int n : subset) {
if (!freq.count(n)) {
return false;
}
freq[n]--;
if (freq[n] < 0) {
return false;
}
}
return true;
}
|
Python example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| from collections import Counter
def is_subrange(subset, multiset):
freq = Counter(multiset)
for n in subset:
if n not in freq:
return False
freq[n] -= 1
if freq[n] < 0:
return False
return True
|
Subset range queries have applications in scheduling, collections, and arrays.
Find All Ranges of Contiguous Subsets
The concept of “Subset Range” refers to finding all contiguous subsets within an array that fall within a specified range [min, max]
. Each subset should have all its elements within this range. This is a common problem in computer science and can be solved using various techniques, including iterative methods or sliding window algorithms.
Example Code
Java
In Java, you can solve the Subset Range problem using nested loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| import java.util.ArrayList;
import java.util.List;
public class SubsetRange {
public static List<List<Integer>> findSubsetsInRange(int[] arr, int min, int max) {
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
List<Integer> subset = new ArrayList<>();
for (int j = i; j < arr.length; j++) {
if (arr[j] >= min && arr[j] <= max) {
subset.add(arr[j]);
} else {
break;
}
}
if (!subset.isEmpty()) {
result.add(subset);
}
}
return result;
}
}
|
C++
In C++, the code structure will look similar but uses vectors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> findSubsetsInRange(vector<int> arr, int min, int max) {
vector<vector<int>> result;
for (int i = 0; i < arr.size(); ++i) {
vector<int> subset;
for (int j = i; j < arr.size(); ++j) {
if (arr[j] >= min && arr[j] <= max) {
subset.push_back(arr[j]);
} else {
break;
}
}
if (!subset.empty()) {
result.push_back(subset);
}
}
return result;
}
|
Python
Python allows for a more concise representation.
1
2
3
4
5
6
7
8
9
10
11
12
| def find_subsets_in_range(arr, min_val, max_val):
result = []
for i in range(len(arr)):
subset = []
for j in range(i, len(arr)):
if min_val <= arr[j] <= max_val:
subset.append(arr[j])
else:
break
if subset:
result.append(subset)
return result
|
Key Takeaways
- Subset Range is a problem focused on finding all contiguous subsets of an array that fall within a specified range
[min, max]
. - It can be solved efficiently using nested loops, iterating through the array and checking each element’s value against the min-max range.
- The implementations in Java, C++, and Python are straightforward, using standard language features like lists or vectors to store the subsets.