FastPrepSort a Binary Array

Sort a Binary Array

AMD logoAMD● EasyFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an integer array containing only 0 and 1, return the values in non-decreasing order.

Preserve the input array and return a newly arranged array.

Function

sortBinaryArray(nums: int[]) → int[]

Examples

Example 1

nums = [1,0,1,0,0]return = [0,0,0,1,1]

All zeros precede all ones.

Example 2

nums = [1,1]return = [1,1]

An already sorted array is unchanged.

Constraints

  • 0 <= nums.length <= 100000.
  • Every value is 0 or 1.

Source note: Author-written prompt excerpt retained for source fidelity.

More AMD problems

See AMD hiring insights
public int[] sortBinaryArray(int[] nums) {
    // Write your code here.
}
nums[1,0,1,0,0]
expected[0,0,0,1,1]
Checking account…