Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.32 KB

_1502. Can Make Arithmetic Progression From Sequence.md

File metadata and controls

56 lines (42 loc) · 1.32 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 04, 2024

Last updated : July 01, 2024


Related Topics : Array, Sorting

Acceptance Rate : 69.52 %


Solutions

C

int compareHelper(const void* one, const void* two) {
    return *((int *) one) - *((int *) two);
}

bool canMakeArithmeticProgression(int* arr, int arrSize) {
    qsort(arr, arrSize, sizeof(int), compareHelper);

    for (int i = 0; i < arrSize - 2; i++) {
        if (!(arr[i] - arr[i + 1] == arr[i + 1] - arr[i + 2])) {
            return false;
        }
    }
    return true;
}

Python

class Solution:
    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
        arr.sort()
        for i in range(len(arr) - 2) :
            if not arr[i] - arr[i + 1] == arr[i + 1] - arr[i + 2] :
                return False

        return True