1answer.
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]
3 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]3 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
Draw a flowchart or write pseudocode to represent a program's logic that allows the user to enter a value. The program multiplie
PIT_PIT [208]

Ill choose flowchart. Look picture for the answer

ask me if you have any doubts about my answer.

5 0
3 years ago
What kind of animation is used in the powerpuff girls show??
konstantin123 [22]
Calarts? Cartoon I don’t really know but that’s all the information that I have
8 0
3 years ago
The programming projects of Chapter 4 discussed a Card class that represents a standard playing card. Create a class called Deck
RideAnS [48]

Answer:

Check the explanation

Explanation:

====================Card.java====================

package card_game;

public class Card {

public final static int ACE = 1;

public final static int TWO = 2;

public final static int THREE = 3;

public final static int FOUR = 4;

public final static int FIVE = 5;

public final static int SIX = 6;

public final static int SEVEN = 7;

public final static int EIGHT = 8;

public final static int NINE = 9;

public final static int TEN = 10;

public final static int JACK = 11;

public final static int QUEEN = 12;

public final static int KING = 13;

public final static int CLUBS = 1;

public final static int DIAMONDS = 2;

public final static int HEARTS = 3;

public final static int SPADES = 4;

private final static int NUM_FACES = 13;

private final static int NUM_SUITS = 4;

private int face, suit;

private String faceName, suitName;

// -----------------------------------------------------------------

// Creates a random card.

// -----------------------------------------------------------------

public Card() {

face = (int) (Math.random() * NUM_FACES) + 1;

setFaceName();

suit = (int) (Math.random() * NUM_SUITS) + 1;

setSuitName();

}

// -----------------------------------------------------------------

// Creates a card of the specified suit and face value.

// -----------------------------------------------------------------

public Card(int faceValue, int suitValue) {

face = faceValue;

setFaceName();

suit = suitValue;

setSuitName();

}

// -----------------------------------------------------------------

// Sets the string representation of the face using its stored

// numeric value.

// -----------------------------------------------------------------

private void setFaceName() {

switch (face) {

case ACE:

faceName = "Ace";

break;

case TWO:

faceName = "Two";

break;

case THREE:

faceName = "Three";

break;

case FOUR:

faceName = "Four";

break;

case FIVE:

faceName = "Five";

break;

case SIX:

faceName = "Six";

break;

case SEVEN:

faceName = "Seven";

break;

case EIGHT:

faceName = "Eight";

break;

case NINE:

faceName = "Nine";

break;

case TEN:

faceName = "Ten";

break;

case JACK:

faceName = "Jack";

break;

case QUEEN:

faceName = "Queen";

break;

case KING:

faceName = "King";

break;

}

}

// -----------------------------------------------------------------

// Sets the string representation of the suit using its stored

// numeric value.

// -----------------------------------------------------------------

private void setSuitName() {

switch (suit) {

case CLUBS:

suitName = "Clubs";

break;

case DIAMONDS:

suitName = "Diamonds";

break;

case HEARTS:

suitName = "Hearts";

break;

case SPADES:

suitName = "Spades";

break;

}

}

// -----------------------------------------------------------------

// Determines if this card is higher than the parameter. The

// second parameter determines if aces should be considered high

// (beats a King) or low (lowest of all faces). Uses the suit

// if both cards have the same face.

// -----------------------------------------------------------------

public boolean isHigherThan(Card card2, boolean aceHigh) {

boolean result = false;

if (face == card2.getFace()) {

if (suit > card2.getSuit())

result = true;

}

else {

if (aceHigh && face == ACE)

result = true;

else if (face > card2.getFace())

result = true;

}

}

=========================Sample output=======================

3 0
4 years ago
A red bullet in a microflow indicates: a. that the microflow has been completed b. that the microflow contains errors c. that on
Viefleur [7K]

Answer:

d. an end point of the microflow mendix

Explanation:

In Computer programming, Microflows can be defined as a visual representation of textual program codes based on the Business Process Model and Notation (BPMN), it enables users to express the logic of application.

It is capable of performing various actions such as creating and updating objects, showing pages and making choices.

Microflows cannot be used in offline apps as it basically runs in the runtime server.

A red bullet in a microflow indicates an end point of the microflow mendix.

8 0
3 years ago
When using correct ergonomic technique be sure to _____.
jeka57 [31]

Answer: The answer is C

4 0
4 years ago
Read 2 more answers
Other questions:
  • Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the use
    14·1 answer
  • Which of these consoles have 64 bit architecture
    13·1 answer
  • Was LDAP version2 an internet standard in 1998? is it now?
    5·1 answer
  • A location in space is called?
    6·2 answers
  • Here we have a program which is calling the subtract function to calculate the difference between two numbers. The value from th
    7·1 answer
  • Match each command group under Picture Tools to the appropriate tasks.
    6·1 answer
  • Select the correct answer from each drop-down menu.
    6·1 answer
  • What is a function in Microsoft Excel?
    5·1 answer
  • Please help!!!!!!! 50 POINTS!!!!
    10·1 answer
  • Matching parentheses. An math expression may have a number of parentheses like (, ), [, ], { and }. Each openning parenthesis (,
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!