Given an array of array: S = {s1, s2, ... , sn}, where si = {i1, i2, ..., im} and every element in array si is the integer from 1 to n with m ≤ n/2. For example, S = {{1, 2, 3}, {1, 3, 4}, {2, 5, 6}, {4, 5, 6}}, where m = 3 and n = 6.
If for any element si, sj in S they are disjoint with |si| + |sj| ≤ n, we say si and sj are in the same group g.
Question: For a given array S, I wonder if there is an efficient algorithm to output the group with minimal count. Still the example mentioned previously:
INPUT: S = {{1, 2, 3}, {1, 3, 4}, {2, 5, 6}, {4, 5, 6}}
OUTPUT:G1 = {{1, 2, 3}, {4, 5, 6}}, G2 = {{1, 3, 4}, {2, 5, 6}}, GroupCount = 2
The only method I can figure out is BF, I hope anyone can give an answer to this question or give some materials about this.
Related
Given an array arr[] of N elements, the task is to find the maximum sum of lengths of all non-overlapping subarrays with K as the maximum element in the subarray.
Example 1:
Input: N = 9, K = 4
arr[] = {2, 1, 4, 9, 2, 3, 8, 3, 4}
Output: 5
Explanation: {2, 1, 4} => Length = 3
{3, 4} => Length = 2
So, 3 + 2 = 5 is the answer.
{2, 1, 4, 9, 2, 3, 8, 3, 4} in this array we can have {2,1,4}, {2,3} and {3,4 } subarray which satisfies the required condition but second subarray is not mentioned. Please tell me Why?
I'm trying to find the best way to solve the question: "Use Range, Reverse and Join to create {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}"
So basically the given lists are {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}.
I could easily solve this question but wanted to know if there is a better way (more efficient) than what i've come up with.:
My Solutions:
In[136]:= Join[ Reverse[Range[3]], Reverse[Range[4]], Reverse[Range[5]] ]
In[141]:= Reverse[Join[ Range[5], Range[4], Range[3] ]]
given lists: {1, 2, 3, 4, 5}, {1, 2, 3, 4}, {1, 2, 3}, where you have to use the functions Range, Reverse and Join to create the expected output:
{3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
My solution will not be efficient if there were to be 100 lists instead of three.
Thanks in advance for the help
RUNNING EACH ELEMENT OF A LIST THROUGH A FUNCTION:
listA = {}
Function[x, listA = Join[listA, x]] /# {Range[5], Range[4], Range[3]}
listB = Reverse[listA]
Clear[listA]
output:
result -> listB: {3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
Range[#] & /* Reverse /# {3, 4, 5} // Flatten
{3, 2, 1, 4, 3, 2, 1, 5, 4, 3, 2, 1}
Update
Someone voted to delete my answer without providing a reason. Perhaps because it did not use Join. To address that
Range[#] & /* Reverse /# {3, 4, 5} // Apply[Join]
I want to check if the matrices are of the same size: if both matrices have the same number of rows and the same number of columns.
matrix1 := [][]int{{1,2,3} ,{4,5,6}}
matrix2 := [][]int{{7,8,9}, {10,11,12}}
I get len(matrix1) == len(matrix2) == 2. Which is the correct number of rows.
How can I check the length of each row (i.e. the number of columns, which should be 3) if I'm declaring the matrices as shown above?
Note that since every "row" in a 2D slice may have arbitrary length, you should check the length of each of the corresponding rows (having the same index) if they are equal.
Here's a function that does that:
func match(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
for i, row1 := range m1 {
row2 := m2[i]
if len(row1) != len(row2) {
return false
}
}
return true
}
See usage examples:
m1 := [][]int{{1, 2, 3}, {4, 5, 6}}
m2 := [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12, 13}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}, {4, 5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
m1 = [][]int{{1, 2, 3}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12, 12}}
fmt.Println(match(m1, m2))
Output (as expected):
true
true
false
false
Simplification for Special case:
If you have guarantee that in all of your matrices all rows have the same length, you can make a big simplification: in this case if number of rows matches, it's enough to compare the length of one of the rows only from each matrices, practically the first row.
It could look like this:
func match2(m1, m2 [][]int) bool {
if len(m1) != len(m2) {
return false
}
return len(m1) == 0 || len(m1[0]) == len(m2[0])
}
Testing it:
m1 = [][]int{{1, 2, 3}, {4, 5, 6}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
m1 = [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
m2 = [][]int{{7, 8, 9}, {10, 11, 12}}
fmt.Println(match2(m1, m2))
Output:
true
false
Try these on the Go Playground.
Let's say I have a list of [1,2,3,4] I want to produce all subsets of this set which covers all members once, the result should has 15 lists which the order isn't important, instead t provides all possible subgroups:
>>>>[[1,2,3,4]]
[[1][2][3][4]]
[[1,2],[3][4]]
[[1,2],[3,4]]
[[1][2],[3,4]]
[[1,3],[2][4]]
[[1,3],[2,4]]
[[1][3],[2,4]]
[[1],[2,3][4]]
[[1,4],[2,3]]
[[1][2,3,4]]
[[2][1,3,4]]
[[3][1,2,4]]
[[4][1,2,3]]
This is a set partitioning problem or partitions of a set which is discussed here, but the response made me confused as it just suggests recalling permutations, but I don't know how! and another response also doesn't include [1,3]
Meanwhile I should solve this problem for high numbers and the result should provide Bell Number
Sorry I'm quite new to python and became confused. Could anyone make t clear for me?
Instead of doing all permutations and remove the duplicates, which was my initial thought, then you can use this recursive function, which I found here and here:
def partitions(set_):
if not set_:
yield []
return
for i in range(int(2**len(set_)/2)):
parts = [set(), set()]
for item in set_:
parts[i&1].add(item)
i >>= 1
for b in partitions(parts[1]):
yield [parts[0]]+b
l = [1, 2, 3, 4]
for p in reversed(sorted(partitions(l))):
print(p)
print('The Bell number is', len(list(partitions(l))))
It prints:
[{1, 2, 3, 4}]
[{1, 2, 4}, {3}]
[{1, 4}, {2, 3}]
[{1, 4}, {3}, {2}]
[{2, 4}, {1, 3}]
[{2, 4}, {3}, {1}]
[{1, 3, 4}, {2}]
[{2, 3, 4}, {1}]
[{3, 4}, {1, 2}]
[{3, 4}, {2}, {1}]
[{4}, {1, 2, 3}]
[{4}, {1, 3}, {2}]
[{4}, {2, 3}, {1}]
[{4}, {3}, {1, 2}]
[{4}, {3}, {2}, {1}]
The Bell number is 15
I want to write program with input an array which contains N sub-array(s). Each sub-array is an array of integers which has M element(s). N and M can be very large (e.g. 1.000.000). It assume that the arrays can be stored in physical memory. The program would output: All sets of integers which appear in at least K or more than K sub-array(s). Each set must have exactly L elements.
Ex:
Input:
N = 5
{1, 2, 3, 4, 5}
{1, 3, 2}
{1, 4, 3, 2, 9}
{2, 3, 4, 7, 9}
{3, 4, 5, 9, 10}
Output:
1. In case K = 3, L = 2
{1, 2}
{1, 3}
{2, 3}
{2, 4}
{3, 4}
{3, 9}
{4, 9}
2. In case K = 3, L = 3
{1, 2, 3}
{2, 3, 4}
How can I do ?
thanks