Consider there is an array with duplicates and u r given two numbers as input and u have to return the minimum distance between the two in the array with minimum complexity.
Input Array:
int [] A = {1, 5, 3, 7, 2, 8, 3, 4, 5, 9, 9, 3, 1, 3, 2, 9};
minDist = minDistance(A, 9, 3);
System.out.println("Min Distance (9, 3): " + minDist);
minDist = minDistance(A, 3, 9);
System.out.println("Min Distance (3, 9): " + minDist);
minDist = minDistance(A, 4, 7);
System.out.println("Min Distance (4, 7): " + minDist);
minDist = minDistance(A, 9, 9);
System.out.println("Min Distance (9, 9): " + minDist);
minDist = minDistance(A, 3, 3);
System.out.println("Min Distance (3, 3): " + minDist);
minDist = calcMinDistance(A, 5, 8);
System.out.println("Min Distance (5, 8): " + (minDist-1));
minDist = calcMinDistance(A, 5, 9);
System.out.println("Min Distance (5, 9): " + (minDist-1));
Output:
Min Distance (9, 3): 0
Min Distance (3, 9): 0
Min Distance (4, 7): 3
Min Distance (9, 9): 0
Min Distance (3, 3): 1
Min Distance (5, 8): 2
Min Distance (5, 9): 0
No comments:
Post a Comment