FastPrepPartition Into Groups of Five Consecutive Values

Partition Into Groups of Five Consecutive Values

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given an integer array nums, determine whether all occurrences can be partitioned into groups of exactly five values such that every group can be reordered as x, x + 1, x + 2, x + 3, x + 4.

Each array occurrence must belong to exactly one group. Equal values in the input are separate occurrences. The empty array can be partitioned into zero groups.

Function

canPartitionIntoFiveConsecutiveGroups(nums: int[]) → boolean

Examples

Example 1

nums = [1,2,3,4,5,2,3,4,5,6]return = true

The occurrences form [1,2,3,4,5] and [2,3,4,5,6].

Example 2

nums = [1,2,3,4,5,6]return = false

Six occurrences cannot be divided into groups of five.

Example 3

nums = []return = true

Zero groups use every occurrence of the empty input.

Constraints

  • 0 <= nums.length <= 200000.
  • -10^9 <= nums[i] <= 10^9.

More Google problems

See Google hiring insights
public boolean canPartitionIntoFiveConsecutiveGroups(int[] nums) {
    // Write your code here.
}
nums[1,2,3,4,5,2,3,4,5,6]
expectedtrue
Checking account…