Question

In: Computer Science

Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm...

Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm to find the largest value of ‘n’ for which f(n) is less than a target. Show all the work (including the values for the left index, right index, middle index and the function value at the middle index) for each iteration as well as write down the initial values of the left index and right index and the corresponding function values.

f(n) = 3n^3 + 5n + 1, Target = 100

Solutions

Expert Solution

First we have to find the upper limit to apply binary search. the largest value of n where it becomes greater than =100. this can done in O(logn) time complexity as shown in the below function:

We do repeated doubling of n until f(n)>=100;

int findLargestNumber()

{

    if (f(0) >= 100)

        return 0;

    int i = 1;

    while (f(i) < 100)

        i = i*2;

    return binarySearch(i/2, i);

}

Next we do binary Search

.................................

int binarySearch(int low, int high)

{

    if (high >= low)

    {

        int mid = low + (high - low)/2;

         cout<<"Left Index: "<< low<<endl;

     cout<<"Right Index: "<< high<<endl;

     cout<<"Mid Index: "<<mid<<endl;

     cout<<"............."<<endl;

        if (f(mid) < 100 && (mid == low || f(mid+1) >= 100))

            return mid;

        if (f(mid) < 100)

            return binarySearch((mid + 1), high);

        else

            return binarySearch(low, (mid -1));

    }

    return -1;

}

The Complete C++ Code is:

..................................

#include<bits/stdc++.h>

using namespace std;

int binarySearch(int low, int high);

int f(int x) { return (3*x*x*x + 5*x +1); }

int findLargestNumber()

{

    if (f(0) >= 100)

        return 0;

    int i = 1;

    while (f(i) < 100)

        i = i*2;

    return binarySearch(i/2, i);

   

}

int binarySearch(int low, int high)

{

    if (high >= low)

    {

        int mid = low + (high - low)/2;

         cout<<"Left Index: "<< low<<endl;

     cout<<"Right Index: "<< high<<endl;

     cout<<"Mid Index: "<<mid<<endl;

     cout<<"............."<<endl;

        if (f(mid) < 100 && (mid == low || f(mid+1) >= 100))

            return mid;

        if (f(mid) < 100)

            return binarySearch((mid + 1), high);

        else

            return binarySearch(low, (mid -1));

    }

    return -1;

}

int main()

{

    cout<<"The value n where f() becomes" <<

        ">=100 "<< findLargestNumber();

    return 0;

}


Related Solutions

Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
9. Given the function: f(x)=3x2+5x-17 a. find the interval where the function is increasing and where...
9. Given the function: f(x)=3x2+5x-17 a. find the interval where the function is increasing and where it is decreasing. b. Clearly state the critical number(s) of the function. c. Find the relative extrema of the function( using 1st derivative test). answer should be in (x,y) 10. Given the function: x3 - x2 + 52x - 17 a. determine the intervals where the graph of the given function is concave upward and where it is concave downward. b. Find any inflection...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Discrete Math Problems: From the below algorithm (linear search) construct the function f(n) which computes the...
Discrete Math Problems: From the below algorithm (linear search) construct the function f(n) which computes the number of steps the algorithm executes for a list of n integers and compute O(f) using the definition                     ALGORITHM 2 The Linear Search Algorithm.                 procedure linear search(x: integer, a1, a2,…, an: distinct integers)                 i := 1                 while (i ≤ n and x ≠ ai)                i := i + 1                if i ≤ n then location := i               ...
Find the runtime of this function, where n is an integer. int function(int n) { int...
Find the runtime of this function, where n is an integer. int function(int n) { int a = 0; for (int i = n; i > 0; i--) { for (int j = 0; j < i; j++) { a = a + i + j; } } return a; } Find the runtime of this function, where m and n are two integers. int f(int m, int n) { if (m==1 || n==1) { return 1; } return f(m,...
1. Use the derivative function, f'(x)f′(x), to determine where the function f(x)=−2x^2+14x−8 is increasing. 2.Use the...
1. Use the derivative function, f'(x)f′(x), to determine where the function f(x)=−2x^2+14x−8 is increasing. 2.Use the derivative function f'(x)f′(x) to determine where the function f(x)=2x^3−27x^2+108x+13 is increasing.   3.Use the derivative function f'(x)f′(x) to determine where the function f(x)=2x^3−27x^2+108x−12 is decreasing. 4.Find each value of the function f(x)=−x^3+12x+9 where the line tangent to the graph is horizontal. x=
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p < r q = (p + r)/2 if V = A[q] return q else if V > A[q] return BinarySearch(A, q+1, r, V)    else return BinarySearch(A, p, q-1) else if p = r,    if V = A[p]    return p else return -1    return -1 end function Using this pseudocode, write a function for BinarySearch and also complete the program, by...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT