Question

3. Let A and B be two sequences of positive integers that are sorted in increasing order. Write an algorithm for determining

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

Algorithm:

start

Declare sequence A elements in Ascending order

Declare sequence B elements in Ascending order

Input x

for i <- 0, size of A do

for j <- 0, size of B do

if(x==(A[i]+B[j])) do

print x is sum of elements in the sequence of elements A and B

end program

end if

end for

end for

print x is not sum of elements in the sequence of elements A and B

Time complexity Analysis:

start

Declare sequence A elements in Ascending order // Since it runs for 1 time so its Complexity is O(1)

Declare sequence B elements in Ascending order // Since it runs for 1 time so its Complexity is O(1)

Input x // Since it runs for 1 time so its Complexity is O(1)

for i <- 0, size of A do   // Since it runs for n time so its Complexity is O(n)

for j <- 0, size of B do // Since it runs for n time so its Complexity is O(n) and also runs every i loop so overall complexity is O(n2)

if(x==(A[i]+B[j])) do // runs every j loop so its complexity is O(n2)

print x is sum of elements in the sequence of elements A and B   // Since it runs for 0 or 1 time so its Complexity is O(1)

end program

end if

end for

end for

print x is not sum of elements in the sequence of elements A and B

end program

Therefore the overall complexity is O(n2)

Program to verify algorithm:

#include <iostream>

using namespace std;

int main() {

int A[]={1,15,20,23,30,44}; // sequence of elemnts A declaration

int B[]={3,17,26,33,42}; // sequence of elemnts B declaration

int x,sizeA,sizeB; // variable declaration

cout << "Enter an integer: ";

cin>>x; // Accept x value

sizeA=sizeof(A)/sizeof(int); // Find size of sequence A

sizeB=sizeof(B)/sizeof(int); // Find size of sequence B

for(int i=0;i<sizeA;i++) // Loop runs for sequence A

for(int j=0;j<sizeB;j++) // Loop runs for sequence B

if(x==(A[i]+B[j])) // check x is sum of sequence of elements A and B

{

cout<<"x is sum of elements in the sequence of elements A and B"<<endl;

return 0; // Stop program

}

cout<<"x is not sum of elements in the sequence of elements A and B"<<endl;

}

Output:

Q > clang++-7 -pthread -std=c++17 -o main main.cpp ./main Enter an integer: 18 x is sum of elements in the sequence of elemen

Add a comment
Know the answer?
Add Answer to:
3. Let A and B be two sequences of positive integers that are sorted in increasing...
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
  • Given two arrays A and B of n integers both of which are sorted in ascending...

    Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity

  • 1. Design an algorithm to find all the non-common elements in two sorted lists of numbers....

    1. Design an algorithm to find all the non-common elements in two sorted lists of numbers. What is the maximum number of comparisons your algorithm makes if the lengths of the two given lists are m and n, ?respectively 2. Estimate how many times faster it will be to find ged(98765, 56789) by Euclid's algorithm compared with the algorithm based on checking consecutive integers from min{m, n} down to gcd(m, n). 3. For each of the following functions, indicate how...

  • For any two positive integers a, b, define k(a,b) to be the largest k such that a* | b but ak+1b. Given two positive in...

    For any two positive integers a, b, define k(a,b) to be the largest k such that a* | b but ak+1b. Given two positive integers x, y, show that (a) k(a, gcd(x, y)) = min{k(a, x), k(a, y)} for any positive integer a (b) k(a, lcm(z, y)) = max{k(a,a),k(a, y)} for any positive integer a. Hint: Think of the prime factorization of the numbers For any two positive integers a, b, define k(a,b) to be the largest k such that...

  • Suppose that we have two unsorted lists of integers A and B. The lists are the...

    Suppose that we have two unsorted lists of integers A and B. The lists are the same size, n. a) Write an algorithm that outputs how many integers occur in both lists. An integer occurs at most once in each given list. For example: if A = [1,2,5,7,13,19] and B = [2,9,13,14,19,22], then we can see that elements {2, 13, 19} occur in both lists, so the output will be 3. b) If the lists were sorted, say how we...

  • Given an array A[1..n] of positive integers and given a number x, find if any two...

    Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • 3. Chapter 7. Write an algorithm that accepts an array of positive integers and two more...

    3. Chapter 7. Write an algorithm that accepts an array of positive integers and two more positive integers. The algorithm should return the array with all the numbers that are between the two numbers. For example, if the given array is 12 16 23 19 33 14 3 15 and the integers 13 and 20 are entered, then the algorithm will return 16 19 14 15 How many comparisons does your algorithm perform? Explain your answer. Zero points if there...

  • 6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative....

    6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <i<n and T[i] = i, provided such an index exists. Your algorithm should take a time in O(lg n) in the worst case. Answers must be proven (or at least well justified)

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

  • I have a bunch of positive integers that need to be sorted by the number of...

    I have a bunch of positive integers that need to be sorted by the number of zeros in it. For instance, 0 has 1 zero in it. 3001 has 2. Write a function called "sort_by_zeros" that takes in a vector of ints and returns a vector of ints sorted by their zero count. When two ints have the same count, preserve the original order. Note, you can only write one named function definition (i.e. sort_by_zeros) for this problem and you...

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
ADVERTISEMENT