wu :: forums
« wu :: forums - How to find subset in two a array »

Welcome, Guest. Please Login or Register.
May 15th, 2024, 2:02am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, towr, Grimbal, ThudnBlunder, SMQ, Icarus, Eigenray)
   How to find subset in two a array
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: How to find subset in two a array  (Read 1155 times)
nks
Junior Member
**





   
Email

Gender: male
Posts: 145
How to find subset in two a array  
« on: Aug 26th, 2011, 10:48am »
Quote Quote Modify Modify

There are two arrays given N and M(larger). How to find wether N is subset of M, i.e All elements of N  is available in M,
 
No hashing is to be used.
 
Is there any other way  possible without using any extra array in O(N).
« Last Edit: Aug 27th, 2011, 9:10am by nks » IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: How to find subset in two a array  
« Reply #1 on: Aug 26th, 2011, 11:41am »
Quote Quote Modify Modify

If M is smaller than N, as you stated, then N can't be a (proper) subset of M.
Assuming that was just a mistake, you could find whether the smaller set is a subset of the larger one in linear time if they're both sorted.
 
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
nks
Junior Member
**





   
Email

Gender: male
Posts: 145
Re: How to find subset in two a array  
« Reply #2 on: Aug 27th, 2011, 9:16am »
Quote Quote Modify Modify

Thanks TOWR for correction, Have corrected it, M is larger,
 
Sorting is known as O( N log N)  excluding counting sort.
 looking in O(N) with space O(1).
 
« Last Edit: Aug 27th, 2011, 9:17am by nks » IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: How to find subset in two a array  
« Reply #3 on: Aug 27th, 2011, 2:32pm »
Quote Quote Modify Modify

Then I don't think it's possible.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
Hippo
Uberpuzzler
*****





   


Gender: male
Posts: 919
Re: How to find subset in two a array  
« Reply #4 on: Sep 12th, 2011, 12:05pm »
Quote Quote Modify Modify

Define encodding of array elements by positive integers (injection), map positive integer i to i-th prime (of precomputed subset of primes) (injection as well). Make products \PiN and \PiM of compound injections of arrray elements of N and of M.
Find their greatest common divisor. If it equals to \PiN you are done.
 
This solves problem when elements of N must be in M with their full multicity.
 
Yes, I am joking with infinit precision arithmetic with O(1) per operation.
« Last Edit: Sep 12th, 2011, 12:08pm by Hippo » IP Logged
TenaliRaman
Uberpuzzler
*****



I am no special. I am only passionately curious.

   


Gender: male
Posts: 1001
Re: How to find subset in two a array  
« Reply #5 on: Sep 12th, 2011, 12:16pm »
Quote Quote Modify Modify

Sort the smaller array. Go over the larger array and check into the smaller array using binary search. This should be M log N. If N <<< M, the sorting cost and the logN search cost should be sufficiently small enough.
 
-- AI
IP Logged

Self discovery comes when a man measures himself against an obstacle - Antoine de Saint Exupery
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board