Friday, 8 January 2016

Kata Day 43: Greatest Position Distance Between Matching Array Values


Problem Description: The goal of this Kata is to return the greatest distance of index positions between matching number values in an array or 0 if there are no matching values.

Example: In an array with the values [0, 2, 1, 2, 4, 1] the greatest index distance is between the matching '1' 
values at index 2 and 5. Executing greatestDistance against this array would return 3. (i.e. 5 - 2)

Here's the previous example in test form:

Test.assertEquals(greatestDistance([0, 2, 1, 2, 4, 1]), 3);
This is based on a Kata I had completed only to realize I has misread the instructions. I enjoyed solving the 
problem I thought it was asking me to complete so I thought I'd add a new Kata for others to enjoy. There are no tricks in this one, good luck!

Level:6kyu

Link To Kata:  Greatest Position Distance Between Matching Array Values

In this kata we have to find two equal numbers in a given array where the distance is maximum. We start solving this kata by initializing a variable dist to 0. We loop through the given array and initialize another index variable which is 1 more than the current index. We find the element which is equal to the element in the looping index and store the difference of two indexes in the dist variable. 

In the end we return the value of dist variable as the solution to this Kata.

Please go through the code for a better understanding. 

Here is the link to code: Kata Day 43


Please mention your suggestions or your doubts in the comment below.

Happy coding. :)

No comments:

Post a Comment