LeetCode Entry

2574. Left and Right Sum Differences

06.06.2026 easy 2026 kotlin rust

abs(prefix sum - suffix sum)

2574. Left and Right Sum Differences easy substack youtube

https://dmitrysamoylenko.com/leetcode/

06.06.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1382

Problem TLDR

abs(prefix sum - suffix sum)

Intuition

  • we can use math to just re-use the sum as is

Approach

Compute the sum - this is the suffix; use a single variable for prefix sum. Problem is small 1000 elements and 10^5 items, meaning we are in 32 bits

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\)

Code

    fun leftRightDifference(n: IntArray) =
    n.run {val s = sum(); var l = 0; map {l += it; abs(2*l-it-s)}}
    pub fn left_right_difference(n: Vec<i32>) -> Vec<i32> {
        let (s, mut l) = (n.iter().sum::<i32>(), 0);
        n.iter().map(|x| { l += x; (2*l-x-s).abs()}).collect()
    }

Comments