Given an array A storing m integer values and an array B storing n integer value
ID: 3935314 • Letter: G
Question
Given an array A storing m integer values and an array B storing n integer values, write in pseudocode an algorithm subarray(A, B, m, n) that returns the value true if A is a sub-array of B and it returns false otherwise. A is a sub-array of B if there is a value 0 lessthanorequalto j lessthanorequalto n - m such that A[0] = B[j], A[1] = B[j + 1], middot middot middot, A[m] = B[j + m - 1], i.e., if the values of A appear in consecutive positions of B. For example, for the arrays A and B shown below the algorithm must return the value true, but for the arrays A' and B, the algorithm must return the value false.Explanation / Answer
bool subarray(a[], b[], m, n)
{
i = 0;
quickSort(a, 0, m-1);
for (i=0; i<n; i++)
{
if (binarySearch(a, 0, m-1, b[i]) == -1)
return false;
}
return true;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.