For a sequence of n distinct elements, like for example {0, 1, 2, ..., n-1}, where n is even, it is possible to identify n permutations of the sequence that are completely dissimilar from each other. If n is odd, then it is possible to identify n-1 such permutations. Note, this is just a claim and not yet a fact.
By completely dissimilar (which might not be accurate), I mean that there is no "adjacent and ordered pair" of elements, like (0,1), (2,3) and (n-3, n-2), from a certain permutation that repeats in any of the remaining permutations.
For example:
If n=2, then we can identify {0, 1} and {1, 0} as two completely dissimilar permutations.
If n=3, then we can identify {0, 1, 2} and {2, 1, 0} as two completely dissimilar permutations.
If n=4, then we can identify {0, 1, 2, 3}, {1, 3, 0, 2}, {2, 0, 3, 1} and {3, 2, 1, 0} as four completely dissimilar permutations.
If n=5, then we can identify {0, 1, 2, 3, 4}, {1, 4, 2, 0, 3}, {3, 0, 2, 4, 1}, {4, 3, 2, 1, 0} as four completely dissimilar permutations.
I will like to know:
1) Is there a general rule or algorithm to find, given a sequence, any n (if n is even) or n-1 (if n is odd) completely dissimilar permutations of that sequence?
2) Is there a formal definition of this problem?
Related
I'm doing some data processing on data that comes in sets of 3 thousands of values long. Sometimes the arrays are of slightly different lengths, and I am trying to find a way to find the minimal length array and match the other two to that.
# Some randomly generated sequences
a = array([7, 1, 7, 8, 0, 0, 1, 2, 8, 7, 2, 3])
b = array([0, 1, 1, 8, 3, 4, 1, 5])
c = array([8, 3, 3, 1, 4, 6, 6, 7, 3, 8, 8])
# What I'd like accomplished
a = array([7, 1, 7, 8, 0, 0, 1, 2])
b = array([0, 1, 1, 8, 3, 4, 1, 5])
c = array([8, 3, 3, 1, 4, 6, 6, 7])
This problem seems well covered for 2 arrays of different lengths but my searches didn't bring up anything for matching the lengths of multiple arrays. Looking at some of the Numpy methods like resize and array_split didn't seem to have the functionality I was looking for. Before I dive into writing some type of ugly recursive function using the directions I found matching 2 arrays, does anyone have any suggestions about how this can be accomplished conveniently?
First we can do return the min length
mlen = min(map(len, [a, b, c]))
8
Then
newl=[x[: mlen ] for x in [a,b,c]]
i have stumbled upon a problem where i am given a 5x5 matrix in the form of a 2D array and i am supposed to find the minimum sum of 5 elements where each element should be in unique row and column and print the indices of those elements and the minimum sum.
The problem gave 3 test cases as example.
Test case 1:
{
{5, 4, 4, 1, 6},
{1, 3, 2, 4, 6},
{3, 2, 3, 2, 6},
{0, 4, 5, 4, 6},
(6, 6, 6, 6, 6}
};
Output: (3,0) (2,1) (1,2) (0,3) (4,4)
Minimum sum: 11
Test case 2:
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
};
Output: (0,0) (1,1) (2,2) (3,3) (4,4)
Minimum sum: 0
Test case 3:
{
{1, 2, 3, 4, 5},
{5, 4, 3, 2, 1},
{1, 2, 7, 4, 5},
{5, 4, 3, 2, 1},
{1, 2, 3, 4, 5},
};
Output: (0,0) (2,1) (4,2) (1,3) (3,4)
Minimum sum: 9
I would like to understand what is meant when they say unique row and column and all i can see from the test cases is that the column indices are starting from 0 and increasing by one for each pair of indices.I would like to know how to approach this problem.
Unique row and column means that no two elements share a row or a column.
Here, I've highlighted the selected numbers.
You can see that when a number is selected, No other number in that same column is also selected. No other number in that same row is selected.
5 4 4 1 6
1 3 2 4 6
3 2 3 2 6
0 4 5 4 6
6 6 6 6 6
1 + 2 + 2 + 0 + 6 = 11
I have an array with N elements (not sorted). Each element of the array can be any integer (positive or negative). Now I have to partitioned this array in a way that absolute(sum(sub_array1) - sum(sub_array2)) is minimal.
Example:
A = {3, 4, 1, 2, -5}
partition1 = sub_array1 {3}, sub_array2 {4, 1, 2, -5} => abs(3-2) = 1
partition2 = sub_array1 {3, 4}, sub_array2 {1, 2, -5} => abs(7-(-2)) = 9
partition3 = sub_array1 {3, 4, 1}, sub_array2 {2, -5} => abs(8-(-3)) = 11
partition4 = sub_array1 {3, 4, 1, 2}, sub_array2{-5} => abs(10-(-5)) = 15
Answer = 1
I have achieved the solution with O(N^2), but I want to do it with at least O(Nlog(N)), and without threading (parallel solution).
This can be done in linear time, by observing that the sum of the two partitions is constant:
Sum up all elements in the array (call this S).
Iterate over the array, computing the partial sum (call this P_i at step i).
While iterating, find i such that abs(P_i - (S - P_i)) is minimised.
First of all, multithreading doesn't help with complexity, unless you have an infinite number of threads.
You can do it in O(N). Here's a general direction - loop over the possible partitions (N+1), and calculate each partition from the previous one (in each iteration you move one item from on partition to the other).
So, I've searched a bit, but not entirely sure what to search on, tbh...
I'm currently doing some "level generation" code, and creating objects to contain the objects for my generation.
Basically I have a "Cell" class which is defined by a Coordinate.
I'm then trying to create a "CellArea" class, which holds multiple cells in an area. It'd be simple enough if this was a rectangle, as I'd just use Cell[, ] then. But since this could be an "L" shape of sorts (I'm fine with limiting it to 2 "Corridors"), how would I go about doing that? Or is it simply more efficient to do some sort of list/collection?
I was wondering if you can do a Jagged Array that'd look something like...
{0, 1, 2}
{0, 1, 2}
{0, 1, 2}
{0, 1, 2}
{0, 1, 2, 3, 4, 5}
{0, 1, 2, 3, 4, 5}
{0, 1, 2, 3, 4, 5}
I hope I made my question clear? Obviously I could do a bidim array that is [7, 6] and then not fill in the "null" places, but isn't that inefficient in a way?
Yes of course you can!
int[][] jaggedArray = new int[7][];
jaggedArray[0] = new int[] {0, 1, 2};
jaggedArray[1] = new int[] {0, 1, 2};
jaggedArray[2] = new int[] {0, 1, 2};
jaggedArray[3] = new int[] {0, 1, 2};
jaggedArray[4] = new int[] {0, 1, 2, 3, 4, 5};
jaggedArray[5] = new int[] {0, 1, 2, 3, 4, 5};
jaggedArray[6] = new int[] {0, 1, 2, 3, 4, 5};
Reference: http://msdn.microsoft.com/en-us/library/2s05feca.aspx
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