관리 메뉴

솜씨좋은장씨

[HackerRank] Minimum Absolute Difference in an Array (Python) 본문

Programming/코딩 1일 1문제

[HackerRank] Minimum Absolute Difference in an Array (Python)

솜씨좋은장씨 2020. 4. 7. 00:21
728x90
반응형

Consider an array of integers, arr = [ arr [0], arr [1], ... , arr [n - 1]]. We define the absolute difference between two elements, a[i] and a[j] (where i != j), to be the absolute value of a[i] - a[j].

Given an array of integers, find and print the minimum absolute difference between any two elements in the array. For example, given the array arr = [ -2, 2, 4 ] we can create 3 pairs of numbers: [-2, 2], [-2, 4] and [2, 4]. The absolute differences for these pairs are |(-2) -2| = 4, |(-2) - 4| = 6 and |2-4| = 2. The minimum absolute difference is 2.

 

Function Description

Complete the minimumAbsoluteDifference function in the editor below. It should return an integer that represents the minimum absolute difference between any pair of elements.

minimumAbsoluteDifference has the following parameter(s):

  • n: an integer that represents the length of arr
  • arr: an array of integers

Input Format

The first line contains a single integer n, the size of arr.
The second line contains n space-separated integers arr [i].

 

Constraints

  • 2 <= n <= 10^5
  • -10^9 <= arr [i] <= 10^9

Output Format

Print the minimum absolute difference between any two elements in the array.

 

Sample Input 0

3
3 -7 0

Sample Output 0

3

Explanation 0

With n = 3 integers in our array, we have three possible pairs: (3, -7), (3, 0), and (-7, 0). The absolute values of the differences between these pairs are as follows:

  • |3- -7| => 10
  • |3 - 0| => 3
  • |-7 -0| => 7

Notice that if we were to switch the order of the numbers in these pairs, the resulting absolute values would still be the same. The smallest of these possible absolute differences is 3.

 

Sample Input 1

10
-59 -36 -13 1 -53 -92 -2 -96 -54 75

Sample Output 1

1

Explanation 1

The smallest absolute difference is |-54- -53| = 1.

 

Sample Input 2

5
1 -3 71 68 17

Sample Output 2

3

Explanation 2

The minimum absolute difference is |71 - 68| = 3.

 

첫번째 시도

#!/bin/python3

import math
import os
import random
import re
import sys
import itertools

# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
    my_permutations = list(set(itertools.permutations(arr, 2)))

    arr_list = [abs(permu[0] - permu[1]) for permu in my_permutations]

    return min(arr_list)

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    result = minimumAbsoluteDifference(arr)

    fptr.write(str(result) + '\n')

    fptr.close()

결과

 

두번째 시도

#!/bin/python3

import math
import os
import random
import re
import sys
import itertools

# Complete the minimumAbsoluteDifference function below.
def minimumAbsoluteDifference(arr):
    sorted_arr = sorted(arr)

    diff = abs(sorted_arr[0] - sorted_arr[1])

    for i in range(1, len(sorted_arr)-1):
        new_diff = abs(sorted_arr[i] - sorted_arr[i+1])
        if diff > new_diff:
            diff = new_diff
    return diff

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    arr = list(map(int, input().rstrip().split()))

    result = minimumAbsoluteDifference(arr)

    fptr.write(str(result) + '\n')

    fptr.close()

결과

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

Comments