6 kyu

Find number in an array # 3

Description:

When no more interesting kata can be resolved, I just choose to create the new kata, to solve their own, to enjoy the process --myjinxin2015 said

Description:

Given an array arr, there are some integers in arr. All the elements are 1(absolute) difference from its neighbors. Like this:

arr = [5,6,7,6,7,6,5,4,3,2,3,4,5]

Given two numbers nth and number, your task is find the index of nth number. For example:

arr = [5,6,7,6,7,6,5,4,3,2,3,4,5]

findIndexOf(arr, 1, 5) === 0  //nth = 1, number = 5

findIndexOf(arr, 2, 5) === 6  //nth = 2, number = 5

findIndexOf(arr, 3, 5) === 12 //nth = 3, number = 5

findIndexOf(arr, 1, 3) === 8 //nth = 1, number = 3

If nth less than 1 or nth more than the count of numbers or number is not found in arr, return null.

arr = [5,6,7,6,7,6,5,4,3,2,3,4,5]

findIndexOf(arr, 0, 5) === null //nth is starting from 1

findIndexOf(arr, -1, 5) === null //nth is starting from 1

findIndexOf(arr, 4, 5) === null //number 5 only appear 3 times in arr

findIndexOf(arr, 1, 10) === null //arr doesn't contains number 10

Note:

  • Arguments arr always be an array that contains at least 5 elements; All elements are positive integers;
  • Arguments nth always be an integer(negative, positive or 0);
  • Arguments number always be an positive integer;
  • Please pay attention to optimizing the code to avoid time out.
  • In the performance test(10000000 elements x 100 testcases), the time consuming of each test case should be within 6ms. This means, Your algorithm should be faster than O(n), your code should run as fast as a rocket ;-)
Algorithms
Puzzles

Stats:

CreatedDec 2, 2016
PublishedDec 2, 2016
Warriors Trained236
Total Skips5
Total Code Submissions1366
Total Times Completed78
JavaScript Completions78
Total Stars11
% of votes with a positive feedback rating95% of 37
Total "Very Satisfied" Votes34
Total "Somewhat Satisfied" Votes2
Total "Not Satisfied" Votes1
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • kazk Avatar
Ad