Find the largest exponent that appears exactly once across prime factorizations

Quick Overview

Factor every number in an integer array into primes, gather the exponents from all the factorizations, and return the largest exponent that occurs exactly once, or -1 if none does. It tests prime factorization by trial division, frequency counting and careful handling of repeated exponents.

Find the largest exponent that appears exactly once across prime factorizations

Company: Sap

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums`, compute the prime factorization of each number. Each factorization is a set of (prime, exponent) pairs. Collect the exponents from the factorizations of every number in the array into one combined list. Return the largest exponent that appears exactly once in that combined list. If no exponent appears exactly once, return `-1`. For example, `12 = 2^2 * 3^1` contributes the exponents 2 and 1, and `8 = 2^3` contributes the exponent 3. ### Function Signature ```python def max_unique_exponent(nums: list[int]) -> int: ``` ### Rules - Every (prime, exponent) pair contributes one exponent to the list. `6 = 2^1 * 3^1` contributes the exponent 1 twice. - Every element of `nums` is factored on its own, so a value that appears twice in `nums` contributes its exponents twice. - "Appears exactly once" refers to the count of an exponent in the combined list over all numbers. ### Constraints - `1 <= len(nums) <= 10^4` - `2 <= nums[i] <= 10^6` ### Examples ```text Input: nums = [12, 8, 5] Output: 3 ``` The factorizations are `12 = 2^2 * 3^1`, `8 = 2^3` and `5 = 5^1`, so the combined list is [2, 1, 3, 1]. Exponent 1 appears twice, while 2 and 3 appear once each, and the larger of those is 3. ```text Input: nums = [4, 9, 25] Output: -1 ``` Each number is the square of a prime, so the combined list is [2, 2, 2]. No exponent appears exactly once. ```text Input: nums = [8, 8, 3] Output: 1 ``` Both copies of 8 contribute exponent 3, so 3 appears twice. The prime 3 contributes exponent 1, which appears once, so the answer is 1 even though a larger exponent is present.

Overview: Factor every number in an integer array into primes, gather the exponents from all the factorizations, and return the largest exponent that occurs exactly once, or -1 if none does. It tests prime factorization by trial division, frequency counting and careful handling of repeated exponents.

Sap logo
Sap
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums, compute the prime factorization of each number. Each factorization is a set of (prime, exponent) pairs. Collect the exponents from the factorizations of every number in the array into one combined list.

Return the largest exponent that appears exactly once in that combined list. If no exponent appears exactly once, return -1.

For example, 12 = 2^2 * 3^1 contributes the exponents 2 and 1, and 8 = 2^3 contributes the exponent 3.

Function Signature

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

Rules

  • Every (prime, exponent) pair contributes one exponent to the list. 6 = 2^1 * 3^1 contributes the exponent 1 twice.
  • Every element of nums is factored on its own, so a value that appears twice in nums contributes its exponents twice.
  • "Appears exactly once" refers to the count of an exponent in the combined list over all numbers.

Constraints

  • 1 <= len(nums) <= 10^4
  • 2 <= nums[i] <= 10^6

Examples

Input: nums = [12, 8, 5]
Output: 3

The factorizations are 12 = 2^2 * 3^1, 8 = 2^3 and 5 = 5^1, so the combined list is [2, 1, 3, 1]. Exponent 1 appears twice, while 2 and 3 appear once each, and the larger of those is 3.

Input: nums = [4, 9, 25]
Output: -1

Each number is the square of a prime, so the combined list is [2, 2, 2]. No exponent appears exactly once.

Input: nums = [8, 8, 3]
Output: 1

Both copies of 8 contribute exponent 3, so 3 appears twice. The prime 3 contributes exponent 1, which appears once, so the answer is 1 even though a larger exponent is present.