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. Sorting
  4. Closest Numbers
  5. Discussions

Closest Numbers

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

    You are viewing a single comment's thread. Return to all comments →

  • akhilgup
    3 years ago+ 2 comments

    Python3 solution All test cases passed

    def closestNumbers(arr):
        sorted_arr = sorted(arr)
        diff = []
        for i in range(len(sorted_arr) - 1):
            diff.append(sorted_arr[i+1] - sorted_arr[i])
        value = min(diff)
        result = []
        for i in range(len(sorted_arr) - 1):
            if (sorted_arr[i+1] - sorted_arr[i]) == value:
                result.append(sorted_arr[i])
                result.append(sorted_arr[i+1])
        return result
    
    7|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature