PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/xAI

Maximize distinct values after unique ± offsets

Last updated: Mar 29, 2026

Quick Overview

This question evaluates combinatorial reasoning and algorithmic optimization skills, focusing on transforming arrays under distinct integer-offset constraints and reasoning about uniqueness of resulting values.

  • hard
  • xAI
  • Coding & Algorithms
  • Software Engineer

Maximize distinct values after unique ± offsets

Company: xAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem You are given an integer array `a` of length `n` and a non-negative integer `k`. For each index `i`, you must choose: - an integer offset `d_i` such that `0 <= d_i <= k`, and - a sign, either `+` or `-`, and replace `a[i]` with `a[i] + d_i` or `a[i] - d_i`. **Constraint:** All chosen offsets must be **distinct** across indices (i.e., `d_i != d_j` for `i != j`). Your goal is to **maximize the number of distinct integers** in the resulting array after all replacements. Return that maximum possible number of distinct values. ## Example - Input: `a = [0, 0, 0]`, `k = 1` - Output: `3` - Explanation: Choose offsets `0, 1` (but offsets must be distinct, so for 3 elements we can use `d = 0, 1` only if `k=1`; instead, one valid construction is to allow `d` to be chosen per element distinctly within `[0,k]`—for this example, a maximal distinct outcome is `[-1, 0, 1]` by applying `-1, 0, +1` to the three zeros. ## Notes / Clarifications - Offsets are integers. - Offset `0` is allowed. - The sign choice is independent per element. ## Constraints (typical interview setting) - `1 <= n <= 2 * 10^5` - `0 <= k <= 10^9` - `-10^9 <= a[i] <= 10^9` Implement a function that returns the maximum possible number of distinct values.

Quick Answer: This question evaluates combinatorial reasoning and algorithmic optimization skills, focusing on transforming arrays under distinct integer-offset constraints and reasoning about uniqueness of resulting values.

Related Interview Questions

  • Flatten and unflatten nested Python structures - xAI (nan)
  • Compute dasher pay from order events - xAI (nan)
  • Compute total active time per Twitter Space - xAI (medium)
  • Design a Recoverable Iterator - xAI (medium)
  • Implement Distributed Matrix Multiplication - xAI (hard)
xAI logo
xAI
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
20
0
Loading...

Problem

You are given an integer array a of length n and a non-negative integer k.

For each index i, you must choose:

  • an integer offset d_i such that 0 <= d_i <= k , and
  • a sign, either + or - ,

and replace a[i] with a[i] + d_i or a[i] - d_i.

Constraint: All chosen offsets must be distinct across indices (i.e., d_i != d_j for i != j).

Your goal is to maximize the number of distinct integers in the resulting array after all replacements.

Return that maximum possible number of distinct values.

Example

  • Input: a = [0, 0, 0] , k = 1
  • Output: 3
  • Explanation: Choose offsets 0, 1 (but offsets must be distinct, so for 3 elements we can use d = 0, 1 only if k=1 ; instead, one valid construction is to allow d to be chosen per element distinctly within [0,k] —for this example, a maximal distinct outcome is [-1, 0, 1] by applying -1, 0, +1 to the three zeros.

Notes / Clarifications

  • Offsets are integers.
  • Offset 0 is allowed.
  • The sign choice is independent per element.

Constraints (typical interview setting)

  • 1 <= n <= 2 * 10^5
  • 0 <= k <= 10^9
  • -10^9 <= a[i] <= 10^9

Implement a function that returns the maximum possible number of distinct values.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More xAI•More Software Engineer•xAI Software Engineer•xAI Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.