↓Skip to main content
  1. LeetCode/

LeetCode 3618: Split Array by Prime Indices

·2 mins· ·
LeetCode Medium Math Number-Theory Sieve Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-19

  • Submissions: Accepted (first try)

Approach
#

Elements at prime indices go to A and the rest to B; return |sum(A) - sum(B)|.

To test every index 0..n-1, a sieve of Eratosthenes marks them all at once in O(n log log n). I trial-divided each index up to √i, which is O(n √n) and still passes, but more slowly (767ms).

0 and 1 are not prime.

My Solution
#

from math import isqrt

class Solution:
    def splitArray(self, nums: List[int]) -> int:
        n = len(nums)
        is_prime = [False, False] + [True] * max(0, n - 2)
        for i in range(2, isqrt(n) + 1):
            if is_prime[i]:
                for j in range(i * i, n, i):
                    is_prime[j] = False
        diff = 0
        for i, x in enumerate(nums):
            diff += x if is_prime[i] else -x
        return abs(diff)

With n = 1, only the first n entries of is_prime are read, so the padded [False, False] does no harm.

Complexity
#

Time O(n log log n), space O(n).

Mistakes to Avoid
#

  • Primality is about the index, not the element’s value.
  • 0 and 1 are not prime.

Interview Explanation
#

A sieve of Eratosthenes marks which indices are prime in one go; then one scan adds elements at prime indices, subtracts the rest, and takes the absolute value. Time O(n log log n), faster than trial division per index.

Related

LeetCode 204: Count Primes
·2 mins
LeetCode Medium Array Math Enumeration Number-Theory
LeetCode 204 note: the sieve of Eratosthenes, crossing out multiples of each prime p starting at p², with the outer loop only up to √n
LeetCode 1979: Find Greatest Common Divisor of Array
·1 min
LeetCode Easy Math Number-Theory Gcd Array
LeetCode 1979 note: find the minimum and maximum, then take their GCD with Euclid’s algorithm
LeetCode 952: Largest Component Size by Common Factor
·2 mins
LeetCode Hard Math Number-Theory Union-Find Hash-Table Array
LeetCode 952 note: sharing a factor ⇔ sharing a prime factor; factor each number and union indices with the same prime factor using Union-Find, with no pairwise GCDs
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 313: Super Ugly Number
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 313 note: generalizing LC 264 with one pointer per prime; with a heap, avoid pushing the same number many times
LeetCode 396: Rotate Function
·2 mins
LeetCode Medium Array Math Dynamic-Programming
LeetCode 396 note: F(k) and F(k - 1) differ by one formula: F(k) = F(k - 1) + sum - n × the element moved to the front