Return a Largest Pairwise-Divisible Subset

Quick Overview

Practice selecting a maximum-size subset whose values are pairwise divisible. Return any optimal membership and explain correctness, including cases with multiple valid answers.

Return a Largest Pairwise-Divisible Subset

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Return a Largest Pairwise-Divisible Subset Given a list `nums` of distinct positive integers, return a subset with the greatest possible number of elements such that every pair of its elements divides in at least one direction. For any two selected values `a` and `b`, either `a % b == 0` or `b % a == 0` must hold. If several maximum-size subsets exist, return any one of them. ### Function `vector<int> largestDivisibleSubset(vector<int>& nums)` ### Example 1 ```text Input: nums = [2,3,6] One valid output: [2,6] ``` `[3,6]` is also valid and has the same maximum size. The subset `[2,3,6]` is invalid because neither `2` nor `3` divides the other. ### Example 2 ```text Input: nums = [4,8,16] Output: [4,8,16] ``` ### Constraints - `1 <= nums.length <= 1000`. - `1 <= nums[i] <= 2000000000`. - All values are distinct. ### Clarifications Return selected values, not their count or indices. Reusing a value is not allowed. A subset has no required display order. Different optimal subsets may have different members; there is no required tie-breaking rule. ### What a Strong Answer Covers - The condition on every selected pair. - Why the returned subset has maximum size. - Single-element inputs and multiple equally good outputs. - A correctness argument and resource bounds at the stated limits. ### Follow-up Questions - If two implementations return different subsets for the same input, what would you check to decide whether both are correct without requiring their chosen members or display order to match?

Overview: Practice selecting a maximum-size subset whose values are pairwise divisible. Return any optimal membership and explain correctness, including cases with multiple valid answers.

Google logo
Google
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Return a Largest Pairwise-Divisible Subset

Given a list nums of distinct positive integers, return a subset with the greatest possible number of elements such that every pair of its elements divides in at least one direction. For any two selected values a and b, either a % b == 0 or b % a == 0 must hold.

If several maximum-size subsets exist, return any one of them.

Function

vector<int> largestDivisibleSubset(vector<int>& nums)

Example 1

Input: nums = [2,3,6]
One valid output: [2,6]

[3,6] is also valid and has the same maximum size. The subset [2,3,6] is invalid because neither 2 nor 3 divides the other.

Example 2

Input: nums = [4,8,16]
Output: [4,8,16]

Constraints

  • 1 <= nums.length <= 1000 .
  • 1 <= nums[i] <= 2000000000 .
  • All values are distinct.

Clarifications

Return selected values, not their count or indices. Reusing a value is not allowed. A subset has no required display order. Different optimal subsets may have different members; there is no required tie-breaking rule.

What a Strong Answer Covers Guidance

  • The condition on every selected pair.
  • Why the returned subset has maximum size.
  • Single-element inputs and multiple equally good outputs.
  • A correctness argument and resource bounds at the stated limits.

Follow-up Questions Guidance

  • If two implementations return different subsets for the same input, what would you check to decide whether both are correct without requiring their chosen members or display order to match?