We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Minimum Distances

Minimum Distances

Problem
Submissions
Leaderboard
Discussions
Editorial

The distance between two array values is the number of indices between them. Given , find the minimum distance between any pair of equal elements in the array. If no such value exists, return .

Example

There are two matching pairs of values: and . The indices of the 's are and , so their distance is . The indices of the 's are and , so their distance is . The minimum distance is .

Function Description

Complete the minimumDistances function in the editor below.

minimumDistances has the following parameter(s):

  • int a[n]: an array of integers

Returns

  • int: the minimum distance found or if there are no matching elements

Input Format

The first line contains an integer , the size of array .
The second line contains space-separated integers .

Constraints

Output Format

Print a single integer denoting the minimum in . If no such value exists, print .

Sample Input

STDIN           Function
-----           --------
6               arr[] size n = 6
7 1 3 4 1 7     arr = [7, 1, 3, 4, 1, 7]

Sample Output

3

Explanation
There are two pairs to consider:

  • and are both , so .
  • and are both , so .

The answer is .

Author

Shafaet

Difficulty

Easy

Max Score

20

Submitted By

123306

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy