- Minimum Distances


 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 .

Python 3:

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'minimumDistances' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY a as parameter.
#

def minimumDistances(a):
    # Write your code here
    traverse=[]
    diff=[]
    for i in range(0,n):
        item=a[i]
        if(item not in traverse):
            if(a.count(item)>1):
                first_occuarance=a.index(item) 
                second_occuarance=a.index(item,i+1)
                diff.append(second_occuarance-first_occuarance)
            traverse.append(item)
    if(len(diff)>0):
        return min(diff)
    else:
        return -1

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    a = list(map(intinput().rstrip().split()))

    result = minimumDistances(a)

    fptr.write(str(result) + '\n')

    fptr.close()

Java 8:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'minimumDistances' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY a as parameter.
     */

    public static int minimumDistances(List<Integer> a) {
    // Write your code here
        int n=a.size();
        List<Integer> traverse = new ArrayList<>();
        List<Integer> diff = new ArrayList<>();
        for(int i=0;i<n;i++)
        {
            int item=a.get(i);
            if(!traverse.contains(item))
            {
                if(Collections.frequency(a, item)>1)
                {
                    int first_occuar=a.indexOf(item);
                    int second_occuar=a.lastIndexOf(item);
                    diff.add(second_occuar-first_occuar);
                }
                traverse.add(item);
            }
        }
        if(diff.size()>0)
        {
            int min = Collections.min(diff);
            return min;
        }
        else
        {
            return -1;
        }
        

    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$""").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        int result = Result.minimumDistances(a);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}


Compiler Message
Success
Input (stdin)
  • 6
  • 7 1 3 4 1 7
Expected Output
  • 3


Post a Comment

Previous Post Next Post