We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticintminimumDistances(List<Integer>a){// Write your code here/* Idea: + With a distance, traverse through the list between each couple of elements in position A and A + distance to check if they are equal. + Increase the distance each time. + Return the distance right after found the first similar couple. Time complex: + Each inner loop goes through (n - dis) element + Each outer loop runs (n - 1) times. + The total time is the total of every (n - dis) with dis from 1 to (n - 1) + Conclusion: O(n^2) */intn=a.size();for(intdis=1;dis<n;dis++){for(inti=0;i<n-dis;i++){if(a.get(i).equals(a.get(i+dis)))returndis;}}return-1;}
Cookie support is required to access HackerRank
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 →
Here is my solution in Java language: