Created
March 13, 2014 06:13
-
-
Save JorgeOlvera/9522691 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.io.*; | |
public class closestPair { | |
public static void main(String [] args) { | |
Scanner input = new Scanner(System.in); | |
int a = input.nextInt(); | |
int c = a-1; | |
Random num = new Random(); | |
int [] array1 = new int [a]; | |
int [] array2 = new int [c]; | |
for (int b=0; b<array1.length; b++) { | |
array1[b] = num.nextInt(100); | |
} | |
for (int k=0; k<array1.length; k++) { | |
System.out.println(array1[k]); | |
System.out.println("--"); | |
} | |
finder(array1, array2, c); | |
} | |
public static void finder(int [] array1, int [] array2, int c) { | |
int f=0; | |
int g=1; | |
for (int d=0; d<c; d++) { | |
array2[d] = array1[f] - array1[g]; | |
f++; | |
g++; | |
} | |
for (int l=0; l<array2.length; l++) { | |
if (array2[l] <0) { | |
array2[l] = -array2[l]; | |
} | |
} | |
int lowest =array2[0]; | |
int val1 = 0; | |
int val2 =0; | |
for (int h=0; h<array2.length; h++) { | |
if (array2[h] < lowest) { | |
lowest = array2[h]; | |
val1 = h; | |
val2 = h+1; | |
} | |
} | |
System.out.println("Closest values are" + val1 + " and " + val2 + " with a difference of " + lowest); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment