Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Construct a distinct elements array with given size, sum and element upper bound in Python
Creating an array with distinct elements that has a specific size, sum, and maximum element value is a common algorithmic problem. We need to construct an array where all elements are distinct, within bounds, and sum to the target value.
Problem Understanding
Given three parameters:
- N: Size of the array
- SUM: Required sum of all elements
- K: Upper bound for any element in the array
We need to find an array of N distinct elements where no element exceeds K and the total sum equals SUM. If impossible, return -1.
Algorithm Approach
The solution uses a greedy approach:
- Calculate minimum possible sum: 1+2+3+...+N = N*(N+1)/2
- Calculate maximum possible sum with constraint K
- Start with the smallest distinct elements [1,2,3,...,N]
- Adjust elements from largest to smallest to reach target sum
Example
Let's implement the solution with proper formatting ?
def get_distinct_array(N, SUM, K):
# Calculate minimum and maximum possible sums
minimum_sum = (N * (N + 1)) // 2
maximum_sum = (N * K) - (N * (N - 1)) // 2
# Check if solution is possible
if minimum_sum > SUM or maximum_sum < SUM:
return -1
# Initialize array with [1, 2, 3, ..., N]
result = [i for i in range(1, N + 1)]
current_sum = minimum_sum
# Adjust elements from largest to smallest
i = N - 1 # Index of last element
while i >= 0 and current_sum < SUM:
# Maximum we can increase current element
max_increase = K - result[i]
# Amount we need to add to reach target sum
needed = SUM - current_sum
# Increase current element by minimum of both
increase = min(max_increase, needed)
result[i] += increase
current_sum += increase
i -= 1
return result
# Test the function
N = 4
SUM = 16
K = 9
print("Input: N =", N, "SUM =", SUM, "K =", K)
print("Output:", get_distinct_array(N, SUM, K))
Input: N = 4 SUM = 16 K = 9 Output: [1, 2, 4, 9]
How It Works
For N=4, SUM=16, K=9:
- Start with [1, 2, 3, 4] (sum = 10)
- Need to add 6 more to reach sum 16
- Increase last element: 4 ? 9 (adds 5, sum = 15)
- Increase third element: 3 ? 4 (adds 1, sum = 16)
- Final array: [1, 2, 4, 9]
Edge Cases
Testing when no solution exists ?
# Test cases where no solution exists
print("Test 1 - Sum too small:")
print(get_distinct_array(3, 5, 10)) # Minimum sum is 6
print("\nTest 2 - Sum too large:")
print(get_distinct_array(3, 50, 5)) # Maximum sum is 12
print("\nTest 3 - Valid case:")
print(get_distinct_array(3, 10, 6)) # Should work
Test 1 - Sum too small: -1 Test 2 - Sum too large: -1 Test 3 - Valid case: [1, 3, 6]
Conclusion
This greedy algorithm efficiently constructs distinct element arrays by starting with the minimum configuration and adjusting larger elements first. The time complexity is O(N) and it handles edge cases by checking sum bounds before processing.
