How to find all combinations of items of multiple arrays - arrays

Lets say these are the start arrays:
[a,b,c]
[d]
[e,f]
What algorithm could produce the following arrays?
[a,d,e]
[a,d,f]
[b,d,e]
[b,d,f]
[c,d,e]
[c,d,f]
The number of start arrays can vary.

Depends on language, but formally something like this (when you have 3 arrays as you specified)
for el1 in first_array do
for el2 in second_array do
for el3 in third_array do
begin
create new element in result array as [e1, el2, el3]
end

The simplest algorithm you can think of is the best you can have. As the answer is of size the multiplied dimensions of all the arrays not much of an improvement can be made here. I personally recommend using recursion as the number of arrays can not be too big without making the number of resulting arrays really huge.

Let there are k arrays of n1, n2... nk elements respectively.
Writing all combinations is very like to writing all numbers in mixed radix.
So, simply loop over all possible numbers from 0 to (n1n2...nk-1) and write it down in mixed radix representation with "digits" taken from your arrays - only two nested loops are required.

Another method is a graphical one, you start with the originals sets and you move their content clockwise and store the combinations. Example first rotate the last row, and after the last rotation move the last - 1 row (in this case d, but you can't rotate it) so you will rotate the first row. It's like the binary sum.
[a,b,c] [a,b,c]---->[b,c,a] [b,c,a]---->[c,a,b] [c,a,b]
[d] [d] [d] [d] [d] [d]
[e,f]------>[f,e]------>[e,f]------>[f,e]------>[e,f]------>[f,e]
PD: you will only save the first elements of each array always.

The proposed solution above has a big problem in the sense the developer needs to know the number of arrays ahead and create the number of loops accordingly.
The following solution in C# does it dynamically based on the number of arrays that you actually have and it's type agnostic:
static void Main(string[] args)
{
List<List<char>> masterListChar = new List<List<char>>();
List<char> c1 = new List<char>() { 'a', 'b', 'c' };
List<char> c2 = new List<char>() { 'd' };
List<char> c3 = new List<char>() { 'e', 'f'};
masterListChar.Add(c1);
masterListChar.Add(c2);
masterListChar.Add(c3);
//PrintCombinations(masterListInt);
PrintCombinations(masterListChar);
Console.ReadKey();
}
public static void PrintCombinations<T>(List<List<T>> masterArray)
{
T[] combination = new T[masterArray.Count];
WalkOnSubArray(combination, masterArray, 0);
}
public static void WalkOnSubArray<T>(T[] combination, List<List<T>> masterArray, int masterIndex)
{
List<T> currentArray = masterArray[masterIndex];
for (int i = 0; i < currentArray.Count; ++i)
{
combination[masterIndex] = currentArray[i];
if(masterIndex != masterArray.Count - 1)
WalkOnSubArray(combination, masterArray, masterIndex + 1);
else
Console.WriteLine(String.Join(",", combination));
}
}

Related

given 3 arrays check if there is any common number

**I have 3 arrays a[1...n] b[1...n] c[1....n] which contain integers.
It is not mentioned if the arrays are sorted or if each array has or has not duplicates.
The task is to check if there is any common number in the given arrays and return true or false.
For example : these arrays a=[3,1,5,10] b=[4,2,6,1] c=[5,3,1,7] have one common number : 1
I need to write an algorithm with time complexity O(n^2).
I let the current element traversed in a[] be x, in b[] be y and in c[] be z and have following cases inside the loop : If x, y and z are same, I can simply return true and stop the program,something like:
for(x=1;x<=n;x++)
for(y=1;y<=n;y++)
for(z=1;z<=n;z++)
if(a[x]==b[y]==c[z])
return true
But this algorithm has time complexity O(n^3) and I need O(n^2).Any suggestions?
There is a pretty simple and efficient solution for this.
Sort a and b. Complexity = O(NlogN)
For each element in c, use binary search to check if it exists in both a and b. Complexity = O(NlogN).
That'll give you a total complexity of O(NlogN), better than O(N^2).
Create a new array, and save common elements in a and b arrays. Then find common elements in this array with c array.
python solution
def find_d(a, b, c):
for i in a:
for j in b:
if i==j:
d.append(i)
def findAllCommon(c, d):
for i in c:
for j in d:
if i==j:
e.append(i)
break
a = [3,1,5,10]
b = [4,2,6,1]
c = [5,3,1,7]
d = []
e = []
find_d(a, b, c)
findAllCommon(c, d)
if len(e)>0:
print("True")
else:
print("False")
Since I haven't seen a solution based on sets, so I suggest looking for how sets are implemented in your language of choice and do the equivalent of this:
set(a).intersection(b).intersection(c) != set([])
This evaluates to True if there is a common element, False otherwise. It runs in O(n) time.
All solutions so far either require O(n) additional space (creating a new array/set) or change the order of the arrays (sorting).
If you want to solve the problem in O(1) additional space and without changing the original arrays, you indeed can't do better than O(n^2) time:
foreach(var x in a) { // n iterations
if (b.Contains(x) && c.Contains(x)) return true; // max. 2n
} // O(n^2)
return false;
A suggestion:
Combine into a single array(z) where z = sum of the entries in each array. Keep track of how many entries there were in Array 1, Array 2, Array 3.
For each entry Z traverse the array to see how many duplicates there are within the combined array and where they are. For those which have 2 or more (ie there are 3 or more of the same number), check that the location of those duplicates correspond to having been in different arrays to begin with (ruling our duplicates within the original arrays). If your number Z has 2 or more duplicates and they are all in different arrays (checked through their position in the array) then store that number Z in result array.
Report result array.
You will traverse the entire combined array once and then almost (no need to check if Z is a duplicate of itself) traverse it again for each Z, so n^2 complexity.
Also worth noting that the time complexity will now be a function of total number of entries and not of number of arrays (your nested loops would become n^4 with 4 arrays - this will stay as n^2)
You could make it more efficient by always checking the already found duplicates before checking for a new Z - if the new Z is already found as a duplicate to an earlier Z you need not traverse to check for that number again. This will make it more efficient the more duplicates there are - with few duplicates the reduction in number of traverses is probably not worth the extra complexity.
Of course you could also do this without actually combining the values into a single array - you would just need to make sure that your traversing routine looks through the arrays and keeps track of what it finds the in the right order.
Edit
Thinking about it, the above is doing way more than you want. It would allow you to report on doubles, quads etc. as well.
If you just want triples, then it is much easier/quicker. Since a triple needs to be in all 3 arrays, you can start by finding those numbers which are in any of the 2 arrays (if they are different lengths, compare the 2 shortest arrays first) and then to check any doublets found against the third array. Not sure what that brings the complexity down to but it will be less than n^2...
there are many ways to solve this here few selected ones sorted by complexity (descending) assuming n is average size of your individual arrays:
Brute force O(n^3)
its basicaly the same as you do so test any triplet combination by 3 nested for loops
for(x=1;x<=n;x++)
for(y=1;y<=n;y++)
for(z=1;z<=n;z++)
if(a[x]==b[y]==c[z])
return true;
return false;
slightly optimized brute force O(n^2)
simply check if each element from a is in b and if yes then check if it is also in c which is O(n*(n+n)) = O(n^2) as the b and c loops are not nested anymore:
for(x=1;x<=n;x++)
{
for(ret=false,y=1;y<=n;y++)
if(a[x]==b[y])
{ ret=true; break; }
if (!ret) continue;
for(ret=false,z=1;z<=n;z++)
if(a[x]==c[z])
{ ret=true; break; }
if (ret) return true;
}
return false;
exploit sorting O(n.log(n))
simply sort all arrays O(n.log(n)) and then just traverse all 3 arrays together to test if each element is present in all arrays (single for loop, incrementing the smallest element array). This can be done also with binary search like one of the other answers suggest but that is slower still not exceeding n.log(n). Here the O(n) traversal:
for(x=1,y=1,z=1;(x<=n)&&(y<=n)&&(z<=n);)
{
if(a[x]==b[y]==c[z]) return true;
if ((a[x]<b[y])&&(a[x]<c[z])) x++;
else if ((b[y]<a[x])&&(b[y]<c[z])) y++;
else z++;
}
return false;
however this needs to change the contents of arrays or need additional arrays for index sorting instead (so O(n) space).
histogram based O(n+m)
this can be used only if the range of elements in your array is not too big. Let say the arrays can hold numbers 1 .. m then you add (modified) histogram holding set bit for each array where value is presen and simply check if value is present in all 3:
int h[m]; // histogram
for(i=1;i<=m;i++) h[i]=0; // clear histogram
for(x=1;x<=n;x++) h[a[x]]|=1;
for(y=1;y<=n;y++) h[b[y]]|=2;
for(z=1;z<=n;z++) h[c[z]]|=4;
for(i=1;i<=m;i++) if (h[i]==7) return true;
return false;
This needs O(m) space ...
So you clearly want option #2
Beware all the code is just copy pasted yours and modified directly in answer editor so there might be typos or syntax error I do not see right now...

How to find all options with repetition?

I'm looking at a problem I can't really still figure out.
I need all (now what's the right math term) permutations? tuples? combinations? with repetition made out of 4 given elements.
I've got the elements A, B, C, D. There is four of them, the amount is fixed. For a given n, I need to be able to get all possible options from these four elements. For example:
n = 1;
Possible options:
A
B
C
D
n = 2;
Possible options:
AA
AB
AC
AD
BA
BB
BC
BD
...
DC
DD
n = 4; Possible options: AAAA
AAAB
AAAC
...
DDDC
DDDD
Would anyone be able to kind of direct me somewhere? There are some following conditions, but I should be able to filter them on the go. Of course I tried searching for the answer, but no topics seem to be the same issue I'm trying to solve.
Big thanks to anyone who would at least try to pinch me in the right direction a bit.
You can not do it using nested loops because you need n for loops and n is not determined at first (and it is ridiculous to use n nested for loops). One approach to solving the problem is to use recursive functions. The recursive function should return the basic list consisting of 4 letters for n=1 and for n>1 it should call itself with parameter (n-1) and then append each one of the four letters to each returned string from the recursive call. I suggest you to try to implement it yourself before reading the following pseudo code!
list func(int n){
if ( n == 1 ) {return list('A', 'B', 'C', 'D'); }
else {
result = list();
permutations = func(n-1);
for (each item in permutations) {
result.append(item+'A');
result.append(item+'B');
result.append(item+'C');
result.append(item+'D');
return result;
}
}
}

Finding the amount of different elements at array

We have an array at size n. How we can find how many different types of elements we have at n and what is the amount of each one?
For example: at {1,-5,2,-5,2,7,-5,-5} we have 4 different types, and the array of the amounts will be: {1,2,1,4}.
So my questions are:
How we can find how many different elements there is at the array?
How we can count the amount if each one?
Now, I try to solve it at Omega(n), I try a lot but I didn't find a way. I try to solve it with hash-tables.
You are trying to get frequency of an element in an array.
Initialize a Hash where every new key is initialized with value 0.
Loop through array and add this key to hash and increment the value.
In JavaScript:
hash = {};
a = [1,-5,2,-5,2,7,-5,-5];
for(var i = 0; i < a.length; ++i) {
if(hash[a[i]] === undefined)
hash[a[i]] = 0
hash[a[i]] = hash[a[i]] + 1;
}
console.log(hash.toSource());
The syntax and specific data structures you use will vary between languages, but the basic idea would be to store a running count of the number of instances of each value in an associative data structure (HashMap, Dictionary, whatever your language calls it).
Here is an example that will work in Java (I took a guess at the language you were using).
It's probably bad Java, but it illustrates the idea.
int[] myArray = {1,-5,2,-5,2,7,-5,-5};
HashMap<Object,Integer> occurrences = new HashMap<Object,Integer>();
for (int i=0;i<myArray.length;i++)
{
if (occurrences.get(myArray[i]) == null)
{
occurrences.put(myArray[i],1);
}
else
{
occurrences.put(myArray[i],occurrences.get(myArray[i])+1);
}
}
You can then use your HashMap to look up the distinct elements of the array like this
occurrences.keySet()
Other languages have their own HashSet implementations (Dictionaries in .NET and Python, Hashes in Ruby).
There are different approaches to solve this problem.The question that asked here might be asked in different ways.Here the the simple way to do it with std::map which is available in STL libraries.But remember it will be always sort by key.
int arr[]={1,-5,2,-5,2,7,-5,-5};
int n=sizeof(arr)/sizeof(arr[0]);
map<int,int>v;
for(int i=0;i<n;i++)
{
if(v[arr[i]])
v[arr[i]]++;
else
v[arr[i]]=1;
}
map<int,int>::iterator it;
for(it=v.begin();it!=v.end();++it)
cout<<it->first<<" "<<it->second<<endl;
return 0;
it will show output like
-5 4
1 1
2 2
7 1
I suggest you read about 'Count Sort'
Although i am not sure i understood correctly what you actually want to ask. Anyway, i think you want to:
1.) Scan an array and come up with the frequency of each unique element in that array.
2.) Total amount of unique elements
3.) all that in linear computational time
I think, what you need is Counting Sort. See algo on wiki.
You can obviously skip the sorting part. But you must see how it does the sorting (the useful part for your problem). It, first, calculates a histogram (array of size nominally equal to the number of unique elements in you original array) of frequency of each key. This works for integers only (although you can always sort other types by putting integer pointers).
So, every index of this histogram array will correspond to an element in your original array, and the value at this index will correspond to the frequency of this element in the original array.
For Example;
your array x = {3, 4, 3, 3, 1, 0, 1, 3}
//after calculation, you will get
your histogram array h[0 to 4] = {1, 2, 0, 4, 1}
i hope that is what you asked

Sorting Numbers with arrays

Im really new to programming in Java so any answers don't be shy in really dumbing it down as much as possible.
I'm trying to create a program that will take an array value and sort it from smallest to largest number. This is what I've got so far:
public class ArraySwap
{
public static void main(String[] args)
{
int[a] = new int[4];
a[0] = 5;
a[1] = 7;
a[2] = 2;
a[3] = 1;
for (int i = a.length-1;
Thats what I've got so far, but I've no idea what to use in the for loop, it has to be an actual code formula with a for loop so no using array.sort or anything like that.
The output should re-arrange the numbers so they display 1 2 5 7 instead of 5 7 2 1 which is what they would be if I just had it print them out down the list.
My teacher gave me an example of what to use as this:
void swap (int x, int y)
{
int temp;
temp = x
x = y
y = temp;
}
But I have no idea how to use this in the program.
As mentioned by others, it would be good to read up on bubble sorting algorithms.
To answer your question, the swap function would be used every time two elements are out of order (when a larger number is before a smaller number). A pseudo-code example would be:
loop until no element changes through one cycle
for every element in the array minus one
if one element is greater than the element after it
swap()
And that is bubble sort! I hope this helps.
This is where you should use the simplest sorting algorithm in the world: Bubble Sort. Click here for the Wikipedia article describing step by step how to do it.
If you are still having trouble, leave a comment to this response.

Efficient algorithm for difference of array and a known subsequence?

I'm passing an array to a library function which returns an array which is a subsequence of the input array. That is to say the orders of the first and second array are identical but the second array may be lacking any number of elements of the first array. There will be no duplicates in either array!
I want to then build a new array of all the elements which were in the input but are not in the output of the function.
For some reason though it sounds trivial I keep getting it wrong, especially at the ends of the arrays it seems.
Example 1 (typical):
input array a:
[ yyz, ltn, tse, uln, ist, gva, doh, hhn, vlc, ios, app, tlv, lcy ]
input array b:
[ yyz, ltn, tse, uln, ist, gva, doh, hhn, vlc, tlv, lcy ]
output array "diff":
[ ios, app ]
Example 2 (minimal, reveals some bugs when the difference is at the end of the strings):
input array a:
[ usa ]
input array b:
[ ]
output array "diff":
[ usa ]
(I'm going to implement it in JavaScript / jQuery but I'm more interested in a generic algorithm in pseudocode since I'll actually be dealing with arrays of objects. So please I'm looking for algorithms which specifically use array indexing rather than pointers like I would in C/C++)
As the second array b is a subset of the first array a with the same order, you can walk both in parallel, compare the current values, and take the current value of a if it is different from the current value of b:
var a = ['yyz','ltn','tse','uln','ist','gva','doh','hhn','vlc','ios','app','tlv','lcy'],
b = ['yyz','ltn','tse','uln','ist','gva','doh','hhn','vlc','tlv','lcy'],
diff = [];
var i=0, j=0, n=a.length, m=b.length;
while (i<n && j<m) {
if (a[i] !== b[j]) {
diff.push(a[i]);
} else {
j++;
}
i++;
}
while (i<n) {
diff.push(a[i++]);
}
Or if you prefer just one while loop:
// …
while (i<n) {
if (j<m && a[i] === b[j]) {
j++;
} else {
diff.push(a[i]);
}
i++;
}
In java i would probably do something like this if I hade to use Arrays. You will have to loop over all your objects you get back and you will have to compare them to all of thoese you sent in so you will in the worst case have a O(n^2) complexity I belive, but, you can probably improve this by sorting your list you send in and the use pointers to to check each position (but since you didnt want to use pointers I leave this sample out) then you might be able to compare this in O(n).
public void doYourJob(){
Object[] allObjects = new Object[10]; //hold all original values
Object[] recivedArray = yourBlackBox(allObjects); //send in the array an gets the smaller one
Object[] missingArray = new Object[allObjects.length - recivedArray.length];
for(Object inObj : allObjects){
boolean foundObject = false;
for(Object obj : recivedArray){
if(inObj.equals(obj)){
foundObject = true;
break;
}
}
if(!foundObject)
missingArray add inObj //add the missing object. This is not correct java code. =)
}
}
If I were aloud to use something from the Collection interface then this would be much simpler since you can use a "myArray.contains()" method.
With Lists instead
public void doYourJob(){
List<Object> allObjects = new ArrayList<Object>(); //hold all original values
List<Object> recivedArray = yourBlackBox(allObjects); //send in the array an gets the smaller one
List<Object> missingArray = new ArrayList<Object>();
for(Object inObj : allObjects){
if(!recivedArray.contains(inObj))
missingArray.add(inObj);
}
}
Do you have a guaranteed ordering imposed on your arrays? If so, it should be relatively simple to do something like:
# our inputs are array1 and array2, array2 is the one with 0 or more missing elements
ix1 = 0
ix2 = 0
diff = new array
while ix2 < length(array2)
while (ix1 < length(array1)) and (array1[ix1] != array2[ix2])
add array1[ix1] to diff
ix1 = ix1 + 1
ix1 = ix1 + 1
ix2 = ix2 + i
return diff
If you do not have an ordering, you can either impose one (sort both arrays) or you can use a hash table.
hash = new hash
diff = new array
for each element in array1
hash[element] = 1
for each element in array2
hash[element] = hash[element] + 1
for each key in hash
if hash[key] == 1
add hash[key] to diff
Both of these should run in (roughly) O(n), if (and only if) adding an element to an array is O(1) (if you double the size of the result array every time it gets filled, it's at least asymptotically O(1)).

Resources