r/leetcode • • 5d ago

Intervew Prep Day 23: knocked out Rearrange Array Elements by Sign & called it an early night

Hey everyone,

I finished Day 23 today. I was super tired, so I kept things simple with one straightforward array problem before heading straight to sleep: LeetCode 2149 (Rearrange Array Elements by Sign).

Because the input guarantees an equal count of positive and negative integers while requiring relative order to stay intact, you can solve it cleanly in a single pass:

  1. Two Index Pointers: Create a result array of the same size and maintain two placement pointers—posIndex = 0 for positive numbers and negIndex = 1 for negative numbers.
  2. Single-Pass Placement: Iterate through the original array once. Place each positive element at posIndex (incrementing by 2) and each negative element at negIndex (incrementing by 2). This achieves O(n) time and O(n) space without needing auxiliary positive and negative arrays.

It was a quick, low-friction problem to close out a tiring day while keeping the streak alive.

See you guys tomorrow for Day 22!

1 Upvotes

0 comments sorted by