As reported: Salesforce LMTS loop (April 2025), DSA round, the second problem after the K-th greatest element for every size from K to N.
Rearrange the integer array arr in place so that all negative numbers come first, followed by all the other numbers. Any order inside each group is accepted, and the function returns nothing.
Example 1
arr = [3,-1,4,-1,-5,9,2]
One valid result: [-1,-1,-5,3,4,9,2]
Example 2
arr = [0,-2,5]
One valid result: [-2,0,5]
Constraints: 1 ≤ arr.length ≤ 10^5; -10^9 ≤ arr[i] ≤ 10^9; use O(1) extra space.
Clarifications
0belongs with the non-negative numbers.
Code runner format: one line: arr as a JSON array. Implement segregate(arr), which changes arr in place. The harness checks that the result holds the same numbers and that no non-negative number comes before a negative one, and prints true when it does.