# Construct a distinct elements array with given size, sum and element upper bound in Python

Suppose we have one size variable N, we also have one variable SUM this is the total sum of all elements available in the array and another variable K such that there is no element in array is greater than K, We have to find one orthogonal array where all elements in the array are distinct. If there is no solution return -1.

So, if the input is like N = 4, SUM = 16 K = 9, then the output will be [1,2,4,9]

To solve this, we will follow these steps −

• minimum_sum := (N *(N + 1)) / 2

• maximum_sum := (N * K) -(N *(N - 1)) / 2

• if minimum_sum > SUM or maximum_sum < SUM, then

• return -1

• res := one array of size N + 1 and fill with 0 to N

• sum := minimum_sum

• i := N

• while i >= 1, do

• x := sum + (K - i)

• if x < SUM, then

• sum := sum +(K - i)

• res[i] := K

• K := K - 1

• otherwise,

• res[i] := res[i] +(SUM - sum)

• sum := SUM

• come out from the loop

• i := i - 1

• return res

## Example

Let us see the following implementation to get better understanding −

Live Demo

def get_arr(N, SUM, K):
minimum_sum = (N * (N + 1)) / 2
maximum_sum = (N * K) - (N * (N - 1)) / 2
if (minimum_sum > SUM or maximum_sum < SUM):
return -1
res = [i for i in range(N + 1)]
sum = minimum_sum
i = N
while(i >= 1):
x = sum + (K - i)
if (x < SUM):
sum = sum + (K - i)
res[i] = K
K -= 1
else:
res[i] += (SUM - sum)
sum = SUM
break
i -= 1
return res
N = 4
SUM = 16
K = 9
print(get_arr(N, SUM, K))

## Input

4, 16, 9

## Output

[0, 1, 2, 4.0, 9]

Updated on: 27-Aug-2020

108 Views
The text passage describes a programming problem that involves constructing a distinct elements array with given size, sum, and element upper bound. It provides a detailed algorithm to solve the problem and includes a Python code example with input and output.
no_question

the passage does not contain a mathematical question in the form of a word problem or calculation that is asked to be solved by the reader. instead, it provides a problem statement and a solution in the form of a python function. the input and output examples are part of the code demonstration, not a question or answer pair.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Construct a distinct elements array with given size, sum and element upper bound in Python

Suppose we have one size variable N, we also have one variable SUM this is the total sum of all elements available in the array and another variable K such that there is no element in array is greater than K, We have to find one orthogonal array where all elements in the array are distinct. If there is no solution return -1.

So, if the input is like N = 4, SUM = 16 K = 9, then the output will be [1,2,4,9]

To solve this, we will follow these steps −

• minimum_sum := (N *(N + 1)) / 2

• maximum_sum := (N * K) -(N *(N - 1)) / 2

• if minimum_sum > SUM or maximum_sum < SUM, then

• return -1

• res := one array of size N + 1 and fill with 0 to N

• sum := minimum_sum

• i := N

• while i >= 1, do

• x := sum + (K - i)

• if x < SUM, then

• sum := sum +(K - i)

• res[i] := K

• K := K - 1

• otherwise,

• res[i] := res[i] +(SUM - sum)

• sum := SUM

• come out from the loop

• i := i - 1

• return res

## Example

Let us see the following implementation to get better understanding −

Live Demo

def get_arr(N, SUM, K):
minimum_sum = (N * (N + 1)) / 2
maximum_sum = (N * K) - (N * (N - 1)) / 2
if (minimum_sum > SUM or maximum_sum < SUM):
return -1
res = [i for i in range(N + 1)]
sum = minimum_sum
i = N
while(i >= 1):
x = sum + (K - i)
if (x < SUM):
sum = sum + (K - i)
res[i] = K
K -= 1
else:
res[i] += (SUM - sum)
sum = SUM
break
i -= 1
return res
N = 4
SUM = 16
K = 9
print(get_arr(N, SUM, K))

## Input

4, 16, 9

## Output

[0, 1, 2, 4.0, 9]

Updated on: 27-Aug-2020

108 Views
user
The text passage describes a programming problem that involves constructing a distinct elements array with given size, sum, and element upper bound. It provides a detailed algorithm to solve the problem and includes a Python code example with input and output.

TYPE: NO_QUESTION

The passage does not contain a mathematical question in the form of a word problem or calculation that is asked to be solved by the reader. Instead, it provides a problem statement and a solution in the form of a Python function. The input and output examples are part of the code demonstration, not a question or answer pair.