KatInterview

667. Beautiful Arrangement II

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to construct a list of n distinct integers from 1 to n such that the list of absolute differences between adjacent elements has exactly k distinct values. We can return any valid construction.

Solution

Beautiful Arrangement II in Python

class Solution:
    def constructArray(self, n: int, k: int) -> List[int]:
        answer = []

        # Step 1: Append numbers 1 to n-k-1 to the answer.
        # These will contribute only the difference of 1.
        # list(range(1, n - k)) generates numbers from 1 up to n-k-1.
        answer.extend(list(range(1, n - k)))
        
        # Step 2: Use a two-pointer approach for the remaining numbers.
        # The numbers are from n-k to n. This part will generate k distinct differences.
        low = n - k
        high = n
        while low <= high:
            # Add the smallest available number
            answer.append(low)
            low += 1
            # If there are more numbers, add the largest available number
            if low <= high:
                answer.append(high)
                high -= 1

        return answer

Beautiful Arrangement II in Java

class Solution {
    public int[] constructArray(int n, int k) {
        int[] answer = new int[n];
        int index = 0;

        // Step 1: Append numbers 1 to n-k-1 to the answer.
        // These will contribute only the difference of 1.
        // Note that if k = n-1, then n-k = 1, and this loop doesn't run, which is correct.
        for (int i = 1; i < n - k; i++) {
            answer[index++] = i;
        }

        // Step 2: Use a two-pointer approach for the remaining numbers.
        // The numbers are from n-k to n. This part will generate k distinct differences.
        int low = n - k;
        int high = n;
        while (low <= high) {
            // Add the smallest available number
            answer[index++] = low++;
            // If there are more numbers, add the largest available number
            if (low <= high) {
                answer[index++] = high--;
            }
        }

        return answer;
    }
}

Beautiful Arrangement II in C++

class Solution {
public:
    vector<int> constructArray(int n, int k) {
        vector<int> answer;
        answer.reserve(n);

        // Step 1: Append numbers 1 to n-k-1 to the answer.
        // These will contribute only the difference of 1.
        for (int i = 1; i < n - k; ++i) {
            answer.push_back(i);
        }

        // Step 2: Use a two-pointer approach for the remaining numbers.
        // The numbers are from n-k to n. This part will generate k distinct differences.
        int low = n - k;
        int high = n;
        while (low <= high) {
            // Add the smallest available number
            answer.push_back(low++);
            // If there are more numbers, add the largest available number
            if (low <= high) {
                answer.push_back(high--);
            }
        }

        return answer;
    }
};

Beautiful Arrangement II in TypeScript

function constructArray(n: number, k: number): number[] {
    const answer: number[] = [];

    // Step 1: Append numbers 1 to n-k-1 to the answer.
    // These will contribute only the difference of 1.
    // Note that if k = n-1, then n-k = 1, and this loop doesn't run, which is correct.
    for (let i = 1; i < n - k; i++) {
        answer.push(i);
    }

    // Step 2: Use a two-pointer approach for the remaining numbers.
    // The numbers are from n-k to n. This part will generate k distinct differences.
    let low = n - k;
    let high = n;
    while (low <= high) {
        // Add the smallest available number
        answer.push(low);
        low++;
        // If there are more numbers, add the largest available number
        if (low <= high) {
            answer.push(high);
            high--;
        }
    }

    return answer;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.