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 n
th 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
Similar Kata:
Stats:
Created | Dec 2, 2016 |
Published | Dec 2, 2016 |
Warriors Trained | 236 |
Total Skips | 5 |
Total Code Submissions | 1366 |
Total Times Completed | 78 |
JavaScript Completions | 78 |
Total Stars | 11 |
% of votes with a positive feedback rating | 95% of 37 |
Total "Very Satisfied" Votes | 34 |
Total "Somewhat Satisfied" Votes | 2 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |