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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Implementation
  4. Minimum Distances
  5. Discussions

Minimum Distances

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1229 Discussions, By:

recency

Please Login in order to post a comment

  • alban_tyrex
    4 hours ago+ 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/H8aBskFE2XI

    int minimumDistances(vector<int> a) {
        map<int, int>mp;
        int result = 1000;
        for(int i = 0; i < a.size(); i++){
            if(mp[a[i]]){
                result = min(result, i + 1 - mp[a[i]]);
            }
            mp[a[i]] = i+1;
        }
        return (result == 1000) ? -1:result;
    }
    
    0|
    Permalink
  • alban_tyrex
    4 hours ago+ 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/H8aBskFE2XI

    int minimumDistances(vector<int> a) {
        map<int, int>mp;
        int result = 1000;
        for(int i = 0; i < a.size(); i++){
            if(mp[a[i]]){
                result = min(result, i + 1 - mp[a[i]]);
            }
            mp[a[i]] = i+1;
        }
        return (result == 1000) ? -1:result;
    }
    
    0|
    Permalink
  • poojachaprana271
    6 hours ago+ 0 comments

    ALL CASES RUNNING import java.io.; import java.util.; import java.math.; import java.util.regex.; import java.text.*; public class solution{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i

        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < n ; i++){
            for (int j = i+1; j < n; j++) {
                if (a[i] == a[j]){
                    int temp = j - i;
                    if(temp < min){
                        min = temp;
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE)
        min = -1;System.out.println(min);
    }
    

    }

    0|
    Permalink
  • poojachaprana271
    6 hours ago+ 0 comments

    ALL CASES RUNNING

    import java.io.; import java.util.; import java.math.; import java.util.regex.; import java.text.*; public class solution{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int a[] = new int[n]; for(int i = 0; i

        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < n ; i++){
            for (int j = i+1; j < n; j++) {
                if (a[i] == a[j]){
                    int temp = j - i;
                    if(temp < min){
                        min = temp;
                    }
                }
            }
        }
        if(min == Integer.MAX_VALUE)
        min = -1;System.out.println(min);
    }
    

    }

    0|
    Permalink
  • nanateinumonde
    2 days ago+ 0 comments
    def minimumDistances(a):
        # Write your code here
        pairs = set()
        for i in a:
            if a.count(i) == 2:
                pairs.add(i)
                
        if not pairs:
            return -1
        distances = []      
        for val in pairs:
            indexes = []
            for i, v in enumerate(a):
                if v == val:
                    indexes.append(i)
            distances.append(indexes[1] - indexes[0])
        return min(distances)
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy