Created
September 1, 2016 03:23
-
-
Save MariaSzubski/7b1dfab706267d87d54cef482654265b to your computer and use it in GitHub Desktop.
John Watson performs an operation called a 'right circular rotation' on an array of integers. Find the element at index 'm' in the rotated array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Solution for HackerRank > Algorithms > Warmup > Circular Array Rotation | |
| https://www.hackerrank.com/challenges/circular-array-rotation | |
| */ | |
| function sherlock() { | |
| // Number of rotations | |
| var k = 51; | |
| // Array of Integers | |
| var arr = [39356, 87674, 16667, 54260, 43958, 70429, 53682, 6169, 87496, 66190, 90286, 4912, 44792, 65142, 36183, 43856, 77633, 38902, 1407, 88185, 80399, 72940, 97555, 23941, 96271, 49288, 27021, 32032, 75662, 69161, 33581, 15017, 56835, 66599, 69277, 17144, 37027, 39310, 23312, 24523, 5499, 13597, 45786, 66642, 95090, 98320, 26849, 72722, 37221, 28255, 60906]; | |
| // Index values of rotated array | |
| var m = [ 47, 10, 12, 13, 6, 29, 22, 17, 7, 3, 30, 45, 1, 21, 50, 17, 25, 42, 5, 6, 47, 2, 24, 1, 6, 14, 24, 43, 7, 2, 35, 3, 13, 22, 16, 19, 0, 12, 10, 32, 41, 14, 1, 42, 35, 0, 9, 34, 17, 14, 15, 38, 17, 13, 40, 48, 27, 38, 41, 8, 14, 25, 11, 27, 47, 2, 20, 22, 39, 4, 28, 29, 43, 29, 21, 1, 4, 4, 10, 46, 43, 50, 33, 34, 12, 47, 32, 13, 8, 47, 22, 23, 21, 33, 24, 43, 35, 19, 39, 24 ]; | |
| // Test for more than one complete rotation | |
| (k >= arr.length) ? (k = k % arr.length) : (k = k); | |
| // Rotate array | |
| var arr_rotated = arr.splice(-k, k).concat(arr); | |
| // Print the index of each query in the rotated array | |
| for (q of m){ | |
| console.log(arr_rotated[q]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment