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
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...
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.
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 =...
You are given two integer arrays a and b of the same length. Let's define the...
You are given two integer arrays a and b of the same length. Let's define the difference between a and b as the sum of absolute differences of corresponding elements: difference = |a[0] - b[0]| + |a[1] - b[1]| + ... + |a[a.length - 1] - b[b.length - 1]| You can replace one element of a with any other element of a. Your task is to return the minimum possible difference between a and b that can be achieved by...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is...
Given two functions, M(x, y) and N(x, y), suppose that (∂N/∂x − ∂M/∂y)/(M − N) is a function of x + y. That is, let f(t) be a function such that f(x + y) = (∂N/∂x − ∂M/∂y)/(M − N) Assume that you can solve the differential equation M dx + N dy = 0 by multiplying by an integrating factor μ that makes it exact and that it can also be written as a function of x + y,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT