코딩테스트 준비/해커랭크

해커랭크 Minimum Absolute Difference in an Array python

watervin 2022. 7. 3. 23:04

The absolute difference is the positive difference between two values  and , is written  or  and they are equal. If  and , . Given an array of integers, find the minimum absolute difference between any two elements in the array.

Example. 

There are  pairs of numbers:  and . The absolute differences for these pairs are ,  and . The minimum absolute difference is .

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):

  • int arr[n]: an array of integers

Returns

  • int: the minimum absolute difference found

Input Format

The first line contains a single integer , the size of .
The second line contains  space-separated integers, .

Constraints

  •  
  •  

Sample Input 0

3
3 -7 0

Sample Output 0

3

Explanation 0

The first line of input is the number of array elements. The array,  There are three pairs to test: , , and . The absolute differences are:

  •  
  •  
  •  

Remember that the order of values in the subtraction does not influence the result. The smallest of these absolute differences is .

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 .

Sample Input 2

5
1 -3 71 68 17

Sample Output 2

3

Explanation 2

 

The minimum absolute difference is .

 

처음 시도

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'minimumAbsoluteDifference' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
import math
def minimumAbsoluteDifference(arr,n):
    # Write your code here

    result = 10**20
    
    for i in  range(n-1):
        for j in range(i+1,n):
            if abs(arr[i] - arr[j]) < result:
                result = abs(arr[i] - arr[j])
    
    return result

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

    n = int(input().strip())

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

    result = minimumAbsoluteDifference(arr,n)

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

    fptr.close()

타임 아웃 떴다.

 

새로 고친 결과

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'minimumAbsoluteDifference' function below.
#
# The function is expected to return an INTEGER.
# The function accepts INTEGER_ARRAY arr as parameter.
#
import math
def minimumAbsoluteDifference(arr,n):
    # Write your code here

    arr = sorted(arr)
    result = 10**20
    
    for i in  range(n-1):
        if arr[i+1] - arr[i] < result:
            result = arr[i+1] - arr[i]
    
    return result

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

    n = int(input().strip())

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

    result = minimumAbsoluteDifference(arr,n)

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

    fptr.close()