Find the nearest coordinate from the origin.
package Interview;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ClosestCoordinateFromOrigin{
public static void main(String[] args) {
//add all characters to single string
String[] coordinates = new String[] {"[-2,6]", "[5,0]", "[2,4]","[6,-4]"};
int lengthCoordinates = coordinates.length;
String addAllChars = " ";
for(int i=0;i<lengthCoordinates;i++) {
System.out.println("The values at coordinates "+i+" are "+coordinates[i]);
addAllChars = addAllChars.concat(coordinates[i]);
}
String data = addAllChars.trim();
System.out.println("All characters added to single string = "+ data);//this returns [-2,6][5,0][2,4][6,-4]
//Create ArrayList of the integers only removing [,],-
int lengtOftheString = data.length();
int[] newArray = new int[lengtOftheString]; //this is useless as shown below
ArrayList<Integer> newArrayList = new ArrayList<>(); //use this one
int count = 0;
for(int n=0;n<lengtOftheString;n++) {
if(!(((Character.toString(data.charAt(n))).equals("["))||((Character.toString(data.charAt(n))).equals("]"))
||((Character.toString(data.charAt(n))).equals("-"))||((Character.toString(data.charAt(n))).equals(",")))){
newArray[count]=Integer.parseInt(Character.toString(data.charAt(n)));
newArrayList.add(newArray[count]);
System.out.println("The numerical values in the array list at index "+count+" = "+newArray[count]);
count++;
}
}
System.out.println("Array = "+Arrays.toString(newArray)); // [2, 6, 5, 0, 2, 4, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
System.out.println("Array List = "+newArrayList); //[2, 6, 5, 0, 2, 4, 6, 4]
//Find the distance of each coordinate
int count1 = 0;
int totalNumberOfdistances = newArrayList.size()/2;
System.out.println("totalNumberOfdistances = "+totalNumberOfdistances);
int lengthFinal = newArrayList.size();
double[] distance = new double[totalNumberOfdistances];
for(int x=1;x<lengthFinal;x=x+2) {
distance[count1] = Math.sqrt(((newArrayList.get(x))*(newArrayList.get(x)))+((newArrayList.get(x-1))*(newArrayList.get(x-1))));
System.out.println("distance "+count1+" = "+distance[count1]);
count1++;
}
System.out.println("Distances are = "+Arrays.toString(distance));
//add HashMap
Map<Double, String> hm = new HashMap<>();
for(int i=0;i<totalNumberOfdistances;i++) {
hm.put(distance[i], coordinates[i]);
}
//To find min distance
Double minDistance = 999999999.00;//Take the max from the above array and use here. For now, I have used all 9s.
for(int i=0;i<totalNumberOfdistances;i++) {
minDistance = Math.min(distance[i], minDistance);
System.out.println("MIN distance "+i+" = "+minDistance);
}
System.out.println("MIN distance out of all = "+minDistance);
//Find the coordinate
System.out.println("MIN distance Coordinate = "+hm.get(minDistance));
}
}
Output:
The values at coordinates 0 are [-2,6]
The values at coordinates 1 are [5,0]
The values at coordinates 2 are [2,4]
The values at coordinates 3 are [6,-4]
All characters added to single string = [-2,6][5,0][2,4][6,-4]
The numerical values in the array list at index 0 = 2
The numerical values in the array list at index 1 = 6
The numerical values in the array list at index 2 = 5
The numerical values in the array list at index 3 = 0
The numerical values in the array list at index 4 = 2
The numerical values in the array list at index 5 = 4
The numerical values in the array list at index 6 = 6
The numerical values in the array list at index 7 = 4
Array = [2, 6, 5, 0, 2, 4, 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Array List = [2, 6, 5, 0, 2, 4, 6, 4]
totalNumberOfdistances = 4
distance 0 = 6.324555320336759
distance 1 = 5.0
distance 2 = 4.47213595499958
distance 3 = 7.211102550927978
Distances are = [6.324555320336759, 5.0, 4.47213595499958, 7.211102550927978]
MIN distance 0 = 6.324555320336759
MIN distance 1 = 5.0
MIN distance 2 = 4.47213595499958
MIN distance 3 = 4.47213595499958
MIN distance out of all = 4.47213595499958
MIN distance Coordinate = [2,4]
No comments:
Post a Comment