Question

In: Computer Science

a) Design in pseudocode an algorithm that gets as input an integer k, and a list...

a) Design in pseudocode an algorithm that gets as input an integer k, and a list of k integers N1, .., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list that sum to the value SUM. For example, if your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm should output "either" the two values 2 and 18, "or" the two values 3 and 17. If your algorithm cannot find any pair of values that sum to the value SUM, then it should print out the message "sorry, there is no such pair of values".

b)   What is the number of comparisons performed by your algorithm in the Best case? In the Worst case?

c)   Express these numbers in the theta (θ) notation.

Solutions

Expert Solution

b) let k=5 ki={10, 2, 3, 15, 10}

Worst case : let SUM=25

then number of comparision is 4+3+2+1;

for k it will be k+(k-1)+(k-2)+......1 = k*(k+1)/2

so worst case is k*(k+1)/2

BestCASE: let sum=12

number of comparisions is 1 (because for first comparision sum =12)

so best case is 1.

#include <bits/stdc++.h>
using namespace std;
void find_pair(int k,int arr[],int SUM)
{
  for (int i = 0; i < k; i++) 
  {
    int tempsum = 0;
    for (int j = i; j < k; j++)
    {
      // Calculate required sum
      tempsum += arr[j];
 
      // Check if sum is equal to
      // required sum
      if (tempsum == SUM)
        {
            cout<<"pairs which sums to given SUM are:"<<arr[i]<<" "<<arr[j]<<endl;
            return ;
        }
    }
  }
  cout<< "sorry, there is no such pair of values";
  return;
}
int main()
{
  int arr[] = {10, 2, 2, 15, 10};
  int k=5;
  int SUM= 25;
  find_pair(k,arr,SUM);
 
}

Note : the code is for referenced and it is written in C++(Since language is not mentioned in problems)


Related Solutions

Design and implement an algorithm that gets as input a list of k integer values N1,...
Design and implement an algorithm that gets as input a list of k integer values N1, N2, …., Nk, as well as a special value SUM. Your algorithm must locate a pair of values in the list N that sum to the value SUM. For example, If your list of values is 3, 8, 13, 2, 17, 18, 10, and the value of SUM is 20, then your algorithm would output either of the two values (2, 18) or (3,...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3...
Using the idea of the Bubble Sort algorithm, design an algorithm in pseudocode that gets 3 numbers a, b, c, from the user, and prints the 3 numbers in ascending order
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Pseudocode and algorithm of merging k sorted list with running time O(Nlogk).
Design an algorithm ( pseudocode or C ) to calculate the frequency of each of the...
Design an algorithm ( pseudocode or C ) to calculate the frequency of each of the vowels (lowercase and uppercase) in a text. The program will ask the user for a text, which will be entered by keyboard and will save, in a table (in%), the frequency of each of the vowels within the text, taking into account all the characters of the text. The text will end with the character '' # ''. Includes the emty sequence.
Write a program that first gets a list of integers from input. The input begins with...
Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain fewer than 20 integers. That list is followed by two more integers representing lower and upper bounds of a range. Your program should output all integers from the list that are within that range (inclusive of the bounds). For coding simplicity, follow each output integer by a space,...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user...
Write an algorithm (flowchart OR Pseudocode) for the following problem Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For example if the user enters B a b e c o o d i u g o a l $ Then we have the output below: #A=2 #E=1 #I=1 #O=3 #U=2
Write an algorithm (flowchart OR Pseudocode) for the following problems: Take ten different numbers as input...
Write an algorithm (flowchart OR Pseudocode) for the following problems: Take ten different numbers as input and display the sum of there squares (SS). Example: let the inputs are: 3, 0, -1, 0, 9, -5, 6, 0, 2, 1 then 157 is displayed, because: SS=32+02+(-1)2+02+92+(-5)2+62+02+22+12=157 Take input character from the user unless he enters '$'. Thereafter display how may vowels were entered by the user. Also display the number of each vowel ('a', 'e', 'i', 'o' and 'u') separately. For...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2...
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2 P is the largest power of two less than or equal to n. Create a 1-dimensional table with P +1 columns. The leftmost entry is the Pth column and the rightmost entry is the 0th column. Repeat until P < 0 If 2 P ≤ n then put 1 into column P set n := n − 2 P Else put 0 into column...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT