Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Beyond Sorting: A mathematical O(n) Solution to Arithmetic Progression

Leetcode's daily challenges can be extremely difficult at times, yet surprisingly simple at others, however even the straightforward problems offer food for thought - which is precisely why I am writing this article 🥺 But personally, I st…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

Leetcode's daily challenges can be extremely difficult at times, yet surprisingly simple at others, however even the straightforward problems offer food for thought - which is precisely why I am writing this article 🥺



But personally, I still think the most straightforward and "standard" solution to this problem is to sort the array first and then check whether it is an arithmetic progression or not.



However, if we can still achieve O(n) solution w/o sorting, I believe it would be a better approach.



Arithmetic Progression






Question: 1502. Can Make Arithmetic Progression From Sequence



Description:



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.



AC Submission






Intuition



First of all, let's recall the definition and some basic properties of an arithmetic progression:




  • The sum of an arithmetic sequence with


    nnn

    elements





Sn=12n(2a+(n−1)d)
S_n = \frac{1}{2}n (2a + (n - 1)d)
Sn=21n(2a+(n1)d)





  • The general formula for an arithmetic sequence:





un=a+(n−1)d
u_n = a + (n - 1)d
un=a+(n1)d




Note that:

aa a

is the first term,

dd d

is the common diff.,

nn n

is the index.



So for any valid arithmetic sequence, every element must satisfy this formula.



And this leads to a natural idea:



Why not covert the original array into a Set, and check whether every expected value exists in that set?





Refine our validation logic:




  1. The diff between the maximum and minimum values must be divisible by n, where n = arr.length - 1. This is because we need n identical steps (i.e. common diff.) to connect min and max, for example [1(d1)3(d2)5], there is exactly two diffs.

  2. If max == min, then all elements are the same (e.g. [0,0,0,0]), which is trivially an arithmetic progression, we can directly return true.





Approach:



The core steps of the solution are:




  1. Find the max and min vals in the array.

  2. Check whether (max - min) is divisible by n.

  3. Compute the common diff.

  4. Convert the array into a set (or this might be the first step).

  5. Verify that every expected value:

    min+i×d\text{min} + i \times d min+i×d

    exists in the set. And if any val is missing, return false immediately.





Complexity:



Time Complexity:




  • Finding min and max: O(n)

  • Building the set: O(n)

  • Iterating through expected values: O(n).
    Overall, O(n).



Space Complexity:




  • Since an extra set is created to store the els,
    overall space complexity is O(n)





Code:



Note: in the following code, n = arr.length, and that's why n - 1 is required.




function canMakeArithmeticProgression(arr: number[]): boolean {.

let n: number = arr.length

let max: number = Math.max(...arr)
let min: number = Math.min(...arr)

if ((max - min) % (n - 1) !== 0){
return false
}

if (max === min){
return true
}

let diff: number = Math.floor((max - min) / (n - 1))

let tmp = new Set<number>(arr)

for (let i = 0; i < n; i++){
let expected: number = min + i * diff
if (!tmp.has(expected)){
return false
}
}
return true
// InkkaPlum https://dev.to/slumhee






Thanks for reading!

If u have any questions, feel free to leave comments.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Beyond Sorting: A mathematical O(n) Solution to Arithmetic Progression

Thematisch verwandte Begriffe: Beyond, Sorting, mathematical, Solution · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick