Question

In: Computer Science

Given two arrays: A[0..m-1] and B[0..n-1]. Find whether B[] is a subset of A[] or not....

Given two arrays: A[0..m-1] and B[0..n-1]. Find whether B[] is a subset of A[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both array are distinct. Design an O(n) time algorithm that solves this problem. Provide pseudocode

Ex:
Input: A[] = {11, 1, 13, 21, 3, 7}, B[] = {11, 3, 7, 1}
Output: B[] is a subset of A[]

Input: A[] = {1, 2, 3, 4, 5, 6}, B[] = {1, 2, 4}
Output: B[] is a subset of A[]

Input: A[] = {10, 5, 2, 23, 19}, B[] = {19, 5, 3}
Output: B[] is not a subset of A[]

Solutions

Expert Solution

SOLUTION IN C LANGUAGE :

EXPLANATION: I have written the code along with explanation here I have used the concept of hashing where the data is stored in a format of key-value pair where key is unique for that in c I have used array, Based on the language you can use that, In java we have directly hash we can use that and do that .

Here for the convenience and checking for the exactness I have written code and checked that So I am directly passing the code so that it will be easier to understand.

NOTE: (int)( sizeof(B) / sizeof(B[0])) Don't worry about this, This is used to calculate the size of the array in c since there is no inbuilt function to determine size of the array.

SOLUTION CODE :

#include<stdio.h>

int main(){
   int A[]={11,1,13,21,3,7},B[]={11,3,7,1},i,subSet=0,sizeB=(int)( sizeof(B) / sizeof(B[0])),max=0;
   // sizeB here is used to calculate the size of b array("(int)( sizeof(B) / sizeof(B[0]))")
   //We are doing using hashing
   //Find the maximum element and then declare the array of that size
   for(i=0;i<(int)( sizeof(A) / sizeof(A[0]));i++){
       if(A[i]>max)
       max=A[i];
   }
   int c[max];
   //Declared array of size of max element
   //store the elements in the array of A to c based on the hashing
   //method of hashing is storing the data based on the value by applying some function to get the unique index here we have used
   //max number modular division n to get the unique index
   for(i=0;i<(int)( sizeof(A) / sizeof(A[0]));i++){
       c[A[i]%max]=A[i];
   }
   //While checking we need to again check that the given data is in the index of c array by again finding the index of B value in c
   //Array if it is there we will continue else we break and print the corresponding message
   for(i=0;i<sizeB;i++){
       if(B[i]==c[B[i]%max]){
           continue;
       }else{
           subSet=1;
           break;
       }
   }
   if(subSet==1){
       printf("B[] is not a subset of A[]");
   }else{
       printf("B[] is a subset of A[]");
   }
   return 0;
  
}

PSEUDO CODE:

Store the values of array A into a hash-based pattern

pass through the array B and check for the value in the hash-based array or object matched with the values of B array

if Matched

continue in checking for other values

else

break

print the message based on the condition of matching

SOLUTION IMAGE:

  

OUTPUT:

INPUT: A[]={11,1,13,21,3,7},B[]={11,3,7,1},

INPUT: A[]={11,1,13,21,3,7},B[]={11,3,7,19}


Related Solutions

In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers into A but not in B. For the example C=A-B= {4,7} Print the values (Use for loops) 2.Create a vector of five integers each in the range from -10 to 100 (Prompt the user for the five integer x). Perform each of the following using loops: a) Find the maximum and minimum value b)Find the number of negatives numbers Print all results
Question 1 a) Determine whether the language {a n b m c n | n >...
Question 1 a) Determine whether the language {a n b m c n | n > 0} is regular or not using pumping Lemma. b) Prove that the language {(ai bn | i, n > 0, i = n or i = 2n} is not regular using the Pumping Lemma.
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
Prove the language of strings over {a, b} of the form (b^m)(a^n) , 0 ≤ m...
Prove the language of strings over {a, b} of the form (b^m)(a^n) , 0 ≤ m < n-2 isn’t regular. (I'm using the ^ notation but your free to make yours bman instead of (b^m)(a^n) ) Use the pumping lemma for regular languages.
1. Two 2-D arrays A [ ] [ ] and B [ ] [ ] are...
1. Two 2-D arrays A [ ] [ ] and B [ ] [ ] are of type boolean and represent two black and white images ( 1’s (black) and 0’s (white) ) having the same size (n x m). Implement a Boolean function to receive these two images(arrays) as an input and return true if A is the negative of B (i.e. each pixel of A is the complement of the corresponding pixel of B) and false otherwise.
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) +...
Let function F(n, m) outputs n if m = 0 and F(n, m − 1) + 1 otherwise. 1. Evaluate F(10, 6). 2. Write a recursion of the running time and solve it . 3. What does F(n, m) compute? Express it in terms of n and m.
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
Find the value of ∑(−1)^n/(3n+1) from n=0 to ∞
Given two 2x2 matrices: M=1     21     1,N=-1       2     1   -1 (a).Verify multiplication of M x N...
Given two 2x2 matrices: M=1     21     1,N=-1       2     1   -1 (a).Verify multiplication of M x N mod 26 = N x M mod 26 = I (identity matrix) (b). Use Hill cipher to encrypt the message EXAMS
Problem 2. Let N denote the non-measurable subset of [0, 1], constructed in class and in...
Problem 2. Let N denote the non-measurable subset of [0, 1], constructed in class and in the book "Real Analysis: Measure Theory, Integration, and Hilbert Spaces" by E. M. Stein, R. Shakarchi. (a) Prove that if E is a measurable subset of N , then m(E) = 0. (b) Assume that G is a subset of R with m∗(G) > 0, prove that there is a subset of G such that it is non-measurable. (c) Prove that if Nc =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT