29 lines
1.0 KiB
Python
29 lines
1.0 KiB
Python
# 1502. Can Make Arithmetic Progression From Sequence
|
|
#
|
|
# A sequence of numbers is called an arithmetic progression if the difference
|
|
# between any two consecutive elements is the same.
|
|
#
|
|
# Given an array of numbers arr, return true if the array can be rearranged to
|
|
# form an arithmetic progression. Otherwise, return false.
|
|
#
|
|
|
|
# usar o metodo sort() para ordenar o vector
|
|
# se o vector tiver tamanho 2, retorna True (só temos a diferença entre dois
|
|
# numeros)
|
|
# calculamos a diferença entre os dois primeiros numeros, e procuramos nos
|
|
# restantes pares de numeros consecutivos se a diferença é igual, se não for
|
|
# retornamos False
|
|
# se chegarmos ao fim do vector e se mantiver a diferença, então o vector
|
|
# tem uma sequencia de progressão aritmetica
|
|
|
|
|
|
def canMakeArithmeticProgression(array: list[int]) -> bool:
|
|
array.sort()
|
|
if len(array) == 2:
|
|
return True
|
|
diff: int = array[1] - array[0]
|
|
for index in range(1, len(array)):
|
|
if diff != (array[index] - array[index - 1]):
|
|
return False
|
|
return True
|