0 out of 464 challenges solved
Write a function `re_arrange_array(arr, n)` that takes in an array `arr` and an integer `n`, and re-arranges the first `n` elements of the given array so that all negative elements appear before positive ones, while preserving the relative order among negative and positive elements. #### Example Usage ```python [main.nopy] re_arrange_array([-1, 2, -3, 4, 5, 6, -7, 8, 9], 9) # Output: [-1, -3, -7, 2, 4, 5, 6, 8, 9] re_arrange_array([12, -14, -26, 13, 15], 5) # Output: [-14, -26, 12, 13, 15] re_arrange_array([10, 24, 36, -42, -39, -78, 85], 7) # Output: [-42, -39, -78, 10, 24, 36, 85] ``` #### Constraints - The input array `arr` will contain integers. - The integer `n` will be a positive integer less than or equal to the length of `arr`. - The function should modify the array in-place and return it.
def re_arrange_array(arr, n): """ Re-arranges the first n elements of the array so that all negative elements appear before positive ones, preserving their relative order. Parameters: arr (list): The input array of integers. n (int): The number of elements to consider for re-arrangement. Returns: list: The modified array with the first n elements re-arranged. """ # Placeholder for the solution pass