0% found this document useful (0 votes)
14 views

Tset 2827, Java

The document contains code to find the minimum distance between equal elements in an integer array. It uses nested for loops to compare each element to every other element, calculates the absolute difference between their indices if they are equal, and tracks the minimum such distance.

Uploaded by

Abhilash Mohanty
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Tset 2827, Java

The document contains code to find the minimum distance between equal elements in an integer array. It uses nested for loops to compare each element to every other element, calculates the absolute difference between their indices if they are equal, and tracks the minimum such distance.

Uploaded by

Abhilash Mohanty
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

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(int arr[]) {


// Write your code here
int d=0;
int min=0;
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{
if(arr[i]==arr[j])
{
int k=i-j;
d=Math.abs(k);
min=Math.min(d,min);
}
}
}
return min;
}

public class Solution {


public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
Result rs=new Result();
System.out.println(rs.minimumDistances(arr));

}
}

You might also like