
Move All Negative Elements to the End of an Array
Introduction In array-based problems, rearranging elements based on certain conditions is a common task. One such problem is moving all negative numbers to the end of the array while maintaining the relative order of elements. This type of problem helps in understanding array traversal and stable rearrangement techniques. Problem Statement Given an unsorted array containing both positive and negative integers, rearrange the array such that: All positive elements appear first All negative elements are moved to the end The * relative order of elements is preserved Example 1 Input: arr = [ 1 , - 1 , 3 , 2 , - 7 , - 5 , 11 , 6 ] Output: [ 1 , 3 , 2 , 11 , 6 , - 1 , - 7 , - 5 ] Explanation: All positive numbers are moved to the front, and negative numbers are placed at the end without changing their original order. Example 2 Input: arr = [ - 5 , 7 , - 3 , - 4 , 9 , 10 , - 1 , 11 ] Output: [ 7 , 9 , 10 , 11 , - 5 , - 3 , - 4 , - 1 ] Approach: Using Extra List (Simple Method) Traverse the arr
Continue reading on Dev.to Python
Opens in a new tab



