Back to articles
Move all Negative Elements to End - CA09

Move all Negative Elements to End - CA09

via Dev.toLokeshwaran S

My Thinking and Approach Introduction In this problem, I was given an array containing both positive and negative integers and asked to move all negative elements to the end. The important condition is that the relative order of positive and negative elements should not be changed. Problem Statement Given an array arr containing: Positive numbers Negative numbers Move all negative elements to the end Conditions: Maintain order of elements Perform operation in-place My Initial Thought At first, I considered: Creating two separate lists One for positive elements One for negative elements Then combining them But this approach uses extra space. Key Observation To maintain order: I should first place all positive elements Then place all negative elements Optimized Approach I decided to: Use an additional list to store elements in order Copy elements back to original array Logic: Traverse array Add positive elements first Add negative elements next Copy back to original array My Approach (St

Continue reading on Dev.to

Opens in a new tab

Read Full Article
7 views

Related Articles