Skip to main content

Determine the Minimum Sum of a k-avoiding Array - Solution & Explanation

MediumMathGreedy15 min readAsked at: Infosys
Practice this problem

Problem Statement

You are given two integers, n and k.

An array of distinct positive integers is called a k-avoiding array if there does not exist any pair of distinct elements that sum to k.

Return the minimum possible sum of a k-avoiding array of length n.

 

Example 1:

Input: n = 5, k = 4
Output: 18
Explanation: Consider the k-avoiding array [1,2,4,5,6], which has a sum of 18.
It can be proven that there is no k-avoiding array with a sum less than 18.

Example 2:

Input: n = 2, k = 6
Output: 3
Explanation: We can construct the array [1,2], which has a sum of 3.
It can be proven that there is no k-avoiding array with a sum less than 3.

 

Constraints:

  • 1 <= n, k <= 50

Approach Overview

Problem Overview: You need to build an array of n positive integers such that no two elements add up to k. Among all valid arrays, return the minimum possible sum of its elements.

Approach 1: Incremental Element Selection (Greedy with Set) (Time: O(n), Space: O(n))

Build the array one number at a time starting from 1. For every candidate number x, check whether its complement k - x already exists in the chosen set. If it does, adding x would create a pair whose sum equals k, so skip it. Otherwise include x and continue until you collect n numbers.

This works because choosing the smallest valid numbers always minimizes the total sum. A hash set allows constant‑time complement checks while iterating through natural numbers. The algorithm performs at most a few extra skips, so it remains linear in practice. This approach directly models the greedy rule and is easy to reason about during interviews involving greedy decisions and simple math observations.

Approach 2: Direct Number Generation (Math + Greedy Insight) (Time: O(n), Space: O(1))

Observe the structure of forbidden pairs: x conflicts with k - x. To minimize the sum, you want the smallest possible integers. All numbers from 1 up to k / 2 can safely be chosen because their complements lie in the larger range near k. After selecting those values, the next safe numbers must start from k, since any number between k/2 and k-1 would pair with something already chosen.

So the construction becomes deterministic: first take numbers from 1 upward until reaching k/2 (or until the array already has n elements). If more elements are needed, continue from k, k+1, and so on. This avoids complement conflicts entirely and removes the need for a hash set. The result is a clean arithmetic construction based purely on math reasoning.

Recommended for interviews: The incremental greedy approach demonstrates clear understanding of the constraint by explicitly checking complements. Interviewers often accept it first because it is intuitive and easy to verify. The direct generation method shows deeper pattern recognition and stronger greedy reasoning, making it the most polished final solution.

Approach 1: Approach with Incremental Element Selection

This approach selects the smallest natural numbers step by step until the desired length n is achieved ensuring no pair sums to k. If a number would create a pair summing to k with a number already in the array, it is skipped.

The solution initializes the current number at 1 and constructs the sum while ensuring no two numbers sum to k. If a potential complement exists that already achieves sum k, it skips to the next number.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

The time complexity is O(n) since we potentially look at up to n numbers, and the space complexity is O(1) as only variables are used.

Try this approach in the editor →

Approach 2: Approach with Direct Number Generation

This approach leverages mathematical deduction to directly determine the number set providing the minimum sum by considering consecutive numbers unless they have a complement already in the set adding up to k. If a conflict arises, it skips directly to numbers above k.

The strategy involves calculating elements based on their position relative to k/2, directly adjusting if they begin to overlap with complements in the resultant numbering schema without iteration.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time complexity is O(n) due to a single loop through n numbers. Space complexity remains O(1).

Try this approach in the editor →

Approach 3: Greedy + Simulation

Starting from the positive integer i = 1, we sequentially determine if i can be added to the array. If it can be added, we add i to the array, accumulate it to the answer, and then mark k - i as visited, indicating that k-i cannot be added to the array. We continue this process until the array's length reaches n.

The time complexity is O(n + k), and the space complexity is O(n + k). Where n is the length of the array.

Code

Python

Java

C++

Go

TypeScript

Rust

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach with Incremental Element Selection

The time complexity is O(n) since we potentially look at up to n numbers, and the space complexity is O(1) as only variables are used.

Approach with Direct Number Generation

Time complexity is O(n) due to a single loop through n numbers. Space complexity remains O(1).

Greedy + Simulation—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Incremental Element Selection (Greedy + Set)O(n)O(n)When explaining the logic step‑by‑step or demonstrating greedy reasoning with complement checks
Direct Number Generation (Math Insight)O(n)O(1)Best optimized solution once the pattern of forbidden pairs around k is recognized

Video Solution

Leetcode Weekly contest 359 - Medium- Determine the Minimum Sum of a k-avoiding Array • Prakhar Agrawal • 603 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Determine the Minimum Sum of a k-avoiding Array easy or hard?
LeetCode classifies this problem as Medium. The greedy idea is straightforward once you notice that pairs summing to k must be avoided, but recognizing the mathematical pattern that allows direct number generation requires some insight.
Determine the Minimum Sum of a k-avoiding Array Python/Java solution
Implement a greedy loop that keeps adding the smallest valid integer until n elements are chosen. In Python or Java, store selected numbers in a HashSet to check complements in O(1) time, or directly generate numbers from 1 to k/2 and then from k upward.
How to solve Determine the Minimum Sum of a k-avoiding Array in O(n)?
Iterate through positive integers and greedily pick the smallest valid number. Either check if k - x already exists using a hash set, or apply the mathematical observation that numbers up to k/2 can be taken safely and remaining numbers should start from k. Both methods build the array in linear time.
What is the best approach for Determine the Minimum Sum of a k-avoiding Array?
The best approach uses a greedy construction based on the observation that numbers x and k-x cannot both appear. Select numbers from 1 upward until reaching k/2, then continue from k if more elements are needed. This produces the minimum sum while maintaining the constraint in O(n) time and O(1) space.
Is Determine the Minimum Sum of a k-avoiding Array asked at Google/Amazon/Meta?
Greedy construction and complement‑avoidance patterns appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact problem may vary, the underlying idea of avoiding pair sums and minimizing totals is a common interview theme.
What data structure is used in Determine the Minimum Sum of a k-avoiding Array?
The straightforward solution uses a hash set to track chosen numbers and quickly check whether the complement k - x already exists. The optimized mathematical approach removes the data structure entirely and generates numbers directly.
What is the time complexity of Determine the Minimum Sum of a k-avoiding Array?
Both common solutions run in O(n) time where n is the required array size. The greedy set-based method also uses O(n) extra space to track chosen values, while the direct math-based generation runs in O(1) space.

Ready to solve this problem?

Practice Determine the Minimum Sum of a k-avoiding Array with our built-in code editor and test cases.

Practice on FleetCode