Skip to content

Commit 83ac1a7

Browse files
committed
Added hard/array_jumping
1 parent c679857 commit 83ac1a7

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

hard/array_jumping.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Using the JavaScript language, have the function arrayJumping(arr) take the
3+
* array of numbers stored in arr and first determine the largest element in the
4+
* array, and then determine whether or not you can reach that same element
5+
* within the array by moving left or right continuously according to whatever
6+
* integer is in the current spot. If you can reach the same spot within the
7+
* array, then your program should output the least amount of jumps it took. For
8+
* example: if the input is [2, 3, 5, 6, 1] you'll start at the spot where 6 is
9+
* and if you jump 6 spaces to the right while looping around the array you end
10+
* up at the last element where the 1 is. Then from here you jump 1 space to the
11+
* left and you're back where you started, so your program should output 2. If
12+
* it's impossible to end up back at the largest element in the array your
13+
* program should output -1. The largest element in the array will never equal
14+
* the number of elements in the array. The largest element will be unique.
15+
*
16+
* https://www.coderbyte.com/results/bhanson:Array%20Jumping:JavaScript
17+
*
18+
* @param {array} arr
19+
* @return {number}
20+
*/
21+
function arrayJumping(arr) {
22+
const max = Math.max(...arr);
23+
const startIndex = arr.indexOf(max);
24+
25+
// Iterative "breadth-first search"
26+
// `queue` stores an array of arrays. The inner arrays represent paths of
27+
// indexes. They all start with the index of the largest number and each
28+
// jump is appended and the entire new structure is stored in a new queue.
29+
30+
let queue = [[startIndex]];
31+
32+
while (queue.length !== 0) {
33+
const nextQueue = [];
34+
35+
for (let i = 0; i < queue.length; i++) {
36+
const indexList = queue[i];
37+
const lastIndex = indexList[indexList.length - 1];
38+
39+
const left =
40+
(((lastIndex - arr[lastIndex]) % arr.length) + arr.length) %
41+
arr.length;
42+
const right = (lastIndex + arr[lastIndex]) % arr.length;
43+
44+
if (left === startIndex || right === startIndex) {
45+
// Success!
46+
return indexList.length;
47+
}
48+
49+
if (!indexList.includes(left)) {
50+
const leftArr = indexList.slice();
51+
leftArr.push(left);
52+
nextQueue.push(leftArr);
53+
}
54+
55+
if (!indexList.includes(right)) {
56+
const rightArr = indexList.slice();
57+
rightArr.push(right);
58+
nextQueue.push(rightArr);
59+
}
60+
}
61+
queue = nextQueue;
62+
}
63+
64+
// No path possible
65+
return -1;
66+
}
67+
68+
module.exports = arrayJumping;

hard/array_jumping.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const arrayJumping = require('./array_jumping');
2+
3+
describe('arrayJumping()', () => {
4+
test('passes Coderbyte.com tests', () => {
5+
expect(arrayJumping([1, 2, 3, 4, 2])).toBe(3);
6+
7+
expect(arrayJumping([1, 7, 1, 1, 1, 1])).toBe(2);
8+
9+
expect(arrayJumping([2, 3, 5, 6, 1])).toBe(2);
10+
11+
expect(arrayJumping([1, 2, 1])).toBe(2);
12+
13+
expect(arrayJumping([1, 2, 1, 2, 1, 9])).toBe(3);
14+
15+
expect(arrayJumping([1, 2, 1, 2, 1, 12])).toBe(1);
16+
17+
expect(arrayJumping([0, 5, 2])).toBe(2);
18+
19+
expect(arrayJumping([1, 6, 5, 7, 9, 2])).toBe(-1);
20+
21+
expect(arrayJumping([1, 3, 5, 7, 9, 2])).toBe(2);
22+
23+
expect(arrayJumping([1, 5, 1, 6, 2, 3, 3])).toBe(2);
24+
});
25+
});

0 commit comments

Comments
 (0)