Split an array so every left element is at most every right element, with the shortest left part

Quick Overview

Given an integer array, find the shortest prefix whose largest value is at most the smallest value in the rest of the array, and return its length. It tests reasoning about running maximums and minimums and writing a single linear scan with constant extra space.

Split an array so every left element is at most every right element, with the shortest left part

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums`, split it into two contiguous parts, `left` (a prefix) and `right` (the rest of the array), so that: - Every element in `left` is less than or equal to every element in `right`. - `left` and `right` are both non-empty. - `left` has the smallest possible size. Return the length of `left` after such a split. ### Function Signature ```python def partition_disjoint(nums: list[int]) -> int: ``` ### Rules - `left` is `nums[0:k]` and `right` is `nums[k:]` for some `k` with `1 <= k <= len(nums) - 1`. Return that `k`. - The condition is `max(left) <= min(right)`, so equal values may sit on both sides. - Because `left` must be as short as possible, the answer is unique. ### Constraints - `2 <= len(nums) <= 10^5` - `0 <= nums[i] <= 10^6` - There is at least one valid split for the given input. ### Examples ```text Input: nums = [5, 0, 3, 8, 6] Output: 3 ``` With `left = [5, 0, 3]` and `right = [8, 6]`, the largest left value is 5 and the smallest right value is 6. A shorter `left` fails because the 0 or the 3 would end up on the right side, below 5. ```text Input: nums = [1, 1, 1, 0, 6, 12] Output: 4 ``` The 0 at index 3 is smaller than the leading 1s, so it has to be in `left`. Then `left = [1, 1, 1, 0]` has max 1 and `right = [6, 12]` has min 6. ```text Input: nums = [2, 1, 3, 0, 5] Output: 4 ``` The 0 at index 3 forces `left` to reach index 3, which also pulls the 3 at index 2 into `left`, so the left maximum is 3. With `right = [5]`, the condition 3 <= 5 holds.

Overview: Given an integer array, find the shortest prefix whose largest value is at most the smallest value in the rest of the array, and return its length. It tests reasoning about running maximums and minimums and writing a single linear scan with constant extra space.

Grab logo
Grab
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, split it into two contiguous parts, left (a prefix) and right (the rest of the array), so that:

  • Every element in left is less than or equal to every element in right .
  • left and right are both non-empty.
  • left has the smallest possible size.

Return the length of left after such a split.

Function Signature

def partition_disjoint(nums: list[int]) -> int:

Rules

  • left is nums[0:k] and right is nums[k:] for some k with 1 <= k <= len(nums) - 1 . Return that k .
  • The condition is max(left) <= min(right) , so equal values may sit on both sides.
  • Because left must be as short as possible, the answer is unique.

Constraints

  • 2 <= len(nums) <= 10^5
  • 0 <= nums[i] <= 10^6
  • There is at least one valid split for the given input.

Examples

Input: nums = [5, 0, 3, 8, 6]
Output: 3

With left = [5, 0, 3] and right = [8, 6], the largest left value is 5 and the smallest right value is 6. A shorter left fails because the 0 or the 3 would end up on the right side, below 5.

Input: nums = [1, 1, 1, 0, 6, 12]
Output: 4

The 0 at index 3 is smaller than the leading 1s, so it has to be in left. Then left = [1, 1, 1, 0] has max 1 and right = [6, 12] has min 6.

Input: nums = [2, 1, 3, 0, 5]
Output: 4

The 0 at index 3 forces left to reach index 3, which also pulls the 3 at index 2 into left, so the left maximum is 3. With right = [5], the condition 3 <= 5 holds.