title: finds two numbers in an array with the smallest distance to each other In Java
body: <p>One question from a website: Implement a Java function that finds two numbers in an array with the smallest distance to each other. The function should return the index of the first number.</p>
  <p>Here is my thoughts. But I don't know how to implement it.</p>
  <ol>
  <li>Step1. Get all the result of 2 element arrays from a given array.
  For example {4,5,7,11} have 4*3 = 12 two element arrays.</li>
  <li>Step2. Calculate each the two array distance. And find out the minimum
  distance.</li>
  </ol>
  <p>I do viewed at least 10 google results. Seems don't have the good answer. IN real life, people would first sort it then try some way to figure it out. But let's assume the array as it is. We do not sort it.
  The following code is based on one expert's answer. But unfortunately it seems not working...</p>
  <pre><code>import java.math.*;
  import java.util.*;
  import java.util.Arrays;
  import java.util.Collections;
  
  public class TestallBasics1 {
      public static void main(String[] args) {
          int[] arr = new int[]{2,1,6,8,10,89,54};
          Arrays.sort(arr);
          System.out.println(Arrays.toString(arr));
  
          int difference=0;
          int n = arr.length;
          for(int i=0; i&lt;n-1; i++){
              if(difference &gt; (arr[i+1]-arr[i]))
              {difference = arr[i+1]-arr[i];}
  
          System.out.println(difference);
  
          }
      }
  }
  </code></pre>