Find the array values that appear exactly once and divide the product of all elements

Quick Overview

Given an array of positive integers, return every value that appears exactly once and also divides the product of all elements, sorted in ascending order. The problem tests frequency counting with a hash map and careful reading of the divisibility condition.

Find the array values that appear exactly once and divide the product of all elements

Company: Nubank

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of positive integers `nums`, a **unique factor** is an integer that satisfies both of the following conditions: - It appears in `nums` exactly once. - It divides the product of all elements in `nums`. Return a list of all unique factors found in `nums`, sorted in ascending order. If no such element exists, return an empty list. ### Function Signature ```python def unique_factor(nums: list[int]) -> list[int]: ``` ### Rules - A unique factor is a value taken from `nums`, and each one is reported once. - Return the values sorted in ascending order. An empty list is a valid answer. ### Constraints - `1 <= len(nums) <= 10^5` - `1 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [2,3,2,5,7,7] Output: [3,5] ``` The product of all elements is 2940. The values 3 and 5 each appear exactly once and both divide 2940. The values 2 and 7 appear twice, so they are excluded. ```text Input: nums = [4,4,4] Output: [] ``` The only value, 4, appears three times, so no element appears exactly once and the result is empty. ```text Input: nums = [1] Output: [1] ``` The value 1 appears once, and it divides the product, which is 1.

Overview: Given an array of positive integers, return every value that appears exactly once and also divides the product of all elements, sorted in ascending order. The problem tests frequency counting with a hash map and careful reading of the divisibility condition.

Nubank logo
Nubank
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of positive integers nums, a unique factor is an integer that satisfies both of the following conditions:

  • It appears in nums exactly once.
  • It divides the product of all elements in nums .

Return a list of all unique factors found in nums, sorted in ascending order. If no such element exists, return an empty list.

Function Signature

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

Rules

  • A unique factor is a value taken from nums , and each one is reported once.
  • Return the values sorted in ascending order. An empty list is a valid answer.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= nums[i] <= 10^9

Examples

Input: nums = [2,3,2,5,7,7]
Output: [3,5]

The product of all elements is 2940. The values 3 and 5 each appear exactly once and both divide 2940. The values 2 and 7 appear twice, so they are excluded.

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

The only value, 4, appears three times, so no element appears exactly once and the result is empty.

Input: nums = [1]
Output: [1]

The value 1 appears once, and it divides the product, which is 1.