Minimum Distances

Minimum Distances

We define the distance between two array values as the number of indices between the two values. Given a, find the minimum distance between any pair of equal elements in the array. If no such value exists, print -1.

For example, if a = [3,2,1,2,3], there are two matching pairs of values: 3 and 2. The indices of the 3‘s are i = 0 and j = 4, so their distance is d[i,j] = |j - i| = 4. The indices of the 2‘s are i = 1 and j = 3, so their distance is d|i,j| = |j - i| = 2.

Function Description

Complete the minimumDistances function in the editor below. It should return the minimum distance between any two matching elements.

minimumDistances has the following parameter(s):

  • a: an array of integers

Input Format

The first line contains an integer n, the size of array a.

The second line contains n space-separated integers a[i].

Constraints

  • 1 <= n <= 103
  • 0 <= a[i] <= 105

Output Format

Print a single integer denoting the minimum d[i,j] in a. If no such value exists, print -1.

Sample Input

1
2
6
7 1 3 4 1 7

Sample Output

1
3

Explanation

Here, we have two options:

  • a[1] and a[4] and are both 1, so d[1,4] = |1 - 4| = 3.
  • a[0] and a[5] and are both 7, so d[0,5] = |0 - 5| = 5.

The answer is min(3,5) = 3.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Complete the minimumDistances function below.
function minimumDistances(a) {
let result = 0;

for (let i of Array.from({ length: a.length }, (value, index) => index)) {
for (let j of Array.from(
{ length: a.length },
(value, index) => index + i + 1
)) {
((a[i] === a[j] && !result) ||
(a[i] === a[j] && Math.abs(i - j) < result)) &&
(result = Math.abs(i - j));
}
}

return !result ? -1 : result;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×