requirement :
use c++ to do the function "int ArrayChallenge(int arr[], int arrLength)" :
1. numbers stored in arr[].
2. the function first determine the largest element in the array,
3. then you can move to left or right in the arr[] with arr[currentindex], you can looping around the arr.
4. continuously according to integer in the current index.
5. If you can reach the same spot within the array, then the function should return the least amount of jumps it took.
7. For example: if the input is [2, 3, 5, 6, 1] you'll start at the spot where 6 is and if you jump 6 spaces to the right while looping around the array you end up at the last element where the 1 is. Then from here you can jump 1 space to the left and you're back where you started, so your program should output 2.
8.If it's impossible to end up back at the largest element in the array your program should output -1.
9. The largest element in the array will never equal the number of elements in the array.
10. The largest element is unique.
Examples
Input: {1,2,3,4,2}
Output: 3
Input: {1,7,1,1,1,1}
Output: 2
code :
TBD.
该博客要求使用C++实现函数“int ArrayChallenge(int arr[], int arrLength)”。函数需先确定数组中最大元素,依据当前索引元素左右移动,可循环数组。若能回到最大元素位置,返回最少跳跃次数;无法回到则返回 -1。还给出了示例输入输出。
463

被折叠的 条评论
为什么被折叠?



