Find minimum distance between two numbers in an array

0
1163
Find minimum distance between two numbers in an array

In the given unsorted array, which may also contain duplicates, find the minimum distance between 2 different numbers in the given array.

Distance between 2 numbers in an array: absolute difference between the indices + 1

Example

Input array be

Algorithm 1

Time Complexity: O(n^2) , Order is O(n^2) because of the 2 loops.

  1. Use two loops, one loop finds any one of the element and second loop finds the other element in the same way.
  2. Subtract the indices we get the distance between them.
  3. Do this until we get the minimum distance.

Algorithm working Example