Find the number inside the array

Source: Internet
Author: User

There is an int array, and the difference between each of the two adjacent numbers is either 1 or-1. Now given a number, it is required to find the position of the number in the array.
In fact, the idea is to jump to find, because you know a number, then it is the second number of the maximum difference of 1, the third number is a maximum of 2, and you can use the target number minus this number to exclude some redundant lookup, such as you want the number is 10, the first number is 2, then its first 7 number is not up to 10 Only the 8th number is possible, so you can directly judge the 8th number.
Here are some other people's code to borrow the idea is probably this:

voidPrintfarray (intA[],intN) { for(inti =0; I < n; i++)printf("%d", A[i]);Putchar(' \ n '); }intFindnumberinarray (intArr[],intNintFind_number) {intNext_arrive_index =ABS(find_number-arr[0]); while(Next_arrive_index < N) {if(Arr[next_arrive_index] = = Find_number)returnNext_arrive_index; Next_arrive_index + =ABS(Find_number-arr[next_arrive_index]); }return-1; }

Find the number inside the array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.