Question

/* * Find the first element in A that is * not also in B. */...

/* * Find the first element in A that is * not also in B. */

01: int firstNotIn (int* A, int* B, int nA, int nB)

02: {

03: int pos = 0;

04: bool found = false;

05: while (pos < nA && !found)

06: {

07: if (find(B, B+nB, A[pos]) != B+nB)

08: found = true;

09: else

10: ++pos;

11: }

12: if (!found)

13: {

14: pos = -1;

15: }

16: return pos;

17: }

According to the copy-and-paste technique, how would you annotate line 5 of this function by the time you had determined the complexity of the entire loop?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Currently, given function is returing the position of first element in A that is also in B.

But now, question is asking to fund the position of first element in A that is not in B.

Please find my modification in given function:


/*
* Find the first element in A that is
* not also in B.
*/
int firstNotIn (int A, int B, int nA, int nB)
{
int pos = 0;
bool found = false;

// this will loop until we find a element in A that is not in B or till end of A, So it runs O(N) times
// N is the size of A
while (pos < nA && !found)
{
if (seqSearch(B, nB, A[pos]) == -1) //this takes O(M) time, M is the size of B
found = true;
else
++pos;
}
if (!found)
{
pos = -1;
}
return pos;
}

Time complexity: (M*N)


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
/* * Find the first element in A that is * not also in B. */...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* Find the first element in A that is not also in B 01: int firstNotln...

    /* Find the first element in A that is not also in B 01: int firstNotln (int* A, int* B, int nA, int nB) 02: 03: int pos= 0; 04: while (pos < NA && find(B, B+nB, A[pos]) != B+nB) 05: 06: ++pos; 07: 08: if (pos > nA) 09: pos -1; 10: 11: 12: 13: return pos 14: According to the copy-and-paste technique, how would you annotate line 4 of this function by the time you had determined the...

  • How do i find the image height and width in SOF marker Segment( the Xthumbnail and...

    How do i find the image height and width in SOF marker Segment( the Xthumbnail and Ythumbnail are all 0)? Which is the resolution of the image ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 c8 00 c8 00 00 ff db 00 43 00 02 02 02 02 02 01 02 02 02 02 02 02 02 03 03 06 04 03 03 03 03 07 05 05 04 06 08 07...

  • Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file...

    Need help writing a program that meets pseudocode and criteria . Txt File below input.txt file data 05 11/30/16 03 12/07/16 05 12/07/16 05 12/08/16 01 12/10/16 07 12/11/16 07 12/14/16 06 12/15/16 02 12/21/16 05 12/21/16 06 12/22/16 07 12/22/16 08 12/23/16 07 12/23/16 07 12/23/16 07 12/23/16 08 12/24/16 08 12/24/16 07 12/24/16 03 12/26/16 05 12/26/16 07 12/28/16 04 12/29/16 07 01/01/17 06 01/03/17 07 01/03/17 08 01/05/17 05 01/10/17 04 01/17/17 08 01/17/17 07 01/18/17 07...

  • Consider the following code: *How many elements in the array A are * also in the...

    Consider the following code: *How many elements in the array A are * also in the array B? Assume B is sorted. */ 01: int overlap (int* A, int* B, int N) 02:{ 03: int count = 0; 04: for (int i = 0; i < N; ++i) 05: 06: int x A [i 07: 08 int pos lower_bound (B, B+N, x) - B; if (pos N && B [pos] 09: 10: 11: 12: == x ) { +count; 13:...

  • In a digital communication system, probability density function of the two level signal received in the...

    In a digital communication system, probability density function of the two level signal received in the receiver is: PR(v) = PS(v)*PN(v) = [0.4δ(v+1) + 0.6δ(v-4)]*η(v). And , η(v) is the noise that added to the message sign as the additive Gaussian noise with a value of zero and an effective value of 3. (* symbol means convolution process, in the solution of this problem you can use the below Q function table.) ,   η(v) = A) Plot the probability density...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • Nonlinear. Here we want you to verify this property by computing the output of S, for the followi...

    nonlinear. Here we want you to verify this property by computing the output of S, for the following two pairs of inputs. The S-box S is given as follows: 0-6. (10 points) One important property which makes DES secure is that the S-Boxes are S-box S S0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 14 04 13 01 02 15 11 08 03 10 06 12 05 09 00 07 1...

  • From the following labeled listing of individuals and the following excerpt from the random digits table B, select a simple random sample of size three. Circle the selected individuals. 3. 10. St...

    From the following labeled listing of individuals and the following excerpt from the random digits table B, select a simple random sample of size three. Circle the selected individuals. 3. 10. Stone 07. Smith LISTING: 01. Evenson 04. Jones 02. Buchanan 05. Hazan 06. Woods 11. Vigil 12. Morrison 08. Sanchez 09. Lin 03. Garcia EXCERPT: 21065 44902 37793 11384 20021 07498 06912 12009 45287 71753 From the following labeled listing of individuals and the following excerpt from the random...

  • Python help it is a grade 12 course def find astrological sign(month, date): (int, int)-str Given...

    Python help it is a grade 12 course def find astrological sign(month, date): (int, int)-str Given two int values represent ing a month and a date, return a 3-character string that gives us what star sign a person born in that month and on that date belongs to, Use the SIGNS string (already defined for you at the top of this file) to figure this out. NOTE FROM BOB: A lot of string slicing to do here. It looks like...

  • The data set below are the unemployment rates from April 2016 through March 2018. The mean...

    The data set below are the unemployment rates from April 2016 through March 2018. The mean unemployment rate is 4.5, the median unemployment rate is 4.5, and the standard deviation of 0.33. Describe the data set using these statistics. Does the data appear to be consistent on a monthly basis? Year/Month Unemployment rate 2016-04 5.0 2016-05 2016-06 2016-07 2016-08 2016-09 2016-10 2016-11 2016-12 2017-01 2017-02 2017-03 2017-04 MacBook Pro 2017-05 4.3 2017-06 4 2017-07 4.3 2017-08 4.4 2017-09 4.2 2017-10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT