Question

In: Advanced Math

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ......

(a) Design an algorithm that reveals some secret integer number from the set {1, 2, ... , n} by guessing random numbers within the given range until the secret number has been guessed. Numbers should be replaced after being guessed such that ​it is possible to guess 2 and then 2 again​, assuming 2 is in the given range. The algorithm should return both the secret number as well as the number of guesses taken.

(b) If possible, calculate the tightest Big-​O​ approximation for the average runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(c) If possible, calculate the tightest Big-​O​ approximation for the worst case runtime complexity of the algorithm from part (a). If it is not possible, explain why not. Note: assume that choosing a random number takes ​O(1)​ time.

(d) In a single run, is it possible that of the algorithm from part (a) finds the secret number in fewer guesses than a standard binary search algorithm? If so, please provide a concrete example of one such situation.

Solutions

Expert Solution

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE TTHERE TO HELP YOU..ALL THE BEST

ANSWER:

code and explanation:

1)

assuming set containing secret intezer is all intezers from 1 to N

implementation of algorithm in c++:

#include <iostream>
#include <algorithm>
#include <random>
#include <time.h>
using namespace std;
#define N 20 //let n be 20
int main()
{
    srand(time(0));
    int secret_number = 2;                 //let secret number be 2
    int count = 0;                         //number of guesses taken
    int current_guess = rand() % N + 1;    //choosing random between 1 to n
    count++;                               //increement count
    while (current_guess != secret_number) //repeat till we encounter secret number
    {
        current_guess = rand() % N + 1; //again choosing random number between 1 to n
        count++;                        //increement count
    }
    cout << "Secret number is " << secret_number << " and number of gueses taken is " << count;
}


2nd and 3rd Q are more or less same

Q asks if we can approximate runtime complexity of this algorithm, but the answer is we can't
atleast for this algorithm because we are initializing rand() using srand(time(0)) which always
generates different random intezer each time program runs.

screenshot is attatched which states different number of guesses each time we run program

But if we assumes random number generator generates purely random intezer from 1 to n
based on probabilities,

    then runtime complexity both average as well as worst case is O(n).


4)

    Yes it is possible that this algorithm takes fewer guesses than binary search algorithm
    example is shown in screenshot. Where n is 20 and secret number is 2.

    here complexity of binary search algorithm is log(n), so log(20) ie more than 4 but
in screenshot below it can be seen that in some cases number of guesses is 3 also which
is less than 4.

I HOPE YOU UNDERSTAND..

PLS RATE THUMBS UP..ITS HELPS ME ALOT..

THANK YOU...!!


Related Solutions

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,...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer...
*Please give the answers in pseudo code 1) Design an algorithm that will receive two integer items from a terminal operator, and display to the screen their sum, difference, product and quotient. Note that the quotient calculation (first integer divided by second integer) is only to be performed if the second integer does not equal zero. 2) Design an algorithm that will read two numbers and an integer code from the screen. The value of the integer code should be...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3,...
Consider the set of integer numbers from 0 to 9, that is {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Bob wishes to use these numbers to create a 7-digit password to secure his new laptop. Note that each number can appear in any position (for example, 0 can be the first number in the password). (a) Find the number of 7-digit passwords that are possible. (b) Find the number of 7-digit passwords with distinct digits. (c) Find...
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...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based...
a. Design a recursive algorithm for computing 3n for any nonnegative integer n that is based on the formula 3n = 3n−1 + 3n−1 + 3n−1 b. Set up a recurrence relation for the number of additions made by the algorithm and solve it. c. Draw a tree of recursive calls for this algorithm and count the number of calls made by the algorithm. d. Is it a good algorithm for solving this problem?
Let A be an integer array of length n. Design a divide and conquer algorithm (description...
Let A be an integer array of length n. Design a divide and conquer algorithm (description and pseudo code) to find the index of an element of the minimum value in the array. If there are several such elements, your algorithm must return the index of the rightmost element. For instance, if A = {0,2,4,5,2,0,3,10}, then the algorithm should return 5, which is the index of the second 0.
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,...
Design an algorithm to prompt user to enter a number. Determine the number is odd or...
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
A Mystery Algorithm Input: An integer n ≥ 1 Output: ?? Find P such that 2^p...
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 P End if Subtract 1...
Consider the number set S={3,5,7,9,11,..,91,93}(i.e., {x|3≤1+2⋅c≤93} where c is an integer). How many items from the...
Consider the number set S={3,5,7,9,11,..,91,93}(i.e., {x|3≤1+2⋅c≤93} where c is an integer). How many items from the set do I have to take such that some subset of the chosen numbers must sum to 96? Be sure to explain your answer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT