You are viewing a single comment's thread. Return to all comments →
Hello friends,
In this video tutorial, In this video tutorial, I have explained How To Solve Minimum Distances hackerrank problem in O(n) time.
Please watch this video till the end and let me know, If you have any doubts or a better approach to solve this problem.
If interested to know more about the generic algorithm in details-
click here for the video explanation of generic algorithm with complexity analysis.
or you can click on the image too to follow youtube tutorial.
Here is the working solution:-
source code :
static int minimumDistances(int[] a) { HashMap<Integer, Integer> hmap = new HashMap<>(); int minDistance = Integer.MAX_VALUE, prevIndex = 0, currentIndex = 0; for (int i = 0; i < a.length; i++) { if (hmap.containsKey(a[i])) { currentIndex = i; prevIndex = hmap.get(a[i]); minDistance = Math.min((currentIndex - prevIndex), minDistance); } hmap.put(a[i], i); } return (minDistance == Integer.MAX_VALUE ? -1 : minDistance); }
Would really appreciate your feedback like, dislike , comment etc. on my video.
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Distances
You are viewing a single comment's thread. Return to all comments →
Hello friends,
In this video tutorial, In this video tutorial, I have explained How To Solve Minimum Distances hackerrank problem in O(n) time.
Please watch this video till the end and let me know, If you have any doubts or a better approach to solve this problem.
If interested to know more about the generic algorithm in details-
click here for the video explanation of generic algorithm with complexity analysis.
or you can click on the image too to follow youtube tutorial.
Here is the working solution:-
source code :
Would really appreciate your feedback like, dislike , comment etc. on my video.
Do not forget to upvote, if you find it useful.