Hello Programmers, In this post, you will know how to solve the HackerRank Minimum Distances Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Minimum Distances Solution
Task
The distance between two array values is the number of indices between them. Given a, find the minimum distance between any pair of equal elements in the array. If no such value exists, return -1.
Example
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. The minimum distance is 2.
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 –1 if there are no matching elements
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
- 1 <= 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
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:
- a[1] and a[4] are both 1, so d[1, 4] = |1 – 4} = 3.
- a[0] and a[5] are both 7, so d[0, 5] = |0 – 5} = 5.
The answer is min(3, 5) = 3.
HackerRank Minimum Distances Solution
Minimum Distances Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n,i,j,min=100000; scanf("%d",&n); int *a = malloc(sizeof(int) * n); for(int i = 0; i < n; i++){ scanf("%d",&a[i]); } for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++) if(a[i]==a[j] && j-i<min) min=j-i; } if(min==100000) min=-1; printf("%d",min); return 0; }
Minimum Distances Solution in Cpp
#include <bits/stdc++.h> #define all(x) (x).begin(), (x).end() #define li long long #define itn int using namespace std; inline int nxt(){ int n; scanf("%d", &n); return n; } int gcd(int x, int y){ while (y){ x %= y; swap(x, y); } return x; } const int mod = 1000000007; int main(){ int n = nxt(); vector<int> a(n); for (int i = 0; i < n; i++){ a[i] = nxt(); } int ans = -1; for (int i = 0; i < n; i++){ for (int j = 0; j < i; j++){ if (a[i] == a[j]){ if (i - j < ans || ans == -1) ans = i - j; } } } cout << ans << "\n"; return 0; }
Minimum Distances Solution in Java
import java.io.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); StringTokenizer st = new StringTokenizer(in.readLine()); int[] A = new int[n]; for(int i=0;i<n;i++) A[i] = Integer.parseInt(st.nextToken()); int ans = Integer.MAX_VALUE; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(A[i] == A[j] && j - i < ans) ans = j - i; System.out.println(ans == Integer.MAX_VALUE? -1 : ans); } }
Minimum Distances Solution in Python
#!/bin/python import sys n = int(raw_input().strip()) A = map(int,raw_input().strip().split(' ')) r=n+1 for i in range(n): for j in range(i+1,n): if A[j]==A[i]: r=min(j-i,r) if r<=n: print r else: print -1
Minimum Distances Solution using JavaScript
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()); A = readLine().split(' '); A = A.map(Number); d = 1000000 for(i=0;i<A.length; i++){ for(j= i+1; j<A.length; j++){ if(A[i] == A[j]) d = Math.min(d, j-i) } } console.log(d != 1000000 ? d : -1) }
Minimum Distances Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in); var n = sc.nextInt(); var A = new Array[Int](n); for(i <- 0 to n-1) { A(i) = sc.nextInt(); } var min = n for (i <- 0 until n; j <- 0 until n) { if ((i != j) && (A(i) == A(j))) { if (Math.abs(i - j) < min) {min = Math.abs(i - j)} } } if (min == n) { min = -1} print(min) } }
Minimum Distances Solution in Pascal
var n,s,i,j,min:longint; a:array[1..1000] of longint; begin readln(n); min:=1001; for i:=1 to n do read(a[i]); for i:=1 to n-1 do for j:=i+1 to n do if a[i]=a[j] then begin s:=j-i; if s<min then min:=s; end; if min<>1001 then writeln(min) else writeln(-1); end.
Disclaimer: This problem (Minimum Distances) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.