Construct a Permutation without an Ordered Arithmetic Triple

Quick Overview

Construct a Permutation without an Ordered Arithmetic Triple. Return any permutation of1 through n such that no positions i<k<j satisfy 2*nums[k] == nums[i]+nums[j].

Construct a Permutation without an Ordered Arithmetic Triple

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return any permutation of1 through n such that no positions i<k<j satisfy `2*nums[k] == nums[i]+nums[j]`. At least one valid permutation exists. The restriction concerns the order of positions as well as their numeric values. ```cpp class Solution { public: vector<int> beautifulArray(int n); }; ``` ### Examples **Example 1:** ```text Input: n = 3 Output: [1,3,2] Explanation: The only possible middle index contains3; twice3 differs from1+2. ``` **Example 2:** ```text Input: n = 6 Output: [1,5,3,2,6,4] Explanation: The odd block and even block each come from the valid order[1,3,2] by an affine transformation. Endpoints from different parity blocks cannot average to an integer middle value. ``` ### Constraints - `1 <= n <= 1000`. ### Contract considerations Every integer1 through n must occur exactly once. No particular valid permutation is prescribed. ```hint Read the boundary condition Separate odd and even values so an average cannot cross the parity blocks. ``` ### What a Strong Answer Covers Every integer1 through n must occur exactly once. No particular valid permutation is prescribed. ### Follow-up Questions Optional practice extension, separate from the original task: How could you validate the defining property of a proposed permutation?

Overview: Construct a Permutation without an Ordered Arithmetic Triple. Return any permutation of1 through n such that no positions i<k<j satisfy 2*nums[k] == nums[i]+nums[j].

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Return any permutation of1 through n such that no positions i<k<j satisfy 2*nums[k] == nums[i]+nums[j]. At least one valid permutation exists. The restriction concerns the order of positions as well as their numeric values.

class Solution {
public:
    vector<int> beautifulArray(int n);
};

Examples

Example 1:

Input: n = 3
Output: [1,3,2]
Explanation: The only possible middle index contains3; twice3 differs from1+2.

Example 2:

Input: n = 6
Output: [1,5,3,2,6,4]
Explanation: The odd block and even block each come from the valid order[1,3,2] by an affine transformation. Endpoints from different parity blocks cannot average to an integer middle value.

Constraints

  • 1 <= n <= 1000 .

Contract considerations

Every integer1 through n must occur exactly once. No particular valid permutation is prescribed.

What a Strong Answer Covers Guidance

Every integer1 through n must occur exactly once. No particular valid permutation is prescribed.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How could you validate the defining property of a proposed permutation?