Question
#include using namespace std; /** * This program takes an array as input and tries to find the index where the treasure is hidden. *
#include
/** * This program takes an array as input and tries to find the index where the treasure is hidden. * The array will have valid locations from 0 to n-1 (n is an input parameter). * * Your treasure map tells you to always look at location (max+min)/2, where max = the largest index * the treasure could be at, and min = the smallest index the treasure could be at (using integer * division, so round down). * * The integer value at that location will give you a hint where the treasure must be stored: * If the value is -1, then the treasure cannot be stored at locations (max+min)/2 or bigger. * If the value is +1, then the treasure cannot be stored at locations (max+min)/2 or smaller. * If the value is 0, then you found the treasure! Return the current index you're looking at. * If you run out of array to search without finding the treasure, return -1. * * There is fake gold and fake hints buried in this array! * If you search anywhere other than where the map tells you, you will get false information! * * You will probably want to make a helper function. You may not use any loops. */
int treasure(int *arr, unsigned int n){ }
//do NOT change the main function int main(int argc, char** argv){ unsigned int n; cin >> n; int *arr = new int[n]; for (unsigned int i = 0; i < n; i++) cin >> arr[i]; cout << treasure(arr, n) << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started