answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
viktelen [127]
2 years ago
3

Design an algorithm to find all the common elements in two sorted lists of numbers. For example, for the lists 2, 5, 5, 5 and 2,

2, 3, 5, 5, 7, the output should be 2, 5, 5. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, respectively?
Computers and Technology
1 answer:
zimovet [89]2 years ago
4 0

Answer:

Algorithm:

1. Declare and initial two array a, b of length m, n.

2.sort both the array.

3.Create and initial two variables i=0,j=0.

4. while(i<m and j<n)

   4.1 if a[i] equal b[j], increase both i and j by 1.

   4.2 if a[i] > b[j] then increase j

   4.3 else increase i

5. End the program.

Here maximum number of comparison will O<=(m+n) if there will be no common element in both the array.In the while loop, i will maximum goes to m And j will maximum to n.In each loop i will increase or j will increase or both

increase.

// here is code in c++ to implemented the above algorithm

#include <bits/stdc++.h>

using namespace std;

int main() {

   

   // declare and initialize two array

 int ar[] = { 2, 5, 5, 5 };

 int br[] = { 2, 2, 3, 5, 5, 7 };

 int i = 0;

 int j = 0;

 int m=sizeof(ar)/sizeof(ar[0]);

 int n=sizeof(br)/sizeof(br[0]);

 cout<<"Common elements are:";

 // use while loop, to check the

 // array elements

 while (i < m && j < n)

 {

    if (ar[i] == br[j])

       {

           // display an array element

           cout<<ar[i]<<" ";

           // Increment variables          

           i++;

           j++;

   

       }

    else if (ar[i] > br[j])

           j++;

   else

       i++;

         } // end of while

return 0;

}

Output:

Common elements are:2 5 5

You might be interested in
XYZ Corp.’s facilities in Nashua, New Hampshire, are two office buildings 400 feet apart, each with its own LAN. To connect the
damaskus [11]

Answer:

a: Twisted pair won't span a 400-foot distance.

Explanation:

The maximum distance that twisted pair cables can support without attenuation is 100 meters which is approximately 328 feet and the two office buildings are 400 feet apart, so it is useless to install a twisted pair cable for such a large distance. A much better option would be to install optic fiber. Though it is a bit expensive but it is the best option in this case.

Hence option (a) is the correct reason for installing an optic fiber cable rather than a twisted pair cable.

6 0
2 years ago
Convert to octal. Convert to hexadecimal. Then convert both of your answers todecimal, and verify that they are the same.(a) 111
melomori [17]

The given decimal values are converted to octal and hexadecimal.

Explanation:

a. The converted octal value of 111010110001.0112 decimal number is  1473055755061.00557000643334272616

The converted hexadecimal value of 111010110001.0112 decimal number is  19D8B7DA31.02DE00D1B71758E21965

b. The converted octal value of 10110011101.112 decimal number is  113246503335.07126010142233513615

The converted hexadecimal value of  10110011101.112 decimal number is  

25A9A86DD.1CAC083126E978D4FDF4

a. while converting back from octal to decimal the value is 111010110001.01119999999999999989

b. while converting back from octal to decimal the value is

10110011101.11199999999999999973

Hence both the values changes while we convert from octal to decimal.

3 0
2 years ago
Consider the following relationship involving two entities, students and classes:A student can take many classes. A class can be
ad-work [718]

Answer:

3

Explanation:

ER model can be used and is based on three basic concepts: Entities, Attributes & Relationships.

An entity can be place, person, object, event or a concept, which stores data in the database.

Relationship is nothing but an association among two or more entities. A weak entity is a type of entity which doesn't have its key attribute.

3 0
2 years ago
3. Choosing a pre-formatted presentation (that already has a design in the slides) is called choosing a ?
Charra [1.4K]

Answer:

3. Choosing a pre-formatted presentation (that already has a design in the slides) is called choosing a ?

Explanation:

3. Choosing a pre-formatted presentation (that already has a design in the slides) is called choosing a ?

7 0
2 years ago
Which of the following is NOT a method in which a macro can be run?
liubo4ka [24]
C....................
7 0
2 years ago
Other questions:
  • A(n) ____ is an electronic device, operating under the control of instructions stored in its own memory, that can accept data, p
    14·1 answer
  • In three to five sentences, describe whether or not files should be deleted from your computer.
    13·2 answers
  • Marissa works at a company that makes perfume. She noticed many samples of the perfume were not passing inspection. She conducte
    6·2 answers
  • If a street has a central lane bordered by solid yellow and broken yellow lines, you ________________.
    6·1 answer
  • A data center that is fully automated to the extend that they can run and manage themselves while being monitored remotely are s
    11·1 answer
  • A website updated daily could be considered _____. a. authoritative b. objective c. accurate d. timely
    8·1 answer
  • Implement a class MyInt() that behaves almost the same as the class int, except when trying to add an object of type MyInt. Then
    11·1 answer
  • 1. Show that the three security services-confidentiality, integrity, and availabilty- are sufficient to deal with the threats of
    10·1 answer
  • covers a wide variety of applications such as web and computer-based training (CBT), and social networks. a. E-learning b. Model
    10·1 answer
  • You need to design a data storage scheme for Hayseed Heaven library data system. There are several hundred thousand large data r
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!