LeetCode Entry

3069. Distribute Elements Into Two Arrays I

20.08.2026 easy 2026 kotlin rust

Shuffle by adding to the biggest half

3069. Distribute Elements Into Two Arrays I easy substack youtube

https://dmitrysamoylenko.com/leetcode/

20.08.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1457

Problem TLDR

Shuffle by adding to the biggest half

Intuition

Just follow the description

Approach

  • optimized version uses no extra containers
  • can be done in-place?

Complexity

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

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

Code

    fun resultArray(n: IntArray) = run {
        val a = arrayListOf(n[0]); val b = arrayListOf(n[1])
        for (x in n.drop(2)) (if (a.last() > b.last()) a else b) += x
        a + b
    }
    pub fn result_array(n: Vec<i32>) -> Vec<i32> {
        let (mut a, mut b) = (vec![n[0]], vec![n[1]]);
        for &x in &n[2..] {
            (if a.last() > b.last() { &mut a } else { &mut b }).push(x);
        }
        a.extend(b); a
    }

Comments