Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active June 25, 2021 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/3475815e6a2e1d3e8a462a01f0901068 to your computer and use it in GitHub Desktop.
Save McLarenCollege/3475815e6a2e1d3e8a462a01f0901068 to your computer and use it in GitHub Desktop.
symmetricalWorm

TIME ALLOWED : 50 minutes

A symmetrical worm of size N can be defined as a sequence of numbers having a central digit and every nth left and right digits to the central digit are equal to each other.

Your task is to find the number of symmetrical worms of the given size in an array.

Here are some examples of symmetrical worms

Size 3: [2,1,2]

Size 5: [8,-5,1,-5,8]

Size 7: [-2,-1, 5, 0, 5,-1,-2]

So all symmetrical worms will have sizes like 3,5,7 .. etc which will be odd numbers greater than 1.

Here is a diagram with an example for illustration.

img

Your function should accept 2 parameters, an array, and a worm size and should return the number of symmetrical worms of that size present in the array.

Foreg.

function numberOfSymmetricalWorms(arr,size){
// write your code here
}
console.log(numberOfSymmetricalWorms([5,1,5,1,5],3))// should return 3. Worms are [5,1,5],[1,5,1],[5,1,5]
console.log(numberOfSymmetricalWorms([5,1,7,1,5],5))// should return 1. Worms are [5,1,7,1,5]
console.log(numberOfSymmetricalWorms([5,1,-7,1,5],3))// should return 1. Worms are [1,-7,1]
console.log(numberOfSymmetricalWorms([5,1,-7,3,5],3))// should return 0. No worms of given size
console.log(numberOfSymmetricalWorms([5,-3,2,1,5,1,2,-3,5,9,5,-3,2,1],9))// should return 2. Worms are [5,-3,2,1,5,1,2,-3,5],[1,2,-3,5,9,5,-3,2,1]
console.log(numberOfSymmetricalWorms([5,-3,2,1,5,1,2,-3,6,9,5,-3,2,1],7))// should return 1. Worms are [-3,2,1,5,1,2,-3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment