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
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Minimum Absolute Difference in an Array
  5. Discussions

Minimum Absolute Difference in an Array

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 639 Discussions, By:

recency

Please Login in order to post a comment

  • lambdadelta_
    1 week ago+ 0 comments

    Python 3

    def minimumAbsoluteDifference(arr):
        arr.sort()
        return min(abs(a - b) for a, b in zip(arr, arr[1:]))
    
    0|
    Permalink
  • augustoribeiro_1
    2 weeks ago+ 0 comments
    public static int minimumAbsoluteDifference(List<Integer> arr) {
            
            Collections.sort(arr);
            
            int min = Math.abs(arr.get(0) - arr.get(1));
            
            for(int i = 0; i < arr.size() - 1; i++) {
                
                
                int diff = Math.abs(arr.get(i) - arr.get(i + 1));
                
                if(diff == 0) {
                   return diff;
                }
                
                if(diff < min) {
                    min = diff;
                }
            }
            
            return min;
        }
    
    0|
    Permalink
  • thecodingsoluti2
    2 weeks ago+ 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/03/hackerRank-minimum-absolute-difference-in-an-array-solution.html

    0|
    Permalink
  • sanjeevarayuduk2
    3 weeks ago+ 0 comments

    java 8 solution:

    Collections.sort(arr); List l=new ArrayList<>(); for(int i=0;i

            l.add(Math.abs((arr.get(i))-(arr.get(i+1))));
    
    }
    Collections.sort(l);
    return l.get(0);
    
    0|
    Permalink
  • p11112008
    3 weeks ago+ 0 comments

    My Python 3 Solution

    def minimumAbsoluteDifference(arr):
        # Write your code here
        arr.sort()
        li=[]
        for i in range (len(arr)-1):
            li.append(abs(arr[i]-arr[i+1]))
        return min(li)
    
    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